Skip to content

Commit e1857ee

Browse files
committed
Omit openapi properties if spec.preserveUnknownFields=true
1 parent 5b496d2 commit e1857ee

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ go_test(
5151
"//vendor/github.com/go-openapi/spec:go_default_library",
5252
"//vendor/github.com/stretchr/testify/assert:go_default_library",
5353
"//vendor/github.com/stretchr/testify/require:go_default_library",
54+
"//vendor/k8s.io/utils/pointer:go_default_library",
5455
],
5556
)
5657

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/builder.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,12 @@ func (b *builder) buildRoute(root, path, httpMethod, actionVerb, operationVerb s
334334

335335
// buildKubeNative builds input schema with Kubernetes' native object meta, type meta and
336336
// extensions
337-
func (b *builder) buildKubeNative(schema *structuralschema.Structural, v2 bool) (ret *spec.Schema) {
337+
func (b *builder) buildKubeNative(schema *structuralschema.Structural, v2 bool, crdPreserveUnknownFields bool) (ret *spec.Schema) {
338338
// only add properties if we have a schema. Otherwise, kubectl would (wrongly) assume additionalProperties=false
339339
// and forbid anything outside of apiVersion, kind and metadata. We have to fix kubectl to stop doing this, e.g. by
340340
// adding additionalProperties=true support to explicitly allow additional fields.
341341
// TODO: fix kubectl to understand additionalProperties=true
342-
if schema == nil || (v2 && schema.XPreserveUnknownFields) {
342+
if schema == nil || (v2 && (schema.XPreserveUnknownFields || crdPreserveUnknownFields)) {
343343
ret = &spec.Schema{
344344
SchemaProps: spec.SchemaProps{Type: []string{"object"}},
345345
}
@@ -506,7 +506,8 @@ func newBuilder(crd *apiextensions.CustomResourceDefinition, version string, sch
506506
}
507507

508508
// Pre-build schema with Kubernetes native properties
509-
b.schema = b.buildKubeNative(schema, v2)
509+
preserveUnknownFields := crd.Spec.PreserveUnknownFields != nil && *crd.Spec.PreserveUnknownFields
510+
b.schema = b.buildKubeNative(schema, v2, preserveUnknownFields)
510511
b.listSchema = b.buildListSchema()
511512

512513
return b

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/builder_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/apiserver/pkg/endpoints"
3434
"k8s.io/apiserver/pkg/features"
3535
utilfeature "k8s.io/apiserver/pkg/util/feature"
36+
utilpointer "k8s.io/utils/pointer"
3637
)
3738

3839
func TestNewBuilder(t *testing.T) {
@@ -554,50 +555,65 @@ func schemaDiff(a, b *spec.Schema) string {
554555

555556
func TestBuildSwagger(t *testing.T) {
556557
tests := []struct {
557-
name string
558-
schema string
559-
wantedSchema string
560-
opts Options
558+
name string
559+
schema string
560+
preserveUnknownFields *bool
561+
wantedSchema string
562+
opts Options
561563
}{
562564
{
563565
"nil",
564566
"",
567+
nil,
565568
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
566569
Options{V2: true, StripDefaults: true},
567570
},
568571
{
569572
"with properties",
570573
`{"type":"object","properties":{"spec":{"type":"object"},"status":{"type":"object"}}}`,
574+
nil,
571575
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"spec":{"type":"object"},"status":{"type":"object"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
572576
Options{V2: true, StripDefaults: true},
573577
},
574578
{
575579
"with invalid-typed properties",
576580
`{"type":"object","properties":{"spec":{"type":"bug"},"status":{"type":"object"}}}`,
581+
nil,
582+
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
583+
Options{V2: true, StripDefaults: true},
584+
},
585+
{
586+
"with spec.preseveUnknownFields=true",
587+
`{"type":"object","properties":{"foo":{"type":"string"}}}`,
588+
utilpointer.BoolPtr(true),
577589
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
578590
Options{V2: true, StripDefaults: true},
579591
},
580592
{
581593
"with stripped defaults",
582594
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}`,
595+
nil,
583596
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
584597
Options{V2: true, StripDefaults: true},
585598
},
586599
{
587600
"with stripped defaults",
588601
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}`,
602+
nil,
589603
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
590604
Options{V2: true, StripDefaults: true},
591605
},
592606
{
593607
"v2",
594608
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
609+
nil,
595610
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
596611
Options{V2: true, StripDefaults: true},
597612
},
598613
{
599614
"v3",
600615
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
616+
nil,
601617
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
602618
Options{V2: false, StripDefaults: true},
603619
},
@@ -628,8 +644,9 @@ func TestBuildSwagger(t *testing.T) {
628644
Kind: "Foo",
629645
ListKind: "FooList",
630646
},
631-
Scope: apiextensions.NamespaceScoped,
632-
Validation: validation,
647+
Scope: apiextensions.NamespaceScoped,
648+
Validation: validation,
649+
PreserveUnknownFields: tt.preserveUnknownFields,
633650
},
634651
}, "v1", tt.opts)
635652
if err != nil {

0 commit comments

Comments
 (0)