From 2afad7cc76a948e920a1bcae6a42bc488848ac11 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Sun, 3 Apr 2022 21:43:46 -0400 Subject: [PATCH 1/3] Expand unit tests of pruning of unknown fields in metadata --- .../schema/pruning/algorithm_test.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go index c7505478f5e..9f529f6924a 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go @@ -383,6 +383,8 @@ func TestPrune(t *testing.T) { "kind": "Foo", "metadata": { "name": "instance", + "namespace": "myns", + "labels":{"foo":"bar"}, "unspecified": "bar" }, "unspecified":"bar", @@ -392,6 +394,8 @@ func TestPrune(t *testing.T) { "unspecified": "bar", "metadata": { "name": "instance", + "namespace": "myns", + "labels":{"foo":"bar"}, "unspecified": "bar" }, "spec": { @@ -404,6 +408,8 @@ func TestPrune(t *testing.T) { "unspecified": "bar", "metadata": { "name": "instance", + "namespace": "myns", + "labels":{"foo":"bar"}, "unspecified": "bar" }, "spec": { @@ -416,6 +422,8 @@ func TestPrune(t *testing.T) { "unspecified": "bar", "metadata": { "name": "instance", + "namespace": "myns", + "labels":{"foo":"bar"}, "unspecified": "bar" }, "spec": { @@ -426,6 +434,8 @@ func TestPrune(t *testing.T) { "unspecified": "bar", "metadata": { "name": "instance", + "namespace": "myns", + "labels":{"foo":"bar"}, "unspecified": "bar" }, "spec": { @@ -438,12 +448,18 @@ func TestPrune(t *testing.T) { `, isResourceRoot: true, schema: &structuralschema.Structural{ Generic: structuralschema.Generic{Type: "object"}, Properties: map[string]structuralschema.Structural{ + "metadata": { + Generic: structuralschema.Generic{Type: "object"}, + }, "pruned": { Generic: structuralschema.Generic{Type: "object"}, Extensions: structuralschema.Extensions{ XEmbeddedResource: true, }, Properties: map[string]structuralschema.Structural{ + "metadata": { + Generic: structuralschema.Generic{Type: "object"}, + }, "spec": { Generic: structuralschema.Generic{Type: "object"}, }, @@ -471,6 +487,9 @@ func TestPrune(t *testing.T) { XEmbeddedResource: true, }, Properties: map[string]structuralschema.Structural{ + "metadata": { + Generic: structuralschema.Generic{Type: "object"}, + }, "spec": { Generic: structuralschema.Generic{Type: "object"}, }, @@ -487,6 +506,8 @@ func TestPrune(t *testing.T) { "kind": "Foo", "metadata": { "name": "instance", + "namespace": "myns", + "labels": {"foo": "bar"}, "unspecified": "bar" }, "pruned": { @@ -494,6 +515,8 @@ func TestPrune(t *testing.T) { "kind": "Foo", "metadata": { "name": "instance", + "namespace": "myns", + "labels": {"foo": "bar"}, "unspecified": "bar" }, "spec": { @@ -505,6 +528,8 @@ func TestPrune(t *testing.T) { "unspecified": "bar", "metadata": { "name": "instance", + "namespace": "myns", + "labels": {"foo": "bar"}, "unspecified": "bar" }, "spec": { @@ -516,6 +541,8 @@ func TestPrune(t *testing.T) { "kind": "Foo", "metadata": { "name": "instance", + "namespace": "myns", + "labels": {"foo": "bar"}, "unspecified": "bar" }, "spec": { @@ -524,6 +551,8 @@ func TestPrune(t *testing.T) { "kind": "Foo", "metadata": { "name": "instance", + "namespace": "myns", + "labels": {"foo": "bar"}, "unspecified": "bar" }, "spec": { From 7de6100dae55dcd33f4c2d0f14f455c9bec17ec1 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Sun, 3 Apr 2022 22:15:45 -0400 Subject: [PATCH 2/3] Expand cmd tests of modifying schema-declaring custom resources --- test/cmd/crd.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/cmd/crd.sh b/test/cmd/crd.sh index 1a1c990988e..c4c6e193652 100755 --- a/test/cmd/crd.sh +++ b/test/cmd/crd.sh @@ -45,8 +45,22 @@ run_crd_tests() { "storage": true, "schema": { "openAPIV3Schema": { - "x-kubernetes-preserve-unknown-fields": true, - "type": "object" + "type": "object", + "properties": { + "metadata": {"type": "object"}, + "nestedField": { + "type": "object", + "properties": { + "someSubfield": {"type": "string"}, + "otherSubfield": {"type": "string"}, + "newSubfield": {"type": "string"} + } + }, + "otherField": {"type": "string"}, + "someField": {"type": "string"}, + "newField": {"type": "string"}, + "patched": {"type": "string"} + } } } } From 869d0b81e19de4e43f1897bb298615c24def6294 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Sun, 3 Apr 2022 22:20:01 -0400 Subject: [PATCH 3/3] Fix bug treating metadata fields as unknown fields --- .../pkg/apiserver/customresource_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go index af036c82be8..701ccde4275 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go @@ -1332,7 +1332,7 @@ func (v *unstructuredSchemaCoercer) apply(u *unstructured.Unstructured) (unknown if v.returnUnknownFieldPaths { pruneOpts.ReturnPruned = true } - unknownFieldPaths = structuralpruning.PruneWithOptions(u.Object, v.structuralSchemas[gv.Version], false, pruneOpts) + unknownFieldPaths = structuralpruning.PruneWithOptions(u.Object, v.structuralSchemas[gv.Version], true, pruneOpts) structuraldefaulting.PruneNonNullableNullsWithoutDefaults(u.Object, v.structuralSchemas[gv.Version]) }