Only publish openapi for structural schemas

This commit is contained in:
Jordan Liggitt 2019-09-12 13:35:49 -04:00
parent e1857ee90f
commit 0f64ec9456
5 changed files with 25 additions and 10 deletions

View File

@ -1236,7 +1236,7 @@ func buildOpenAPIModelsForApply(staticOpenAPISpec *spec.Swagger, crd *apiextensi
specs := []*spec.Swagger{}
for _, v := range crd.Spec.Versions {
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripDefaults: true, StripValueValidation: true})
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripDefaults: true, StripValueValidation: true, AllowNonStructural: true})
if err != nil {
return nil, err
}

View File

@ -75,6 +75,9 @@ type Options struct {
// Strip value validation.
StripValueValidation bool
// AllowNonStructural indicates swagger should be built for a schema that fits into the structural type but does not meet all structural invariants
AllowNonStructural bool
}
// BuildSwagger builds swagger for the given crd in the given version
@ -88,17 +91,19 @@ func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string, o
if s != nil && s.OpenAPIV3Schema != nil {
if !validation.SchemaHasInvalidTypes(s.OpenAPIV3Schema) {
if ss, err := structuralschema.NewStructural(s.OpenAPIV3Schema); err == nil {
// skip non-structural schemas
schema = ss
// skip non-structural schemas unless explicitly asked to produce swagger from them
if opts.AllowNonStructural || len(structuralschema.ValidateStructural(nil, ss)) == 0 {
schema = ss
if opts.StripDefaults {
schema = schema.StripDefaults()
}
if opts.StripValueValidation {
schema = schema.StripValueValidations()
}
if opts.StripDefaults {
schema = schema.StripDefaults()
}
if opts.StripValueValidation {
schema = schema.StripValueValidations()
}
schema = schema.Unfold()
schema = schema.Unfold()
}
}
}
}

View File

@ -582,6 +582,13 @@ func TestBuildSwagger(t *testing.T) {
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with non-structural schema",
`{"type":"object","properties":{"foo":{"type":"array"}}}`,
nil,
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with spec.preseveUnknownFields=true",
`{"type":"object","properties":{"foo":{"type":"string"}}}`,

View File

@ -70,6 +70,7 @@ go_test(
"//vendor/github.com/evanphx/json-patch:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
"//vendor/sigs.k8s.io/yaml:go_default_library",
] + select({
"@io_bazel_rules_go//go/platform:android": [

View File

@ -37,6 +37,7 @@ import (
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
"k8s.io/kubernetes/test/integration/etcd"
"k8s.io/kubernetes/test/integration/framework"
utilpointer "k8s.io/utils/pointer"
)
func TestCRDShadowGroup(t *testing.T) {
@ -172,6 +173,7 @@ func TestCRDOpenAPI(t *testing.T) {
Plural: "foos",
Kind: "Foo",
},
PreserveUnknownFields: utilpointer.BoolPtr(false),
Validation: &apiextensionsv1beta1.CustomResourceValidation{
OpenAPIV3Schema: &apiextensionsv1beta1.JSONSchemaProps{
Type: "object",