From 72b140ea5ff5d05a8dbd5505b659da2174aa8a4e Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Wed, 14 Oct 2020 13:37:33 -0700 Subject: [PATCH] Test for type-level preserve-unknown-fields This adds a test for type-level `pruning:PreserveUnknownFields` to the crd test. --- pkg/crd/testdata/cronjob_types.go | 35 +++++++++++++++++++ .../testdata.kubebuilder.io_cronjobs.yaml | 10 ++++++ pkg/rbac/zz_generated.markerhelp.go | 4 +-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/pkg/crd/testdata/cronjob_types.go b/pkg/crd/testdata/cronjob_types.go index e066d05c2..a80f46244 100644 --- a/pkg/crd/testdata/cronjob_types.go +++ b/pkg/crd/testdata/cronjob_types.go @@ -23,6 +23,7 @@ limitations under the License. package cronjob import ( + "encoding/json" "fmt" batchv1beta1 "k8s.io/api/batch/v1beta1" @@ -127,6 +128,9 @@ type CronJobSpec struct { // +kubebuilder:validation:nullable UnprunedEmbeddedResource runtime.RawExtension `json:"unprunedEmbeddedResource"` + // This tests that a type-level pruning maker works. + UnprunedFromType Preserved `json:"unprunedFomType"` + // This tests that associative lists work. // +listType=map // +listMapKey=name @@ -142,6 +146,37 @@ type CronJobSpec struct { StructWithSeveralFields NestedObject `json:"structWithSeveralFields"` } +// +kubebuilder:validation:Type=object +// +kubebuilder:pruning:PreserveUnknownFields +type Preserved struct { + ConcreteField string `json:"concreteField"` + Rest map[string]interface{} `json:"-"` +} +func (p *Preserved) UnmarshalJSON(data []byte) error { + if err := json.Unmarshal(data, &p.Rest); err != nil { + return err + } + conc, found := p.Rest["concreteField"] + if !found { + return nil + } + concStr, isStr := conc.(string) + if !isStr { + return fmt.Errorf("concreteField was not string") + } + delete(p.Rest, "concreteField") + p.ConcreteField = concStr + return nil +} +func (p *Preserved) MarshalJSON() ([]byte, error) { + full := make(map[string]interface{}, len(p.Rest)+1) + for k, v := range p.Rest { + full[k] = v + } + full["concreteField"] = p.ConcreteField + return json.Marshal(full) +} + type NestedObject struct { Foo string `json:"foo"` Bar bool `json:"bar"` diff --git a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml index 8bdefa79c..0dfd517d7 100644 --- a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml +++ b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml @@ -5090,6 +5090,15 @@ spec: type: object x-kubernetes-embedded-resource: true x-kubernetes-preserve-unknown-fields: true + unprunedFomType: + description: This tests that a type-level pruning maker works. + properties: + concreteField: + type: string + required: + - concreteField + type: object + x-kubernetes-preserve-unknown-fields: true unprunedJSON: properties: bar: @@ -5117,6 +5126,7 @@ spec: - twoOfAKindPart0 - twoOfAKindPart1 - unprunedEmbeddedResource + - unprunedFomType - unprunedJSON type: object status: diff --git a/pkg/rbac/zz_generated.markerhelp.go b/pkg/rbac/zz_generated.markerhelp.go index 8418dcb1a..707fc042e 100644 --- a/pkg/rbac/zz_generated.markerhelp.go +++ b/pkg/rbac/zz_generated.markerhelp.go @@ -57,8 +57,8 @@ func (Rule) Help() *markers.DefinitionHelp { Details: "", }, "ResourceNames": markers.DetailedHelp{ - Summary: "specifies the names of the API resources that this rule encompasses. Create requests cannot be restricted by resourcename, as the object's name is not known at authorization time.", - Details: "", + Summary: "specifies the names of the API resources that this rule encompasses. ", + Details: "Create requests cannot be restricted by resourcename, as the object's name is not known at authorization time.", }, "Verbs": markers.DetailedHelp{ Summary: "specifies the (lowercase) kubernetes API verbs that this rule encompasses.",