mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
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:
parent
8d30a5f136
commit
bde88c6ef7
@ -24,6 +24,7 @@ go_test(
|
|||||||
"//vendor/github.com/googleapis/gnostic/openapiv2:go_default_library",
|
"//vendor/github.com/googleapis/gnostic/openapiv2:go_default_library",
|
||||||
"//vendor/gopkg.in/yaml.v2: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/kube-openapi/pkg/util/proto:go_default_library",
|
||||||
|
"//vendor/k8s.io/utils/pointer:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,6 +73,14 @@ func ToStructuralOpenAPIV2(in *structuralschema.Structural) *structuralschema.St
|
|||||||
changed = true
|
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 {
|
for f, fs := range s.Properties {
|
||||||
if fs.Nullable {
|
if fs.Nullable {
|
||||||
s.ValueValidation.Required, changed = filterOut(s.ValueValidation.Required, f)
|
s.ValueValidation.Required, changed = filterOut(s.ValueValidation.Required, f)
|
||||||
|
@ -36,6 +36,7 @@ import (
|
|||||||
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||||
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
|
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
|
||||||
"k8s.io/kube-openapi/pkg/util/proto"
|
"k8s.io/kube-openapi/pkg/util/proto"
|
||||||
|
"k8s.io/utils/pointer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
|
||||||
@ -643,6 +644,31 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2SchemaByType(t *testing.T) {
|
|||||||
WithExample(testStr),
|
WithExample(testStr),
|
||||||
expectDiff: true,
|
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 {
|
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 {
|
func refEqual(x spec.Ref, y spec.Ref) bool {
|
||||||
return x.String() == y.String()
|
return x.String() == y.String()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user