Skip to content

Commit 9983a52

Browse files
authored
Merge pull request kubernetes#94888 from sttts/sttts-preserve-unknown-fields-array
apiextensions: prune array type without items in published OpenAPI
2 parents b27ecb2 + bde88c6 commit 9983a52

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/v2/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ go_test(
2424
"//vendor/github.com/googleapis/gnostic/openapiv2:go_default_library",
2525
"//vendor/gopkg.in/yaml.v2:go_default_library",
2626
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
27+
"//vendor/k8s.io/utils/pointer:go_default_library",
2728
],
2829
)
2930

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/v2/conversion.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ func ToStructuralOpenAPIV2(in *structuralschema.Structural) *structuralschema.St
7373
changed = true
7474
}
7575

76+
if s.Items == nil && s.Type == "array" {
77+
// kubectl cannot cope with array without item schema, e.g. due to XPreserveUnknownFields case above
78+
// https://github.com/kubernetes/kube-openapi/blob/64514a1d5d596b96e6f957e2be275ae14d6b0804/pkg/util/proto/document.go#L185
79+
s.Type = ""
80+
81+
changed = true
82+
}
83+
7684
for f, fs := range s.Properties {
7785
if fs.Nullable {
7886
s.ValueValidation.Required, changed = filterOut(s.ValueValidation.Required, f)

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/v2/conversion_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
3737
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
3838
"k8s.io/kube-openapi/pkg/util/proto"
39+
"k8s.io/utils/pointer"
3940
)
4041

4142
func Test_ConvertJSONSchemaPropsToOpenAPIv2Schema(t *testing.T) {
@@ -643,6 +644,31 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2SchemaByType(t *testing.T) {
643644
WithExample(testStr),
644645
expectDiff: true,
645646
},
647+
{
648+
name: "preserve-unknown-fields in arrays",
649+
in: &apiextensions.JSONSchemaProps{
650+
XPreserveUnknownFields: pointer.BoolPtr(true),
651+
Type: "array",
652+
Items: &apiextensions.JSONSchemaPropsOrArray{Schema: &apiextensions.JSONSchemaProps{
653+
Type: "string",
654+
}},
655+
},
656+
expected: withVendorExtensions(new(spec.Schema), "x-kubernetes-preserve-unknown-fields", true),
657+
},
658+
{
659+
name: "preserve-unknown-fields in objects",
660+
in: &apiextensions.JSONSchemaProps{
661+
XPreserveUnknownFields: pointer.BoolPtr(true),
662+
Type: "object",
663+
Properties: map[string]apiextensions.JSONSchemaProps{
664+
"foo": {
665+
Type: "string",
666+
},
667+
},
668+
},
669+
expected: withVendorExtensions(new(spec.Schema), "x-kubernetes-preserve-unknown-fields", true).
670+
Typed("object", ""),
671+
},
646672
}
647673

648674
for _, test := range tests {
@@ -666,6 +692,11 @@ func Test_ConvertJSONSchemaPropsToOpenAPIv2SchemaByType(t *testing.T) {
666692
}
667693
}
668694

695+
func withVendorExtensions(s *spec.Schema, key string, value interface{}) *spec.Schema {
696+
s.VendorExtensible.AddExtension(key, value)
697+
return s
698+
}
699+
669700
func refEqual(x spec.Ref, y spec.Ref) bool {
670701
return x.String() == y.String()
671702
}

0 commit comments

Comments
 (0)