Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Remove defaults from crd v1beta1 #446

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
if err != nil {
return err
}
if ver == "v1beta1" {
obj := conv.(*apiextlegacy.CustomResourceDefinition)
if obj.Spec.Validation != nil &&
obj.Spec.Validation.OpenAPIV3Schema != nil {
removeDefaults(obj.Spec.Validation.OpenAPIV3Schema)
}
}
versionedCRDs[i] = conv
}

Expand Down Expand Up @@ -182,6 +189,59 @@ func toTrivialVersions(crd *apiextlegacy.CustomResourceDefinition) {
crd.Spec.AdditionalPrinterColumns = canonicalColumns
}

// removeDefaults removes defaults from apiextensions.k8s.io/v1beta1 CRD definition.
// To use defaulting, CustomResourceDefinitions must use API version apiextensions.k8s.io/v1
func removeDefaults(schema *apiextlegacy.JSONSchemaProps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've got a schema visitor for this purpose, so you shouldn't have to manually write a schema visitor.

if schema == nil {
return
}

schema.Default = nil

if schema.Items != nil {
removeDefaults(schema.Items.Schema)

for idx := range schema.Items.JSONSchemas {
removeDefaults(&schema.Items.JSONSchemas[idx])
}
}

for idx := range schema.AllOf {
removeDefaults(&schema.AllOf[idx])
}
for idx := range schema.OneOf {
removeDefaults(&schema.OneOf[idx])
}
for idx := range schema.AnyOf {
removeDefaults(&schema.AnyOf[idx])
}
if schema.Not != nil {
removeDefaults(schema.Not)
}
for key, prop := range schema.Properties {
removeDefaults(&prop)
schema.Properties[key] = prop
}
if schema.AdditionalProperties != nil {
removeDefaults(schema.AdditionalProperties.Schema)
}
for key, prop := range schema.PatternProperties {
removeDefaults(&prop)
schema.PatternProperties[key] = prop
}
for key, prop := range schema.Dependencies {
removeDefaults(prop.Schema)
schema.Dependencies[key] = prop
}
if schema.AdditionalItems != nil {
removeDefaults(schema.AdditionalItems.Schema)
}
for key, prop := range schema.Definitions {
removeDefaults(&prop)
schema.Definitions[key] = prop
}
}

// addAttribution adds attribution info to indicate controller-gen tool was used
// to generate this CRD definition along with the version info.
func addAttribution(crd *apiext.CustomResourceDefinition) {
Expand Down