From 1df4fabeb3af1edb22fd3f6e919fefec470a45ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Thu, 2 Jun 2022 14:14:53 +0200 Subject: [PATCH] fix: allow setting PreserveUnknownFields at both type and field level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/crd/flatten.go | 2 ++ pkg/crd/testdata/cronjob_types.go | 6 +++++- pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml | 8 +++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/crd/flatten.go b/pkg/crd/flatten.go index 00f4dc4c7..8af36440f 100644 --- a/pkg/crd/flatten.go +++ b/pkg/crd/flatten.go @@ -143,6 +143,8 @@ func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, e dstProps.Schema = &apiext.JSONSchemaProps{} } flattenAllOfInto(dstProps.Schema, *srcProps.Schema, errRec) + case "XPreserveUnknownFields": + dstField.Set(srcField) // NB(directxman12): no need to explicitly handle nullable -- false is considered to be the zero value // TODO(directxman12): src isn't necessarily the field value -- it's just the most recent allOf entry default: diff --git a/pkg/crd/testdata/cronjob_types.go b/pkg/crd/testdata/cronjob_types.go index 66222f6ea..86ab623ba 100644 --- a/pkg/crd/testdata/cronjob_types.go +++ b/pkg/crd/testdata/cronjob_types.go @@ -134,9 +134,13 @@ type CronJobSpec struct { // +kubebuilder:validation:nullable UnprunedEmbeddedResource runtime.RawExtension `json:"unprunedEmbeddedResource"` - // This tests that a type-level pruning maker works. + // This tests that a type-level pruning marker works. UnprunedFromType Preserved `json:"unprunedFomType"` + // This tests that a type-level pruning marker combined with a field-level pruning marker works. + // +kubebuilder:pruning:PreserveUnknownFields + UnprunedFromTypeAndField Preserved `json:"unprunedFomTypeAndField"` + // This tests that associative lists work. // +listType=map // +listMapKey=name diff --git a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml index a4b031f47..71e014eaf 100644 --- a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml +++ b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml @@ -7344,7 +7344,12 @@ spec: x-kubernetes-embedded-resource: true x-kubernetes-preserve-unknown-fields: true unprunedFomType: - description: This tests that a type-level pruning maker works. + description: This tests that a type-level pruning marker works. + type: object + x-kubernetes-preserve-unknown-fields: true + unprunedFomTypeAndField: + description: This tests that a type-level pruning marker combined + with a field-level pruning marker works. type: object x-kubernetes-preserve-unknown-fields: true unprunedJSON: @@ -7381,6 +7386,7 @@ spec: - twoOfAKindPart1 - unprunedEmbeddedResource - unprunedFomType + - unprunedFomTypeAndField - unprunedJSON type: object status: