apiextensions: builder for OpenAPI v3

This commit is contained in:
Dr. Stefan Schimanski 2019-08-15 14:32:01 +02:00
parent e710fb7cdf
commit c0594d2e38
6 changed files with 110 additions and 6 deletions

View File

@ -6,6 +6,7 @@ go_library(
"complete.go",
"convert.go",
"goopenapi.go",
"skeleton.go",
"structural.go",
"unfold.go",
"validation.go",

View File

@ -0,0 +1,51 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package schema
// StripDefaults returns a copy without defaults.
func (s *Structural) StripDefaults() *Structural {
s = s.DeepCopy()
v := Visitor{
Structural: func(s *Structural) bool {
changed := false
if s.Default.Object != nil {
s.Default.Object = nil
changed = true
}
return changed
},
}
v.Visit(s)
return s
}
// StripValueValidations returns a copy without value validations.
func (s *Structural) StripValueValidations() *Structural {
s = s.DeepCopy()
v := Visitor{
Structural: func(s *Structural) bool {
changed := false
if s.ValueValidation != nil {
s.ValueValidation = nil
changed = true
}
return changed
},
}
v.Visit(s)
return s
}

View File

@ -65,8 +65,20 @@ var definitions map[string]common.OpenAPIDefinition
var buildDefinitions sync.Once
var namer *openapi.DefinitionNamer
// Options contains builder options.
type Options struct {
// Convert to OpenAPI v2.
V2 bool
// Strip defaults.
StripDefaults bool
// Strip value validation.
StripValueValidation bool
}
// BuildSwagger builds swagger for the given crd in the given version
func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string) (*spec.Swagger, error) {
func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string, opts Options) (*spec.Swagger, error) {
var schema *structuralschema.Structural
s, err := apiextensions.GetSchemaForVersion(crd, version)
if err != nil {
@ -76,7 +88,17 @@ func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string) (
if s != nil && s.OpenAPIV3Schema != nil {
if !validation.SchemaHasInvalidTypes(s.OpenAPIV3Schema) {
if ss, err := structuralschema.NewStructural(s.OpenAPIV3Schema); err == nil {
schema = ss.Unfold()
// skip non-structural schemas
schema = ss
if opts.StripDefaults {
schema = schema.StripDefaults()
}
if opts.StripValueValidation {
schema = schema.StripValueValidations()
}
schema = schema.Unfold()
}
}
}
@ -85,7 +107,7 @@ func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string) (
// comes from function registerResourceHandlers() in k8s.io/apiserver.
// Alternatives are either (ideally) refactoring registerResourceHandlers() to
// reuse the code, or faking an APIInstaller for CR to feed to registerResourceHandlers().
b := newBuilder(crd, version, schema, true)
b := newBuilder(crd, version, schema, opts.V2)
// Sample response types for building web service
sample := &CRDCanonicalTypeNamer{

View File

@ -485,7 +485,7 @@ func TestCRDRouteParameterBuilder(t *testing.T) {
},
},
}
swagger, err := BuildSwagger(testNamespacedCRD, testCRDVersion)
swagger, err := BuildSwagger(testNamespacedCRD, testCRDVersion, Options{V2: true, StripDefaults: true})
require.NoError(t, err)
require.Equal(t, len(testCase.paths), len(swagger.Paths.Paths), testCase.scope)
for path, expected := range testCase.paths {
@ -557,21 +557,49 @@ func TestBuildSwagger(t *testing.T) {
name string
schema string
wantedSchema string
opts Options
}{
{
"nil",
"",
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with properties",
`{"type":"object","properties":{"spec":{"type":"object"},"status":{"type":"object"}}}`,
`{"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"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with invalid-typed properties",
`{"type":"object","properties":{"spec":{"type":"bug"},"status":{"type":"object"}}}`,
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with stripped defaults",
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}`,
`{"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"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with stripped defaults",
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}`,
`{"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"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"v2",
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
`{"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"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"v3",
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
`{"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"}]}`,
Options{V2: false, StripDefaults: true},
},
}
for _, tt := range tests {
@ -603,7 +631,7 @@ func TestBuildSwagger(t *testing.T) {
Scope: apiextensions.NamespaceScoped,
Validation: validation,
},
}, "v1")
}, "v1", tt.opts)
if err != nil {
t.Fatal(err)
}

View File

@ -193,7 +193,7 @@ func buildVersionSpecs(crd *apiextensions.CustomResourceDefinition, oldSpecs map
if !v.Served {
continue
}
spec, err := builder.BuildSwagger(crd, v.Name)
spec, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: true, StripDefaults: true})
if err != nil {
return nil, false, err
}

2
vendor/modules.txt vendored
View File

@ -1163,6 +1163,8 @@ k8s.io/apiextensions-apiserver/pkg/controller/establish
k8s.io/apiextensions-apiserver/pkg/controller/finalizer
k8s.io/apiextensions-apiserver/pkg/controller/nonstructuralschema
k8s.io/apiextensions-apiserver/pkg/controller/openapi
k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder
k8s.io/apiextensions-apiserver/pkg/controller/openapi/v2
k8s.io/apiextensions-apiserver/pkg/controller/status
k8s.io/apiextensions-apiserver/pkg/crdserverscheme
k8s.io/apiextensions-apiserver/pkg/features