Skip to content

Commit 0f64ec9

Browse files
committed
Only publish openapi for structural schemas
1 parent e1857ee commit 0f64ec9

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ func buildOpenAPIModelsForApply(staticOpenAPISpec *spec.Swagger, crd *apiextensi
12361236

12371237
specs := []*spec.Swagger{}
12381238
for _, v := range crd.Spec.Versions {
1239-
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripDefaults: true, StripValueValidation: true})
1239+
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripDefaults: true, StripValueValidation: true, AllowNonStructural: true})
12401240
if err != nil {
12411241
return nil, err
12421242
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ type Options struct {
7575

7676
// Strip value validation.
7777
StripValueValidation bool
78+
79+
// AllowNonStructural indicates swagger should be built for a schema that fits into the structural type but does not meet all structural invariants
80+
AllowNonStructural bool
7881
}
7982

8083
// BuildSwagger builds swagger for the given crd in the given version
@@ -88,17 +91,19 @@ func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string, o
8891
if s != nil && s.OpenAPIV3Schema != nil {
8992
if !validation.SchemaHasInvalidTypes(s.OpenAPIV3Schema) {
9093
if ss, err := structuralschema.NewStructural(s.OpenAPIV3Schema); err == nil {
91-
// skip non-structural schemas
92-
schema = ss
93-
94-
if opts.StripDefaults {
95-
schema = schema.StripDefaults()
94+
// skip non-structural schemas unless explicitly asked to produce swagger from them
95+
if opts.AllowNonStructural || len(structuralschema.ValidateStructural(nil, ss)) == 0 {
96+
schema = ss
97+
98+
if opts.StripDefaults {
99+
schema = schema.StripDefaults()
100+
}
101+
if opts.StripValueValidation {
102+
schema = schema.StripValueValidations()
103+
}
104+
105+
schema = schema.Unfold()
96106
}
97-
if opts.StripValueValidation {
98-
schema = schema.StripValueValidations()
99-
}
100-
101-
schema = schema.Unfold()
102107
}
103108
}
104109
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,13 @@ func TestBuildSwagger(t *testing.T) {
582582
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
583583
Options{V2: true, StripDefaults: true},
584584
},
585+
{
586+
"with non-structural schema",
587+
`{"type":"object","properties":{"foo":{"type":"array"}}}`,
588+
nil,
589+
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
590+
Options{V2: true, StripDefaults: true},
591+
},
585592
{
586593
"with spec.preseveUnknownFields=true",
587594
`{"type":"object","properties":{"foo":{"type":"string"}}}`,

test/integration/master/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ go_test(
7070
"//vendor/github.com/evanphx/json-patch:go_default_library",
7171
"//vendor/github.com/go-openapi/spec:go_default_library",
7272
"//vendor/github.com/stretchr/testify/require:go_default_library",
73+
"//vendor/k8s.io/utils/pointer:go_default_library",
7374
"//vendor/sigs.k8s.io/yaml:go_default_library",
7475
] + select({
7576
"@io_bazel_rules_go//go/platform:android": [

test/integration/master/crd_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
3838
"k8s.io/kubernetes/test/integration/etcd"
3939
"k8s.io/kubernetes/test/integration/framework"
40+
utilpointer "k8s.io/utils/pointer"
4041
)
4142

4243
func TestCRDShadowGroup(t *testing.T) {
@@ -172,6 +173,7 @@ func TestCRDOpenAPI(t *testing.T) {
172173
Plural: "foos",
173174
Kind: "Foo",
174175
},
176+
PreserveUnknownFields: utilpointer.BoolPtr(false),
175177
Validation: &apiextensionsv1beta1.CustomResourceValidation{
176178
OpenAPIV3Schema: &apiextensionsv1beta1.JSONSchemaProps{
177179
Type: "object",

0 commit comments

Comments
 (0)