Skip to content

Commit

Permalink
Remove defaults from crd v1beta1
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha committed May 27, 2020
1 parent 820a4a2 commit da8ccbe
Showing 1 changed file with 60 additions and 0 deletions.
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) {
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

0 comments on commit da8ccbe

Please sign in to comment.