From 5038f80b45a881f9e601d1ee0776e772a5118332 Mon Sep 17 00:00:00 2001 From: Antoine Pelisse Date: Tue, 12 Nov 2019 16:33:20 -0800 Subject: [PATCH] Strip nullable for Server-side apply --- .../pkg/apiserver/customresource_handler.go | 2 +- .../pkg/apiserver/schema/skeleton.go | 14 ++++++++++++++ .../pkg/controller/openapi/builder/builder.go | 6 ++++++ test/integration/apiserver/apply/apply_crd_test.go | 3 ++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go index 8f21e2d9043..7f54d8396d3 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go @@ -1245,7 +1245,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, AllowNonStructural: true}) + s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripDefaults: true, StripValueValidation: true, StripNullable: true, AllowNonStructural: true}) if err != nil { return nil, err } diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/skeleton.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/skeleton.go index ef1fa7808f7..550347adec0 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/skeleton.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/skeleton.go @@ -49,3 +49,17 @@ func (s *Structural) StripValueValidations() *Structural { v.Visit(s) return s } + +// StripNullable returns a copy without nullable. +func (s *Structural) StripNullable() *Structural { + s = s.DeepCopy() + v := Visitor{ + Structural: func(s *Structural) bool { + changed := s.Nullable + s.Nullable = false + return changed + }, + } + v.Visit(s) + return s +} diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/builder.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/builder.go index 8e71458ab6e..af8f0983d27 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/builder.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/builder.go @@ -76,6 +76,9 @@ type Options struct { // Strip value validation. StripValueValidation bool + // Strip nullable. + StripNullable 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 } @@ -101,6 +104,9 @@ func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string, o if opts.StripValueValidation { schema = schema.StripValueValidations() } + if opts.StripNullable { + schema = schema.StripNullable() + } schema = schema.Unfold() } diff --git a/test/integration/apiserver/apply/apply_crd_test.go b/test/integration/apiserver/apply/apply_crd_test.go index 2e1bc1a91bb..2451136f38d 100644 --- a/test/integration/apiserver/apply/apply_crd_test.go +++ b/test/integration/apiserver/apply/apply_crd_test.go @@ -190,7 +190,8 @@ func TestApplyCRDStructuralSchema(t *testing.T) { "type": "string" }, "protocol": { - "type": "string" + "type": "string", + "nullable": true } }, "required": [