@@ -59,6 +59,7 @@ type options struct {
5959 inputDir string
6060 outputDir string
6161 ignoreExportSchemas []string
62+ preserveResources bool
6263
6364 ignoreExportSchemasParsed []metav1.GroupResource
6465}
@@ -68,6 +69,7 @@ func bindOptions(fs *pflag.FlagSet) *options {
6869 fs .StringVar (& o .inputDir , "input-dir" , "" , "Directory containing CustomResourceDefinition YAML files." )
6970 fs .StringVar (& o .outputDir , "output-dir" , "" , "Directory where APIResourceSchemas and APIExports will be written." )
7071 fs .StringSliceVar (& o .ignoreExportSchemas , "ignore-export-schemas" , []string {}, "Comma-separated list of 'Resource.Group' to be ignored for APIExports generation." )
72+ fs .BoolVar (& o .preserveResources , "preserve-resources" , false , "Preserve existing resources in APIExport manifests instead of overriding them." )
7173 return & o
7274}
7375
@@ -169,7 +171,7 @@ func main() {
169171 }
170172
171173 apiResourceSchemas := resolveLatestAPIResourceSchemas (logger , previousApiResourceSchemas , currentApiResourceSchemas )
172- apiExports , err := generateExports (opts .outputDir , opts .ignoreExportSchemasParsed , apiResourceSchemas )
174+ apiExports , err := generateExports (opts .outputDir , opts .ignoreExportSchemasParsed , opts . preserveResources , apiResourceSchemas )
173175 if err != nil {
174176 logger .Error (err , "Could not generate APIExports." )
175177 os .Exit (1 )
@@ -346,7 +348,7 @@ func compareSchemas() cmp.Option {
346348 }))
347349}
348350
349- func generateExports (outputDir string , ignoreExportSchemas []metav1.GroupResource , allSchemas map [metav1.GroupResource ]* apisv1alpha1.APIResourceSchema ) ([]* apisv1alpha2.APIExport , error ) {
351+ func generateExports (outputDir string , ignoreExportSchemas []metav1.GroupResource , preserveResources bool , allSchemas map [metav1.GroupResource ]* apisv1alpha1.APIResourceSchema ) ([]* apisv1alpha2.APIExport , error ) {
350352 type grs struct {
351353 group string
352354 resource string
@@ -405,16 +407,48 @@ func generateExports(outputDir string, ignoreExportSchemas []metav1.GroupResourc
405407 }
406408 }
407409
408- export .Spec .Resources = []apisv1alpha2.ResourceSchema {}
409- for _ , schema := range grss {
410- export .Spec .Resources = append (export .Spec .Resources , apisv1alpha2.ResourceSchema {
411- Group : schema .group ,
412- Name : schema .resource ,
413- Schema : schema .schema ,
414- Storage : apisv1alpha2.ResourceSchemaStorage {
415- CRD : & apisv1alpha2.ResourceSchemaStorageCRD {},
416- },
417- })
410+ if ! preserveResources {
411+ export .Spec .Resources = []apisv1alpha2.ResourceSchema {}
412+ }
413+
414+ if preserveResources {
415+ existingResourceMap := make (map [string ]bool )
416+ for _ , resource := range export .Spec .Resources {
417+ key := fmt .Sprintf ("%s/%s" , resource .Group , resource .Name )
418+ existingResourceMap [key ] = true
419+ }
420+
421+ for _ , schema := range grss {
422+ key := fmt .Sprintf ("%s/%s" , schema .group , schema .resource )
423+ if ! existingResourceMap [key ] {
424+ export .Spec .Resources = append (export .Spec .Resources , apisv1alpha2.ResourceSchema {
425+ Group : schema .group ,
426+ Name : schema .resource ,
427+ Schema : schema .schema ,
428+ Storage : apisv1alpha2.ResourceSchemaStorage {
429+ CRD : & apisv1alpha2.ResourceSchemaStorageCRD {},
430+ },
431+ })
432+ } else {
433+ for i , resource := range export .Spec .Resources {
434+ if resource .Group == schema .group && resource .Name == schema .resource {
435+ export .Spec .Resources [i ].Schema = schema .schema
436+ break
437+ }
438+ }
439+ }
440+ }
441+ } else {
442+ for _ , schema := range grss {
443+ export .Spec .Resources = append (export .Spec .Resources , apisv1alpha2.ResourceSchema {
444+ Group : schema .group ,
445+ Name : schema .resource ,
446+ Schema : schema .schema ,
447+ Storage : apisv1alpha2.ResourceSchemaStorage {
448+ CRD : & apisv1alpha2.ResourceSchemaStorageCRD {},
449+ },
450+ })
451+ }
418452 }
419453
420454 exports = append (exports , & export )
0 commit comments