forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apiextensions: switch validation to kube-openapi
- Loading branch information
Showing
11 changed files
with
282 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/kubeopenapi.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
Copyright 2020 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 | ||
|
||
import ( | ||
"k8s.io/kube-openapi/pkg/validation/spec" | ||
) | ||
|
||
// ToKubeOpenAPI converts a structural schema to go-openapi schema. It is faithful and roundtrippable. | ||
func (s *Structural) ToKubeOpenAPI() *spec.Schema { | ||
if s == nil { | ||
return nil | ||
} | ||
|
||
ret := &spec.Schema{} | ||
|
||
if s.Items != nil { | ||
ret.Items = &spec.SchemaOrArray{Schema: s.Items.ToKubeOpenAPI()} | ||
} | ||
if s.Properties != nil { | ||
ret.Properties = make(map[string]spec.Schema, len(s.Properties)) | ||
for k, v := range s.Properties { | ||
ret.Properties[k] = *v.ToKubeOpenAPI() | ||
} | ||
} | ||
s.Generic.toKubeOpenAPI(ret) | ||
s.Extensions.toKubeOpenAPI(ret) | ||
s.ValueValidation.toKubeOpenAPI(ret) | ||
|
||
return ret | ||
} | ||
|
||
func (g *Generic) toKubeOpenAPI(ret *spec.Schema) { | ||
if g == nil { | ||
return | ||
} | ||
|
||
if len(g.Type) != 0 { | ||
ret.Type = spec.StringOrArray{g.Type} | ||
} | ||
ret.Nullable = g.Nullable | ||
if g.AdditionalProperties != nil { | ||
ret.AdditionalProperties = &spec.SchemaOrBool{ | ||
Allows: g.AdditionalProperties.Bool, | ||
Schema: g.AdditionalProperties.Structural.ToKubeOpenAPI(), | ||
} | ||
} | ||
ret.Description = g.Description | ||
ret.Title = g.Title | ||
ret.Default = g.Default.Object | ||
} | ||
|
||
func (x *Extensions) toKubeOpenAPI(ret *spec.Schema) { | ||
if x == nil { | ||
return | ||
} | ||
|
||
if x.XPreserveUnknownFields { | ||
ret.VendorExtensible.AddExtension("x-kubernetes-preserve-unknown-fields", true) | ||
} | ||
if x.XEmbeddedResource { | ||
ret.VendorExtensible.AddExtension("x-kubernetes-embedded-resource", true) | ||
} | ||
if x.XIntOrString { | ||
ret.VendorExtensible.AddExtension("x-kubernetes-int-or-string", true) | ||
} | ||
if len(x.XListMapKeys) > 0 { | ||
ret.VendorExtensible.AddExtension("x-kubernetes-list-map-keys", x.XListMapKeys) | ||
} | ||
if x.XListType != nil { | ||
ret.VendorExtensible.AddExtension("x-kubernetes-list-type", *x.XListType) | ||
} | ||
if x.XMapType != nil { | ||
ret.VendorExtensible.AddExtension("x-kubernetes-map-type", *x.XMapType) | ||
} | ||
} | ||
|
||
func (v *ValueValidation) toKubeOpenAPI(ret *spec.Schema) { | ||
if v == nil { | ||
return | ||
} | ||
|
||
ret.Format = v.Format | ||
ret.Maximum = v.Maximum | ||
ret.ExclusiveMaximum = v.ExclusiveMaximum | ||
ret.Minimum = v.Minimum | ||
ret.ExclusiveMinimum = v.ExclusiveMinimum | ||
ret.MaxLength = v.MaxLength | ||
ret.MinLength = v.MinLength | ||
ret.Pattern = v.Pattern | ||
ret.MaxItems = v.MaxItems | ||
ret.MinItems = v.MinItems | ||
ret.UniqueItems = v.UniqueItems | ||
ret.MultipleOf = v.MultipleOf | ||
if v.Enum != nil { | ||
ret.Enum = make([]interface{}, 0, len(v.Enum)) | ||
for i := range v.Enum { | ||
ret.Enum = append(ret.Enum, v.Enum[i].Object) | ||
} | ||
} | ||
ret.MaxProperties = v.MaxProperties | ||
ret.MinProperties = v.MinProperties | ||
ret.Required = v.Required | ||
for i := range v.AllOf { | ||
ret.AllOf = append(ret.AllOf, *v.AllOf[i].toKubeOpenAPI()) | ||
} | ||
for i := range v.AnyOf { | ||
ret.AnyOf = append(ret.AnyOf, *v.AnyOf[i].toKubeOpenAPI()) | ||
} | ||
for i := range v.OneOf { | ||
ret.OneOf = append(ret.OneOf, *v.OneOf[i].toKubeOpenAPI()) | ||
} | ||
ret.Not = v.Not.toKubeOpenAPI() | ||
} | ||
|
||
func (vv *NestedValueValidation) toKubeOpenAPI() *spec.Schema { | ||
if vv == nil { | ||
return nil | ||
} | ||
|
||
ret := &spec.Schema{} | ||
|
||
vv.ValueValidation.toKubeOpenAPI(ret) | ||
if vv.Items != nil { | ||
ret.Items = &spec.SchemaOrArray{Schema: vv.Items.toKubeOpenAPI()} | ||
} | ||
if vv.Properties != nil { | ||
ret.Properties = make(map[string]spec.Schema, len(vv.Properties)) | ||
for k, v := range vv.Properties { | ||
ret.Properties[k] = *v.toKubeOpenAPI() | ||
} | ||
} | ||
vv.ForbiddenGenerics.toKubeOpenAPI(ret) // normally empty. Exception: int-or-string | ||
vv.ForbiddenExtensions.toKubeOpenAPI(ret) // shouldn't do anything | ||
|
||
return ret | ||
} |
102 changes: 102 additions & 0 deletions
102
staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/kubeopenapi_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
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 | ||
|
||
import ( | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
fuzz "github.com/google/gofuzz" | ||
|
||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/diff" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
) | ||
|
||
func TestStructuralKubeOpenAPIRoundtrip(t *testing.T) { | ||
f := fuzz.New() | ||
seed := time.Now().UnixNano() | ||
t.Logf("seed = %v", seed) | ||
//seed = int64(1549012506261785182) | ||
f.RandSource(rand.New(rand.NewSource(seed))) | ||
f.Funcs( | ||
func(s *JSON, c fuzz.Continue) { | ||
switch c.Intn(7) { | ||
case 0: | ||
s.Object = float64(42.2) | ||
case 1: | ||
s.Object = map[string]interface{}{"foo": "bar"} | ||
case 2: | ||
s.Object = "" | ||
case 3: | ||
s.Object = []interface{}{} | ||
case 4: | ||
s.Object = map[string]interface{}{} | ||
case 5: | ||
s.Object = nil | ||
case 6: | ||
s.Object = int64(42) | ||
} | ||
}, | ||
) | ||
f.MaxDepth(3) | ||
f.NilChance(0.5) | ||
|
||
for i := 0; i < 10000; i++ { | ||
orig := &Structural{} | ||
f.Fuzz(orig) | ||
|
||
// normalize Structural.ValueValidation to zero values if it was nil before | ||
normalizer := Visitor{ | ||
Structural: func(s *Structural) bool { | ||
if s.ValueValidation == nil { | ||
s.ValueValidation = &ValueValidation{} | ||
return true | ||
} | ||
return false | ||
}, | ||
} | ||
normalizer.Visit(orig) | ||
|
||
kubeOpenAPI := orig.ToKubeOpenAPI() | ||
bs, err := json.Marshal(kubeOpenAPI) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
v1beta1Schema := &apiextensionsv1beta1.JSONSchemaProps{} | ||
err = json.Unmarshal(bs, v1beta1Schema) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
internalSchema := &apiextensions.JSONSchemaProps{} | ||
err = apiextensionsv1beta1.Convert_v1beta1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(v1beta1Schema, internalSchema, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
s, err := NewStructural(internalSchema) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(orig, s) { | ||
t.Fatalf("original and result differ: %v", diff.ObjectGoPrintDiff(orig, s)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.