apiextensions: prune array type without items in published OpenAPI

kubectl falls over arrays without item schema. Hence, we have to publish a less precise OpenAPI spec (similar to other pruning we already do for the same reason).
This commit is contained in:
Dr. Stefan Schimanski 2020-09-18 13:44:10 +02:00
parent 8d30a5f136
commit bde88c6ef7
3 changed files with 40 additions and 0 deletions

View File

@ -24,6 +24,7 @@ go_test(
"//vendor/github.com/googleapis/gnostic/openapiv2:go_default_library",
"//vendor/gopkg.in/yaml.v2:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
],
)

View File

@ -73,6 +73,14 @@ func ToStructuralOpenAPIV2(in *structuralschema.Structural) *structuralschema.St
changed = true
}
if s.Items == nil && s.Type == "array" {
// kubectl cannot cope with array without item schema, e.g. due to XPreserveUnknownFields case above
// https://github.com/kubernetes/kube-openapi/blob/64514a1d5d596b96e6f957e2be275ae14d6b0804/pkg/util/proto/document.go#L185
s.Type = ""
changed = true
}
for f, fs := range s.Properties {
if fs.Nullable {
s.ValueValidation.Required, changed = filterOut(s.ValueValidation.Required, f)

View File

@ -36,6 +36,7 @@ import (
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
"k8s.io/kube-openapi/pkg/util/proto"
"k8s.io/utils/pointer"
)
func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
@ -643,6 +644,31 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2SchemaByType(t *testing.T) {
WithExample(testStr),
expectDiff: true,
},
{
name: "preserve-unknown-fields in arrays",
in: &apiextensions.JSONSchemaProps{
XPreserveUnknownFields: pointer.BoolPtr(true),
Type: "array",
Items: &apiextensions.JSONSchemaPropsOrArray{Schema: &apiextensions.JSONSchemaProps{
Type: "string",
}},
},
expected: withVendorExtensions(new(spec.Schema), "x-kubernetes-preserve-unknown-fields", true),
},
{
name: "preserve-unknown-fields in objects",
in: &apiextensions.JSONSchemaProps{
XPreserveUnknownFields: pointer.BoolPtr(true),
Type: "object",
Properties: map[string]apiextensions.JSONSchemaProps{
"foo": {
Type: "string",
},
},
},
expected: withVendorExtensions(new(spec.Schema), "x-kubernetes-preserve-unknown-fields", true).
Typed("object", ""),
},
}
for _, test := range tests {
@ -666,6 +692,11 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2SchemaByType(t *testing.T) {
}
}
func withVendorExtensions(s *spec.Schema, key string, value interface{}) *spec.Schema {
s.VendorExtensible.AddExtension(key, value)
return s
}
func refEqual(x spec.Ref, y spec.Ref) bool {
return x.String() == y.String()
}