diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats.go index c11f1c627a7..5c0397909b6 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats.go @@ -113,9 +113,11 @@ func GetUnrecognizedFormats(schema *spec.Schema, compatibilityVersion *version.V return unrecognizedFormats } - normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization - if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) { - unrecognizedFormats = append(unrecognizedFormats, schema.Format) + if len(schema.Type) == 1 && schema.Type[0] == "string" { + normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization + if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) { + unrecognizedFormats = append(unrecognizedFormats, schema.Format) + } } return unrecognizedFormats diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats_test.go index 7558444b218..b4a0e08d741 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/formats_test.go @@ -148,58 +148,76 @@ func TestGetUnrecognizedFormats(t *testing.T) { }{ { name: "empty format", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: ""}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 0), expectedFormats: []string{}, }, { name: "recognized format at version 1.0", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 0), expectedFormats: []string{}, }, { name: "unrecognized format at version 1.0", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 0), expectedFormats: []string{"unknown-format"}, }, { name: "recognized format with normalization", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 34), expectedFormats: []string{}, }, { name: "unrecognized format with normalization", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-long-name"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-long-name", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 33), expectedFormats: []string{"k8s-long-name"}, }, { name: "format introduced in later version", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 0), expectedFormats: []string{"k8s-short-name"}, }, { name: "format with dash normalization", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8sshortname"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8sshortname", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 34), expectedFormats: []string{}, }, { name: "recognized format at exact version", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "uuid"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "uuid", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 0), expectedFormats: []string{}, }, { name: "recognized format at higher version", - schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email"}}, + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email", Type: []string{"string"}}}, compatibilityVersion: version.MajorMinor(1, 35), expectedFormats: []string{}, }, + { + name: "unrecognized format for integer type is not reported", + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"integer"}}}, + compatibilityVersion: version.MajorMinor(1, 0), + expectedFormats: []string{}, + }, + { + name: "unrecognized format for string,null type is not reported", + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string", "null"}}}, + compatibilityVersion: version.MajorMinor(1, 0), + expectedFormats: []string{}, + }, + { + name: "unrecognized format for no type is not reported", + schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}}, + compatibilityVersion: version.MajorMinor(1, 0), + expectedFormats: []string{}, + }, } for _, tc := range testCases { diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresourcedefinition/strategy.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresourcedefinition/strategy.go index 1705f3abcf8..fba3c43311b 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresourcedefinition/strategy.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresourcedefinition/strategy.go @@ -223,7 +223,7 @@ func getUnrecognizedFormatsInSchema(schema *apiextensions.JSONSchemaProps, compa validation.SchemaHas(schema, func(s *apiextensions.JSONSchemaProps) bool { if len(s.Format) > 0 { // Convert to spec.Schema for format validation - specSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Format: s.Format}} + specSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Format: s.Format, Type: []string{s.Type}}} if formats := apiservervalidation.GetUnrecognizedFormats(specSchema, compatibilityVersion); len(formats) > 0 { unrecognizedFormats = append(unrecognizedFormats, formats...) }