mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
Merge pull request #52281 from nikhita/crd-validation-conversion-fix
Automatic merge from submit-queue (batch tested with PRs 51869, 52281). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.. apiextensions: fix conversion of CRD schema - [x] Fix conversion of CRD schema to go-openapi types. - [x] Add roundtrip tests for this conversion: https://github.com/kubernetes/kubernetes/pull/52793. Split into another PR since it touches godeps. **Release note**: ```release-note NONE ``` /cc @sttts
This commit is contained in:
commit
a514443091
@ -50,7 +50,7 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
|
|||||||
for i := 0; i < tobj.NumField(); i++ {
|
for i := 0; i < tobj.NumField(); i++ {
|
||||||
field := tobj.Field(i)
|
field := tobj.Field(i)
|
||||||
switch field.Name {
|
switch field.Name {
|
||||||
case "Default", "Enum", "Example":
|
case "Default", "Enum", "Example", "Ref":
|
||||||
continue
|
continue
|
||||||
default:
|
default:
|
||||||
isValue := true
|
isValue := true
|
||||||
@ -74,6 +74,10 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
|
|||||||
validJSON := apiextensions.JSON(`"foobarbaz"`)
|
validJSON := apiextensions.JSON(`"foobarbaz"`)
|
||||||
obj.Example = &validJSON
|
obj.Example = &validJSON
|
||||||
}
|
}
|
||||||
|
if c.RandBool() {
|
||||||
|
validRef := "validRef"
|
||||||
|
obj.Ref = &validRef
|
||||||
|
}
|
||||||
},
|
},
|
||||||
func(obj *apiextensions.JSONSchemaPropsOrBool, c fuzz.Continue) {
|
func(obj *apiextensions.JSONSchemaPropsOrBool, c fuzz.Continue) {
|
||||||
if c.RandBool() {
|
if c.RandBool() {
|
||||||
|
@ -40,7 +40,6 @@ func ConvertToOpenAPITypes(in *apiextensions.CustomResourceDefinition, out *spec
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,9 +83,6 @@ func convertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
|
|||||||
out.Enum[k] = v
|
out.Enum[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := convertJSONSchemaPropsOrArray(in.Items, out.Items); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf); err != nil {
|
if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -96,8 +92,13 @@ func convertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
|
|||||||
if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf); err != nil {
|
if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := convertJSONSchemaProps(in.Not, out.Not); err != nil {
|
|
||||||
return err
|
if in.Not != nil {
|
||||||
|
in, out := &in.Not, &out.Not
|
||||||
|
*out = new(spec.Schema)
|
||||||
|
if err := convertJSONSchemaProps(*in, *out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@ -111,6 +112,11 @@ func convertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if in.Ref != nil {
|
if in.Ref != nil {
|
||||||
out.Ref, err = spec.NewRef(*in.Ref)
|
out.Ref, err = spec.NewRef(*in.Ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -118,21 +124,40 @@ func convertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := convertJSONSchemaPropsorBool(in.AdditionalProperties, out.AdditionalProperties); err != nil {
|
if in.AdditionalProperties != nil {
|
||||||
return err
|
in, out := &in.AdditionalProperties, &out.AdditionalProperties
|
||||||
|
*out = new(spec.SchemaOrBool)
|
||||||
|
if err := convertJSONSchemaPropsorBool(*in, *out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := convertJSONSchemaPropsorBool(in.AdditionalItems, out.AdditionalItems); err != nil {
|
if in.AdditionalItems != nil {
|
||||||
return err
|
in, out := &in.AdditionalItems, &out.AdditionalItems
|
||||||
|
*out = new(spec.SchemaOrBool)
|
||||||
|
if err := convertJSONSchemaPropsorBool(*in, *out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := convertJSONSchemaDependencies(in.Dependencies, out.Dependencies); err != nil {
|
if in.Items != nil {
|
||||||
return err
|
in, out := &in.Items, &out.Items
|
||||||
|
*out = new(spec.SchemaOrArray)
|
||||||
|
if err := convertJSONSchemaPropsOrArray(*in, *out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions)
|
if in.Dependencies != nil {
|
||||||
if err != nil {
|
in, out := &in.Dependencies, &out.Dependencies
|
||||||
return err
|
*out = make(spec.Dependencies, len(*in))
|
||||||
|
for key, val := range *in {
|
||||||
|
newVal := new(spec.SchemaOrStringArray)
|
||||||
|
if err := convertJSONSchemaPropsOrStringArray(&val, newVal); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
(*out)[key] = *newVal
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if in.ExternalDocs != nil {
|
if in.ExternalDocs != nil {
|
||||||
@ -172,21 +197,31 @@ func convertMapOfJSONSchemaProps(in map[string]apiextensions.JSONSchemaProps) (m
|
|||||||
}
|
}
|
||||||
|
|
||||||
func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray) error {
|
func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray) error {
|
||||||
if in != nil {
|
if in.Schema != nil {
|
||||||
out.Schema = &spec.Schema{}
|
in, out := &in.Schema, &out.Schema
|
||||||
if err := convertJSONSchemaProps(in.Schema, out.Schema); err != nil {
|
*out = new(spec.Schema)
|
||||||
|
if err := convertJSONSchemaProps(*in, *out); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if in.JSONSchemas != nil {
|
||||||
|
in, out := &in.JSONSchemas, &out.Schemas
|
||||||
|
*out = make([]spec.Schema, len(*in))
|
||||||
|
for i := range *in {
|
||||||
|
if err := convertJSONSchemaProps(&(*in)[i], &(*out)[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool) error {
|
func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool) error {
|
||||||
if in != nil {
|
out.Allows = in.Allows
|
||||||
out = &spec.SchemaOrBool{}
|
if in.Schema != nil {
|
||||||
out.Allows = in.Allows
|
in, out := &in.Schema, &out.Schema
|
||||||
out.Schema = &spec.Schema{}
|
*out = new(spec.Schema)
|
||||||
if err := convertJSONSchemaProps(in.Schema, out.Schema); err != nil {
|
if err := convertJSONSchemaProps(*in, *out); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -194,25 +229,13 @@ func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *
|
|||||||
}
|
}
|
||||||
|
|
||||||
func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray) error {
|
func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray) error {
|
||||||
if in != nil {
|
out.Property = in.Property
|
||||||
out.Property = in.Property
|
if in.Schema != nil {
|
||||||
out.Schema = &spec.Schema{}
|
in, out := &in.Schema, &out.Schema
|
||||||
if err := convertJSONSchemaProps(in.Schema, out.Schema); err != nil {
|
*out = new(spec.Schema)
|
||||||
|
if err := convertJSONSchemaProps(*in, *out); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertJSONSchemaDependencies(in apiextensions.JSONSchemaDependencies, out spec.Dependencies) error {
|
|
||||||
if in != nil {
|
|
||||||
for k, v := range in {
|
|
||||||
schemaOrArray := spec.SchemaOrStringArray{}
|
|
||||||
if err := convertJSONSchemaPropsOrStringArray(&v, &schemaOrArray); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
out[k] = schemaOrArray
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user