Skip to content

Commit 4f077b9

Browse files
authored
Merge pull request #481 from munnerz/remove-defaults-v1beta1
✨ Remove 'default' fields from v1beta1 CRDs
2 parents 9a9db70 + d9ff927 commit 4f077b9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pkg/crd/gen.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
156156
}
157157

158158
for i, crd := range versionedCRDs {
159+
// defaults are not allowed to be specified in v1beta1 CRDs, so strip them
160+
// before writing to a file
161+
if crdVersions[i] == "v1beta1" {
162+
removeDefaultsFromSchemas(crd.(*apiextlegacy.CustomResourceDefinition))
163+
}
159164
var fileName string
160165
if i == 0 {
161166
fileName = fmt.Sprintf("%s_%s.yaml", crdRaw.Spec.Group, crdRaw.Spec.Names.Plural)
@@ -171,6 +176,43 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
171176
return nil
172177
}
173178

179+
// removeDefaultsFromSchemas will remove all instances of default values being
180+
// specified across all defined API versions
181+
func removeDefaultsFromSchemas(crd *apiextlegacy.CustomResourceDefinition) {
182+
if crd.Spec.Validation != nil {
183+
removeDefaultsFromSchemaProps(crd.Spec.Validation.OpenAPIV3Schema)
184+
}
185+
186+
for _, versionSpec := range crd.Spec.Versions {
187+
if versionSpec.Schema != nil {
188+
removeDefaultsFromSchemaProps(versionSpec.Schema.OpenAPIV3Schema)
189+
}
190+
}
191+
}
192+
193+
// removeDefaultsFromSchemaProps will recurse into JSONSchemaProps to remove
194+
// all instances of default values being specified
195+
func removeDefaultsFromSchemaProps(v *apiextlegacy.JSONSchemaProps) {
196+
if v == nil {
197+
return
198+
}
199+
200+
// nil-out the default field
201+
v.Default = nil
202+
for name, prop := range v.Properties {
203+
removeDefaultsFromSchemaProps(&prop)
204+
v.Properties[name] = prop
205+
}
206+
if v.Items != nil {
207+
removeDefaultsFromSchemaProps(v.Items.Schema)
208+
for i := range v.Items.JSONSchemas {
209+
props := v.Items.JSONSchemas[i]
210+
removeDefaultsFromSchemaProps(&props)
211+
v.Items.JSONSchemas[i] = props
212+
}
213+
}
214+
}
215+
174216
// toTrivialVersions strips out all schemata except for the storage schema,
175217
// and moves that up into the root object. This makes the CRD compatible
176218
// with pre 1.13 clusters.

0 commit comments

Comments
 (0)