mirror of https://github.com/k3s-io/k3s
Revert "Merge pull request #71137 from sttts/sttts-crd-openapi-spec-recursive-v2-prune"
This reverts commitpull/58/head3ea3cfc3be
, reversing changes made tofab7009997
.
parent
3ea3cfc3be
commit
ad2b916d7c
|
@ -36,6 +36,7 @@ import (
|
|||
"k8s.io/client-go/util/workqueue"
|
||||
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||
informers "k8s.io/apiextensions-apiserver/pkg/client/informers/internalversion/apiextensions/internalversion"
|
||||
listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/internalversion"
|
||||
apiextensionsfeatures "k8s.io/apiextensions-apiserver/pkg/features"
|
||||
|
@ -130,10 +131,18 @@ func (c *DiscoveryController) sync(version schema.GroupVersion) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Convert internal CustomResourceValidation to versioned CustomResourceValidation
|
||||
versionedSchema := new(v1beta1.CustomResourceValidation)
|
||||
if validationSchema == nil {
|
||||
versionedSchema = nil
|
||||
} else {
|
||||
if err := v1beta1.Convert_apiextensions_CustomResourceValidation_To_v1beta1_CustomResourceValidation(validationSchema, versionedSchema, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// We aggregate the schema even if it's nil as it maybe a removal of the schema for this CRD,
|
||||
// and the aggreated OpenAPI spec should reflect this change.
|
||||
crdspec, etag, err := apiextensionsopenapi.CustomResourceDefinitionOpenAPISpec(&crd.Spec, version.Version, validationSchema)
|
||||
crdspec, etag, err := apiextensionsopenapi.CustomResourceDefinitionOpenAPISpec(&crd.Spec, version.Version, versionedSchema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -50,17 +50,8 @@ func ValidateCustomResource(customResource interface{}, validator *validate.Sche
|
|||
return nil
|
||||
}
|
||||
|
||||
// ConvertJSONSchemaProps converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema.
|
||||
// ConvertJSONSchemaProps converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema
|
||||
func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema) error {
|
||||
return ConvertJSONSchemaPropsWithPostProcess(in, out, nil)
|
||||
}
|
||||
|
||||
// PostProcessFunc post-processes one node of a spec.Schema.
|
||||
type PostProcessFunc func(*spec.Schema) error
|
||||
|
||||
// ConvertJSONSchemaPropsWithPostProcess converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema
|
||||
// and run a post process step on each JSONSchemaProps node.
|
||||
func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, out *spec.Schema, postProcess PostProcessFunc) error {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -95,43 +86,41 @@ func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, ou
|
|||
out.Example = *(in.Example)
|
||||
}
|
||||
|
||||
if in.Enum != nil {
|
||||
out.Enum = make([]interface{}, len(in.Enum))
|
||||
for k, v := range in.Enum {
|
||||
out.Enum[k] = v
|
||||
}
|
||||
out.Enum = make([]interface{}, len(in.Enum))
|
||||
for k, v := range in.Enum {
|
||||
out.Enum[k] = v
|
||||
}
|
||||
|
||||
if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf, postProcess); err != nil {
|
||||
if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := convertSliceOfJSONSchemaProps(&in.OneOf, &out.OneOf, postProcess); err != nil {
|
||||
if err := convertSliceOfJSONSchemaProps(&in.OneOf, &out.OneOf); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf, postProcess); err != nil {
|
||||
if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if in.Not != nil {
|
||||
in, out := &in.Not, &out.Not
|
||||
*out = new(spec.Schema)
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
|
||||
if err := ConvertJSONSchemaProps(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
out.Properties, err = convertMapOfJSONSchemaProps(in.Properties, postProcess)
|
||||
out.Properties, err = convertMapOfJSONSchemaProps(in.Properties)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out.PatternProperties, err = convertMapOfJSONSchemaProps(in.PatternProperties, postProcess)
|
||||
out.PatternProperties, err = convertMapOfJSONSchemaProps(in.PatternProperties)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions, postProcess)
|
||||
out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -146,7 +135,7 @@ func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, ou
|
|||
if in.AdditionalProperties != nil {
|
||||
in, out := &in.AdditionalProperties, &out.AdditionalProperties
|
||||
*out = new(spec.SchemaOrBool)
|
||||
if err := convertJSONSchemaPropsorBool(*in, *out, postProcess); err != nil {
|
||||
if err := convertJSONSchemaPropsorBool(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -154,7 +143,7 @@ func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, ou
|
|||
if in.AdditionalItems != nil {
|
||||
in, out := &in.AdditionalItems, &out.AdditionalItems
|
||||
*out = new(spec.SchemaOrBool)
|
||||
if err := convertJSONSchemaPropsorBool(*in, *out, postProcess); err != nil {
|
||||
if err := convertJSONSchemaPropsorBool(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -162,7 +151,7 @@ func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, ou
|
|||
if in.Items != nil {
|
||||
in, out := &in.Items, &out.Items
|
||||
*out = new(spec.SchemaOrArray)
|
||||
if err := convertJSONSchemaPropsOrArray(*in, *out, postProcess); err != nil {
|
||||
if err := convertJSONSchemaPropsOrArray(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -172,7 +161,7 @@ func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, ou
|
|||
*out = make(spec.Dependencies, len(*in))
|
||||
for key, val := range *in {
|
||||
newVal := new(spec.SchemaOrStringArray)
|
||||
if err := convertJSONSchemaPropsOrStringArray(&val, newVal, postProcess); err != nil {
|
||||
if err := convertJSONSchemaPropsOrStringArray(&val, newVal); err != nil {
|
||||
return err
|
||||
}
|
||||
(*out)[key] = *newVal
|
||||
|
@ -185,20 +174,14 @@ func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, ou
|
|||
out.ExternalDocs.URL = in.ExternalDocs.URL
|
||||
}
|
||||
|
||||
if postProcess != nil {
|
||||
if err := postProcess(out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]spec.Schema, postProcess PostProcessFunc) error {
|
||||
func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]spec.Schema) error {
|
||||
if in != nil {
|
||||
for _, jsonSchemaProps := range *in {
|
||||
schema := spec.Schema{}
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(&jsonSchemaProps, &schema, postProcess); err != nil {
|
||||
if err := ConvertJSONSchemaProps(&jsonSchemaProps, &schema); err != nil {
|
||||
return err
|
||||
}
|
||||
*out = append(*out, schema)
|
||||
|
@ -207,27 +190,25 @@ func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]s
|
|||
return nil
|
||||
}
|
||||
|
||||
func convertMapOfJSONSchemaProps(in map[string]apiextensions.JSONSchemaProps, postProcess PostProcessFunc) (map[string]spec.Schema, error) {
|
||||
if in == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func convertMapOfJSONSchemaProps(in map[string]apiextensions.JSONSchemaProps) (map[string]spec.Schema, error) {
|
||||
out := make(map[string]spec.Schema)
|
||||
for k, jsonSchemaProps := range in {
|
||||
schema := spec.Schema{}
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(&jsonSchemaProps, &schema, postProcess); err != nil {
|
||||
return nil, err
|
||||
if len(in) != 0 {
|
||||
for k, jsonSchemaProps := range in {
|
||||
schema := spec.Schema{}
|
||||
if err := ConvertJSONSchemaProps(&jsonSchemaProps, &schema); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[k] = schema
|
||||
}
|
||||
out[k] = schema
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray, postProcess PostProcessFunc) error {
|
||||
func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray) error {
|
||||
if in.Schema != nil {
|
||||
in, out := &in.Schema, &out.Schema
|
||||
*out = new(spec.Schema)
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
|
||||
if err := ConvertJSONSchemaProps(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -235,7 +216,7 @@ func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out
|
|||
in, out := &in.JSONSchemas, &out.Schemas
|
||||
*out = make([]spec.Schema, len(*in))
|
||||
for i := range *in {
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(&(*in)[i], &(*out)[i], postProcess); err != nil {
|
||||
if err := ConvertJSONSchemaProps(&(*in)[i], &(*out)[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -243,24 +224,24 @@ func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out
|
|||
return nil
|
||||
}
|
||||
|
||||
func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool, postProcess PostProcessFunc) error {
|
||||
func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool) error {
|
||||
out.Allows = in.Allows
|
||||
if in.Schema != nil {
|
||||
in, out := &in.Schema, &out.Schema
|
||||
*out = new(spec.Schema)
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
|
||||
if err := ConvertJSONSchemaProps(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray, postProcess PostProcessFunc) error {
|
||||
func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray) error {
|
||||
out.Property = in.Property
|
||||
if in.Schema != nil {
|
||||
in, out := &in.Schema, &out.Schema
|
||||
*out = new(spec.Schema)
|
||||
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
|
||||
if err := ConvertJSONSchemaProps(*in, *out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ go_library(
|
|||
deps = [
|
||||
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions:go_default_library",
|
||||
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation:go_default_library",
|
||||
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||
"//vendor/github.com/go-openapi/spec:go_default_library",
|
||||
|
@ -28,8 +28,7 @@ go_test(
|
|||
srcs = ["conversion_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
||||
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library",
|
||||
"//vendor/github.com/go-openapi/spec:go_default_library",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -17,29 +17,33 @@ limitations under the License.
|
|||
package openapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-openapi/spec"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||
)
|
||||
|
||||
// ConvertJSONSchemaPropsToOpenAPIv2Schema converts our internal OpenAPI v3 schema
|
||||
// (*apiextensions.JSONSchemaProps) to an OpenAPI v2 schema (*spec.Schema).
|
||||
// NOTE: we use versioned type (v1beta1) here so that we can properly marshal the object
|
||||
// using the JSON tags
|
||||
func ConvertJSONSchemaPropsToOpenAPIv2Schema(in *apiextensions.JSONSchemaProps) (*spec.Schema, error) {
|
||||
func ConvertJSONSchemaPropsToOpenAPIv2Schema(in *v1beta1.JSONSchemaProps) (*spec.Schema, error) {
|
||||
if in == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Marshal JSONSchemaProps into JSON and unmarshal the data into spec.Schema
|
||||
data, err := json.Marshal(*in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := new(spec.Schema)
|
||||
validation.ConvertJSONSchemaPropsWithPostProcess(in, out, func(p *spec.Schema) error {
|
||||
// Remove unsupported fields in OpenAPI v2
|
||||
p.OneOf = nil
|
||||
p.AnyOf = nil
|
||||
p.Not = nil
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := out.UnmarshalJSON(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Remove unsupported fields in OpenAPI v2
|
||||
out.OneOf = nil
|
||||
out.AnyOf = nil
|
||||
out.Not = nil
|
||||
return out, nil
|
||||
}
|
||||
|
|
|
@ -17,12 +17,12 @@ limitations under the License.
|
|||
package openapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/go-openapi/spec"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||
)
|
||||
|
||||
func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
||||
|
@ -30,16 +30,18 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
testStr2 := "test2"
|
||||
testFloat64 := float64(6.4)
|
||||
testInt64 := int64(64)
|
||||
testApiextensionsJSON := apiextensions.JSON(testStr)
|
||||
raw, _ := json.Marshal(testStr)
|
||||
raw2, _ := json.Marshal(testStr2)
|
||||
testApiextensionsJSON := v1beta1.JSON{Raw: raw}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
in *apiextensions.JSONSchemaProps
|
||||
in *v1beta1.JSONSchemaProps
|
||||
expected *spec.Schema
|
||||
}{
|
||||
{
|
||||
name: "id",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
ID: testStr,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -47,7 +49,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "$schema",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Schema: "test",
|
||||
},
|
||||
expected: &spec.Schema{
|
||||
|
@ -58,14 +60,14 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "$ref",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Ref: &testStr,
|
||||
},
|
||||
expected: spec.RefSchema(testStr),
|
||||
},
|
||||
{
|
||||
name: "description",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Description: testStr,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -73,7 +75,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "type and format",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Type: testStr,
|
||||
Format: testStr2,
|
||||
},
|
||||
|
@ -82,7 +84,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "title",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Title: testStr,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -90,7 +92,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "default",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Default: &testApiextensionsJSON,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -98,7 +100,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "maximum and exclusiveMaximum",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Maximum: &testFloat64,
|
||||
ExclusiveMaximum: true,
|
||||
},
|
||||
|
@ -107,7 +109,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "minimum and exclusiveMinimum",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Minimum: &testFloat64,
|
||||
ExclusiveMinimum: true,
|
||||
},
|
||||
|
@ -116,7 +118,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "maxLength",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MaxLength: &testInt64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -124,7 +126,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "minLength",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MinLength: &testInt64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -132,7 +134,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "pattern",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Pattern: testStr,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -140,7 +142,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "maxItems",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MaxItems: &testInt64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -148,7 +150,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "minItems",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MinItems: &testInt64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -156,7 +158,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "uniqueItems",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
UniqueItems: true,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -164,7 +166,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "multipleOf",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MultipleOf: &testFloat64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -172,15 +174,15 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "enum",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Enum: []apiextensions.JSON{apiextensions.JSON(testStr), apiextensions.JSON(testStr2)},
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Enum: []v1beta1.JSON{{Raw: raw}, {Raw: raw2}},
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
WithEnum(testStr, testStr2),
|
||||
},
|
||||
{
|
||||
name: "maxProperties",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MaxProperties: &testInt64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -188,7 +190,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "minProperties",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
MinProperties: &testInt64,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -196,7 +198,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "required",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Required: []string{testStr, testStr2},
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -204,9 +206,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "items single props",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Items: &apiextensions.JSONSchemaPropsOrArray{
|
||||
Schema: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Items: &v1beta1.JSONSchemaPropsOrArray{
|
||||
Schema: &v1beta1.JSONSchemaProps{
|
||||
Type: "boolean",
|
||||
},
|
||||
},
|
||||
|
@ -221,9 +223,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "items array props",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Items: &apiextensions.JSONSchemaPropsOrArray{
|
||||
JSONSchemas: []apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Items: &v1beta1.JSONSchemaPropsOrArray{
|
||||
JSONSchemas: []v1beta1.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
|
@ -242,8 +244,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "allOf",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
AllOf: []apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
AllOf: []v1beta1.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
|
@ -253,8 +255,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "oneOf",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
OneOf: []apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
OneOf: []v1beta1.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
|
@ -271,8 +273,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "anyOf",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
AnyOf: []apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
AnyOf: []v1beta1.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
|
@ -289,8 +291,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "not",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Not: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Not: &v1beta1.JSONSchemaProps{
|
||||
Type: "boolean",
|
||||
},
|
||||
},
|
||||
|
@ -301,90 +303,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
// },
|
||||
// },
|
||||
},
|
||||
{
|
||||
name: "nested logic",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
AllOf: []apiextensions.JSONSchemaProps{
|
||||
{
|
||||
Not: &apiextensions.JSONSchemaProps{
|
||||
Type: "boolean",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnyOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
{
|
||||
OneOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
{Type: "string"},
|
||||
},
|
||||
AnyOf: []apiextensions.JSONSchemaProps{
|
||||
{
|
||||
Not: &apiextensions.JSONSchemaProps{
|
||||
Type: "boolean",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnyOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
{
|
||||
OneOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
{Type: "string"},
|
||||
},
|
||||
OneOf: []apiextensions.JSONSchemaProps{
|
||||
{
|
||||
Not: &apiextensions.JSONSchemaProps{
|
||||
Type: "boolean",
|
||||
},
|
||||
},
|
||||
{
|
||||
AnyOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
{
|
||||
OneOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
{Type: "string"},
|
||||
},
|
||||
Not: &apiextensions.JSONSchemaProps{
|
||||
Not: &apiextensions.JSONSchemaProps{
|
||||
Type: "boolean",
|
||||
},
|
||||
AnyOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
OneOf: []apiextensions.JSONSchemaProps{
|
||||
{Type: "boolean"},
|
||||
{Type: "string"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
WithAllOf(spec.Schema{}, spec.Schema{}, spec.Schema{}, *spec.StringProperty()),
|
||||
},
|
||||
{
|
||||
name: "properties",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Properties: map[string]apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Properties: map[string]v1beta1.JSONSchemaProps{
|
||||
testStr: {Type: "boolean"},
|
||||
},
|
||||
},
|
||||
|
@ -393,10 +315,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "additionalProperties",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
AdditionalProperties: &apiextensions.JSONSchemaPropsOrBool{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{
|
||||
Allows: true,
|
||||
Schema: &apiextensions.JSONSchemaProps{Type: "boolean"},
|
||||
Schema: &v1beta1.JSONSchemaProps{Type: "boolean"},
|
||||
},
|
||||
},
|
||||
expected: &spec.Schema{
|
||||
|
@ -410,8 +332,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "patternProperties",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
PatternProperties: map[string]apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
PatternProperties: map[string]v1beta1.JSONSchemaProps{
|
||||
testStr: {Type: "boolean"},
|
||||
},
|
||||
},
|
||||
|
@ -425,10 +347,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "dependencies schema",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Dependencies: apiextensions.JSONSchemaDependencies{
|
||||
testStr: apiextensions.JSONSchemaPropsOrStringArray{
|
||||
Schema: &apiextensions.JSONSchemaProps{Type: "boolean"},
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Dependencies: v1beta1.JSONSchemaDependencies{
|
||||
testStr: v1beta1.JSONSchemaPropsOrStringArray{
|
||||
Schema: &v1beta1.JSONSchemaProps{Type: "boolean"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -444,9 +366,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "dependencies string array",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Dependencies: apiextensions.JSONSchemaDependencies{
|
||||
testStr: apiextensions.JSONSchemaPropsOrStringArray{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Dependencies: v1beta1.JSONSchemaDependencies{
|
||||
testStr: v1beta1.JSONSchemaPropsOrStringArray{
|
||||
Property: []string{testStr2},
|
||||
},
|
||||
},
|
||||
|
@ -463,10 +385,10 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "additionalItems",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
AdditionalItems: &apiextensions.JSONSchemaPropsOrBool{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
AdditionalItems: &v1beta1.JSONSchemaPropsOrBool{
|
||||
Allows: true,
|
||||
Schema: &apiextensions.JSONSchemaProps{Type: "boolean"},
|
||||
Schema: &v1beta1.JSONSchemaProps{Type: "boolean"},
|
||||
},
|
||||
},
|
||||
expected: &spec.Schema{
|
||||
|
@ -480,9 +402,9 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "definitions",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
Definitions: apiextensions.JSONSchemaDefinitions{
|
||||
testStr: apiextensions.JSONSchemaProps{Type: "boolean"},
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Definitions: v1beta1.JSONSchemaDefinitions{
|
||||
testStr: v1beta1.JSONSchemaProps{Type: "boolean"},
|
||||
},
|
||||
},
|
||||
expected: &spec.Schema{
|
||||
|
@ -495,8 +417,8 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "externalDocs",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
ExternalDocs: &apiextensions.ExternalDocumentation{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
ExternalDocs: &v1beta1.ExternalDocumentation{
|
||||
Description: testStr,
|
||||
URL: testStr2,
|
||||
},
|
||||
|
@ -506,7 +428,7 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "example",
|
||||
in: &apiextensions.JSONSchemaProps{
|
||||
in: &v1beta1.JSONSchemaProps{
|
||||
Example: &testApiextensionsJSON,
|
||||
},
|
||||
expected: new(spec.Schema).
|
||||
|
@ -515,14 +437,12 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
out, err := ConvertJSONSchemaPropsToOpenAPIv2Schema(test.in)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error in converting openapi schema: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(*out, *test.expected) {
|
||||
t.Errorf("unexpected result:\n want=%v\n got=%v\n\n%s", *test.expected, *out, diff.ObjectDiff(*test.expected, *out))
|
||||
}
|
||||
})
|
||||
out, err := ConvertJSONSchemaPropsToOpenAPIv2Schema(test.in)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error in converting openapi schema: %v", test.name)
|
||||
}
|
||||
if !reflect.DeepEqual(out, test.expected) {
|
||||
t.Errorf("result of conversion test '%v' didn't match, want: %v; got: %v", test.name, *test.expected, *out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import (
|
|||
"github.com/go-openapi/spec"
|
||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
|
@ -205,7 +206,7 @@ func scaleStatusSchema() *spec.Schema {
|
|||
// NOTE: in apiserver we general operates on internal types. We are using versioned (v1beta1)
|
||||
// validation schema here because we need the json tags to properly marshal the object to
|
||||
// JSON.
|
||||
func CustomResourceDefinitionOpenAPISpec(crdSpec *apiextensions.CustomResourceDefinitionSpec, version string, validationSchema *apiextensions.CustomResourceValidation) (*spec.Swagger, string, error) {
|
||||
func CustomResourceDefinitionOpenAPISpec(crdSpec *apiextensions.CustomResourceDefinitionSpec, version string, validationSchema *v1beta1.CustomResourceValidation) (*spec.Swagger, string, error) {
|
||||
schema := &spec.Schema{}
|
||||
if validationSchema != nil && validationSchema.OpenAPIV3Schema != nil {
|
||||
var err error
|
||||
|
|
Loading…
Reference in New Issue