Skip to content

Commit

Permalink
Merge pull request #109268 from liggitt/pruning-metadata
Browse files Browse the repository at this point in the history
ServerSideFieldValidation: Fix bug treating metadata fields as unknown fields
  • Loading branch information
k8s-ci-robot committed May 4, 2022
2 parents 4dacd51 + 869d0b8 commit 6b988c5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ func TestPrune(t *testing.T) {
"kind": "Foo",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels":{"foo":"bar"},
"unspecified": "bar"
},
"unspecified":"bar",
Expand All @@ -392,6 +394,8 @@ func TestPrune(t *testing.T) {
"unspecified": "bar",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels":{"foo":"bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -404,6 +408,8 @@ func TestPrune(t *testing.T) {
"unspecified": "bar",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels":{"foo":"bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -416,6 +422,8 @@ func TestPrune(t *testing.T) {
"unspecified": "bar",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels":{"foo":"bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -426,6 +434,8 @@ func TestPrune(t *testing.T) {
"unspecified": "bar",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels":{"foo":"bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -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"},
},
Expand Down Expand Up @@ -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"},
},
Expand All @@ -487,13 +506,17 @@ func TestPrune(t *testing.T) {
"kind": "Foo",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels": {"foo": "bar"},
"unspecified": "bar"
},
"pruned": {
"apiVersion": "foo/v1",
"kind": "Foo",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels": {"foo": "bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -505,6 +528,8 @@ func TestPrune(t *testing.T) {
"unspecified": "bar",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels": {"foo": "bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -516,6 +541,8 @@ func TestPrune(t *testing.T) {
"kind": "Foo",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels": {"foo": "bar"},
"unspecified": "bar"
},
"spec": {
Expand All @@ -524,6 +551,8 @@ func TestPrune(t *testing.T) {
"kind": "Foo",
"metadata": {
"name": "instance",
"namespace": "myns",
"labels": {"foo": "bar"},
"unspecified": "bar"
},
"spec": {
Expand Down
18 changes: 16 additions & 2 deletions test/cmd/crd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
}
}
}
}
Expand Down

0 comments on commit 6b988c5

Please sign in to comment.