@@ -39,7 +39,7 @@ import (
39
39
40
40
// CRDInstallOptions are the options for installing CRDs
41
41
type CRDInstallOptions struct {
42
- // Paths is a list of paths to the directories containing CRDs
42
+ // Paths is a list of paths to the directories or files containing CRDs
43
43
Paths []string
44
44
45
45
// CRDs is a list of CRDs to install
@@ -88,16 +88,12 @@ func InstallCRDs(config *rest.Config, options CRDInstallOptions) ([]*apiextensio
88
88
// readCRDFiles reads the directories of CRDs in options.Paths and adds the CRD structs to options.CRDs
89
89
func readCRDFiles (options * CRDInstallOptions ) error {
90
90
if len (options .Paths ) > 0 {
91
- for _ , path := range options .Paths {
92
- if _ , err := os .Stat (path ); ! options .ErrorIfPathMissing && os .IsNotExist (err ) {
93
- continue
94
- }
95
- new , err := readCRDs (path )
96
- if err != nil {
97
- return err
98
- }
99
- options .CRDs = append (options .CRDs , new ... )
91
+ crdList , err := renderCRDs (options )
92
+ if err != nil {
93
+ return err
100
94
}
95
+
96
+ options .CRDs = append (options .CRDs , crdList ... )
101
97
}
102
98
return nil
103
99
}
@@ -232,28 +228,67 @@ func CreateCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResource
232
228
return nil
233
229
}
234
230
235
- // readCRDs reads the CRDs from files and Unmarshals them into structs
236
- func readCRDs (path string ) ([]* apiextensionsv1beta1.CustomResourceDefinition , error ) {
237
- // Get the CRD files
238
- var files []os.FileInfo
239
- var err error
240
- log .V (1 ).Info ("reading CRDs from path" , "path" , path )
241
- if files , err = ioutil .ReadDir (path ); err != nil {
242
- return nil , err
231
+ // renderCRDs iterate through options.Paths and extract all CRD files.
232
+ func renderCRDs (options * CRDInstallOptions ) ([]* apiextensionsv1beta1.CustomResourceDefinition , error ) {
233
+ var (
234
+ err error
235
+ info os.FileInfo
236
+ crds []* apiextensionsv1beta1.CustomResourceDefinition
237
+ files []os.FileInfo
238
+ )
239
+
240
+ for _ , path := range options .Paths {
241
+ var filePath = path
242
+
243
+ // Return the error if ErrorIfPathMissing exists
244
+ if info , err = os .Stat (path ); os .IsNotExist (err ) {
245
+ if options .ErrorIfPathMissing {
246
+ return nil , err
247
+ }
248
+ continue
249
+ }
250
+
251
+ if ! info .IsDir () {
252
+ filePath , files = filepath .Dir (path ), append (files , info )
253
+ } else {
254
+ if files , err = ioutil .ReadDir (path ); err != nil {
255
+ return nil , err
256
+ }
257
+ }
258
+
259
+ log .V (1 ).Info ("reading CRDs from path" , "path" , path )
260
+
261
+ crdList , err := readCRDs (filePath , files )
262
+ if err != nil {
263
+ return nil , err
264
+ }
265
+
266
+ // If CRD already in the list, skip it.
267
+ if existsCRDs (crds , crdList ) {
268
+ continue
269
+ }
270
+
271
+ crds = append (crds , crdList ... )
243
272
}
244
273
274
+ return crds , nil
275
+ }
276
+
277
+ // readCRDs reads the CRDs from files and Unmarshals them into structs
278
+ func readCRDs (basePath string , files []os.FileInfo ) ([]* apiextensionsv1beta1.CustomResourceDefinition , error ) {
279
+ var crds []* apiextensionsv1beta1.CustomResourceDefinition
280
+
245
281
// White list the file extensions that may contain CRDs
246
282
crdExts := sets .NewString (".json" , ".yaml" , ".yml" )
247
283
248
- var crds []* apiextensionsv1beta1.CustomResourceDefinition
249
284
for _ , file := range files {
250
285
// Only parse whitelisted file types
251
286
if ! crdExts .Has (filepath .Ext (file .Name ())) {
252
287
continue
253
288
}
254
289
255
290
// Unmarshal CRDs from file into structs
256
- docs , err := readDocuments (filepath .Join (path , file .Name ()))
291
+ docs , err := readDocuments (filepath .Join (basePath , file .Name ()))
257
292
if err != nil {
258
293
return nil , err
259
294
}
0 commit comments