|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package envtest |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/ghodss/yaml" |
| 26 | + apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" |
| 27 | + "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" |
| 28 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 29 | + "k8s.io/apimachinery/pkg/util/sets" |
| 30 | + "k8s.io/apimachinery/pkg/util/wait" |
| 31 | + "k8s.io/client-go/rest" |
| 32 | +) |
| 33 | + |
| 34 | +// CRDInstallOptions are the options for installing CRDs |
| 35 | +type CRDInstallOptions struct { |
| 36 | + // Paths is the path to the directory containing CRDs |
| 37 | + Paths []string |
| 38 | + |
| 39 | + // CRDs is a list of CRDs to install |
| 40 | + CRDs []*apiextensionsv1beta1.CustomResourceDefinition |
| 41 | + |
| 42 | + // maxTime is the max time to wait |
| 43 | + maxTime time.Duration |
| 44 | + |
| 45 | + // pollInterval is the interval to check |
| 46 | + pollInterval time.Duration |
| 47 | +} |
| 48 | + |
| 49 | +const defaultPollInterval = 100 * time.Millisecond |
| 50 | +const defaultMaxWait = 10 * time.Second |
| 51 | + |
| 52 | +// InstallCRDs installs a collection of CRDs into a cluster by reading the crd yaml files from a directory |
| 53 | +func InstallCRDs(config *rest.Config, options CRDInstallOptions) ([]*apiextensionsv1beta1.CustomResourceDefinition, error) { |
| 54 | + defaultCRDOptions(&options) |
| 55 | + |
| 56 | + // Read the CRD yamls into options.CRDs |
| 57 | + if err := readCRDFiles(&options); err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + // Create the CRDs in the apiserver |
| 62 | + if err := CreateCRDs(config, options.CRDs); err != nil { |
| 63 | + return options.CRDs, err |
| 64 | + } |
| 65 | + |
| 66 | + // Wait for the CRDs to appear as Resources in the apiserver |
| 67 | + if err := WaitForCRDs(config, options.CRDs, options); err != nil { |
| 68 | + return options.CRDs, err |
| 69 | + } |
| 70 | + |
| 71 | + return options.CRDs, nil |
| 72 | +} |
| 73 | + |
| 74 | +// readCRDFiles reads the directories of CRDs in options.Paths and adds the CRD structs to options.CRDs |
| 75 | +func readCRDFiles(options *CRDInstallOptions) error { |
| 76 | + if len(options.Paths) > 0 { |
| 77 | + for _, path := range options.Paths { |
| 78 | + new, err := readCRDs(path) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + options.CRDs = append(options.CRDs, new...) |
| 83 | + } |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// defaultCRDOptions sets the default values for CRDs |
| 89 | +func defaultCRDOptions(o *CRDInstallOptions) { |
| 90 | + if o.maxTime == 0 { |
| 91 | + o.maxTime = defaultMaxWait |
| 92 | + } |
| 93 | + if o.pollInterval == 0 { |
| 94 | + o.pollInterval = defaultPollInterval |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// WaitForCRDs waits for the CRDs to appear in discovery |
| 99 | +func WaitForCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResourceDefinition, options CRDInstallOptions) error { |
| 100 | + // Add each CRD to a map of GroupVersion to Resource |
| 101 | + waitingFor := map[schema.GroupVersion]*sets.String{} |
| 102 | + for _, crd := range crds { |
| 103 | + gv := schema.GroupVersion{Group: crd.Spec.Group, Version: crd.Spec.Version} |
| 104 | + if _, found := waitingFor[gv]; !found { |
| 105 | + // Initialize the set |
| 106 | + waitingFor[gv] = &sets.String{} |
| 107 | + } |
| 108 | + // Add the Resource |
| 109 | + waitingFor[gv].Insert(crd.Spec.Names.Plural) |
| 110 | + } |
| 111 | + |
| 112 | + // Poll until all resources are found in discovery |
| 113 | + p := &poller{config: config, waitingFor: waitingFor} |
| 114 | + return wait.PollImmediate(options.pollInterval, options.maxTime, p.poll) |
| 115 | +} |
| 116 | + |
| 117 | +// poller checks if all the resources have been found in discovery, and returns false if not |
| 118 | +type poller struct { |
| 119 | + // config is used to get discovery |
| 120 | + config *rest.Config |
| 121 | + |
| 122 | + // waitingFor is the map of resources keyed by group version that have not yet been found in discovery |
| 123 | + waitingFor map[schema.GroupVersion]*sets.String |
| 124 | +} |
| 125 | + |
| 126 | +// poll checks if all the resources have been found in discovery, and returns false if not |
| 127 | +func (p *poller) poll() (done bool, err error) { |
| 128 | + // Create a new clientset to avoid any client caching of discovery |
| 129 | + cs, err := clientset.NewForConfig(p.config) |
| 130 | + if err != nil { |
| 131 | + return false, err |
| 132 | + } |
| 133 | + |
| 134 | + allFound := true |
| 135 | + for gv, resources := range p.waitingFor { |
| 136 | + // All resources found, do nothing |
| 137 | + if resources.Len() == 0 { |
| 138 | + delete(p.waitingFor, gv) |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + // Get the Resources for this GroupVersion |
| 143 | + // TODO: Maybe the controller-runtime client should be able to do this... |
| 144 | + resourceList, err := cs.Discovery().ServerResourcesForGroupVersion(gv.Group + "/" + gv.Version) |
| 145 | + if err != nil { |
| 146 | + return false, nil |
| 147 | + } |
| 148 | + |
| 149 | + // Remove each found resource from the resources set that we are waiting for |
| 150 | + for _, resource := range resourceList.APIResources { |
| 151 | + resources.Delete(resource.Name) |
| 152 | + } |
| 153 | + |
| 154 | + // Still waiting on some resources in this group version |
| 155 | + if resources.Len() != 0 { |
| 156 | + allFound = false |
| 157 | + } |
| 158 | + } |
| 159 | + return allFound, nil |
| 160 | +} |
| 161 | + |
| 162 | +// CreateCRDs creates the CRDs |
| 163 | +func CreateCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResourceDefinition) error { |
| 164 | + cs, err := clientset.NewForConfig(config) |
| 165 | + if err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + // Create each CRD |
| 170 | + for _, crd := range crds { |
| 171 | + if _, err := cs.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + } |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +// readCRDs reads the CRDs from files and Unmarshals them into structs |
| 179 | +func readCRDs(path string) ([]*apiextensionsv1beta1.CustomResourceDefinition, error) { |
| 180 | + // Get the CRD files |
| 181 | + var files []os.FileInfo |
| 182 | + var err error |
| 183 | + if files, err = ioutil.ReadDir(path); err != nil { |
| 184 | + return nil, err |
| 185 | + } |
| 186 | + |
| 187 | + // White list the file extensions that may contain CRDs |
| 188 | + crdExts := sets.NewString(".json", ".yaml", ".yml") |
| 189 | + |
| 190 | + var crds []*apiextensionsv1beta1.CustomResourceDefinition |
| 191 | + for _, file := range files { |
| 192 | + // Only parse whitelisted file types |
| 193 | + if !crdExts.Has(filepath.Ext(file.Name())) { |
| 194 | + continue |
| 195 | + } |
| 196 | + |
| 197 | + // Unmarshal the file into a struct |
| 198 | + b, err := ioutil.ReadFile(filepath.Join(path, file.Name())) |
| 199 | + if err != nil { |
| 200 | + return nil, err |
| 201 | + } |
| 202 | + crd := &apiextensionsv1beta1.CustomResourceDefinition{} |
| 203 | + if err = yaml.Unmarshal(b, crd); err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + |
| 207 | + // Check that it is actually a CRD |
| 208 | + if crd.Spec.Names.Kind == "" || crd.Spec.Group == "" { |
| 209 | + continue |
| 210 | + } |
| 211 | + |
| 212 | + crds = append(crds, crd) |
| 213 | + } |
| 214 | + return crds, nil |
| 215 | +} |
0 commit comments