Skip to content

Commit

Permalink
Merge pull request #362 from DirectXMan12/bug/preserve-unknown-fields…
Browse files Browse the repository at this point in the history
…-default

🐛 preserveUnknownFields: true on v1beta1, option
  • Loading branch information
k8s-ci-robot committed Nov 19, 2019
2 parents 257814f + 6eb7baf commit 00e4139
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ type Generator struct {
// Only works with the v1beta1 CRD version.
TrivialVersions bool `marker:",optional"`

// PreserveUnknownFields indicates whether or not we should turn off pruning.
//
// Left unspecified, it'll default to true when only a v1beta1 CRD is
// generated (to preserve compatibility with older versions of this tool),
// or false otherwise.
//
// It's required to be false for v1 CRDs.
PreserveUnknownFields *bool `marker:",optional"`

// MaxDescLen specifies the maximum description length for fields in CRD's OpenAPI schema.
//
// 0 indicates drop the description for all fields completely.
Expand Down Expand Up @@ -117,6 +126,20 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
}
}

// *If* we're only generating v1beta1 CRDs, default to `preserveUnknownFields: (unset)`
// for compatibility purposes. In any other case, default to false, since that's
// the sensible default and is required for v1.
v1beta1Only := len(crdVersions) == 1 && crdVersions[0] == "v1beta1"
switch {
case (g.PreserveUnknownFields == nil || *g.PreserveUnknownFields) && v1beta1Only:
crd := versionedCRDs[0].(*apiextlegacy.CustomResourceDefinition)
crd.Spec.PreserveUnknownFields = nil
case g.PreserveUnknownFields == nil, g.PreserveUnknownFields != nil && !*g.PreserveUnknownFields:
// it'll be false here (coming from v1) -- leave it as such
default:
return fmt.Errorf("you may only set PreserveUnknownFields to true with v1beta1 CRDs")
}

for i, crd := range versionedCRDs {
var fileName string
if i == 0 {
Expand Down

0 comments on commit 00e4139

Please sign in to comment.