Skip to content

Commit 3896d9f

Browse files
yongruilink8s-publishing-bot
authored andcommitted
fix: Only warn for unrecognized formats on type=string
Kubernetes-commit: 7bd2900b1e6c80d16f373fc431973385791f969d
1 parent aada5e8 commit 3896d9f

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

pkg/apiserver/validation/formats.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ func GetUnrecognizedFormats(schema *spec.Schema, compatibilityVersion *version.V
113113
return unrecognizedFormats
114114
}
115115

116-
normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization
117-
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
118-
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
116+
if len(schema.Type) == 1 && schema.Type[0] == "string" {
117+
normalized := strings.ReplaceAll(schema.Format, "-", "") // go-openapi default format name normalization
118+
if !supportedFormatsAtVersion(compatibilityVersion).supported.Has(normalized) {
119+
unrecognizedFormats = append(unrecognizedFormats, schema.Format)
120+
}
119121
}
120122

121123
return unrecognizedFormats

pkg/apiserver/validation/formats_test.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,58 +148,76 @@ func TestGetUnrecognizedFormats(t *testing.T) {
148148
}{
149149
{
150150
name: "empty format",
151-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: ""}},
151+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "", Type: []string{"string"}}},
152152
compatibilityVersion: version.MajorMinor(1, 0),
153153
expectedFormats: []string{},
154154
},
155155
{
156156
name: "recognized format at version 1.0",
157-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email"}},
157+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email", Type: []string{"string"}}},
158158
compatibilityVersion: version.MajorMinor(1, 0),
159159
expectedFormats: []string{},
160160
},
161161
{
162162
name: "unrecognized format at version 1.0",
163-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}},
163+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string"}}},
164164
compatibilityVersion: version.MajorMinor(1, 0),
165165
expectedFormats: []string{"unknown-format"},
166166
},
167167
{
168168
name: "recognized format with normalization",
169-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name"}},
169+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name", Type: []string{"string"}}},
170170
compatibilityVersion: version.MajorMinor(1, 34),
171171
expectedFormats: []string{},
172172
},
173173
{
174174
name: "unrecognized format with normalization",
175-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-long-name"}},
175+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-long-name", Type: []string{"string"}}},
176176
compatibilityVersion: version.MajorMinor(1, 33),
177177
expectedFormats: []string{"k8s-long-name"},
178178
},
179179
{
180180
name: "format introduced in later version",
181-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name"}},
181+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8s-short-name", Type: []string{"string"}}},
182182
compatibilityVersion: version.MajorMinor(1, 0),
183183
expectedFormats: []string{"k8s-short-name"},
184184
},
185185
{
186186
name: "format with dash normalization",
187-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8sshortname"}},
187+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "k8sshortname", Type: []string{"string"}}},
188188
compatibilityVersion: version.MajorMinor(1, 34),
189189
expectedFormats: []string{},
190190
},
191191
{
192192
name: "recognized format at exact version",
193-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "uuid"}},
193+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "uuid", Type: []string{"string"}}},
194194
compatibilityVersion: version.MajorMinor(1, 0),
195195
expectedFormats: []string{},
196196
},
197197
{
198198
name: "recognized format at higher version",
199-
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email"}},
199+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "email", Type: []string{"string"}}},
200200
compatibilityVersion: version.MajorMinor(1, 35),
201201
expectedFormats: []string{},
202202
},
203+
{
204+
name: "unrecognized format for integer type is not reported",
205+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"integer"}}},
206+
compatibilityVersion: version.MajorMinor(1, 0),
207+
expectedFormats: []string{},
208+
},
209+
{
210+
name: "unrecognized format for string,null type is not reported",
211+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format", Type: []string{"string", "null"}}},
212+
compatibilityVersion: version.MajorMinor(1, 0),
213+
expectedFormats: []string{},
214+
},
215+
{
216+
name: "unrecognized format for no type is not reported",
217+
schema: &spec.Schema{SchemaProps: spec.SchemaProps{Format: "unknown-format"}},
218+
compatibilityVersion: version.MajorMinor(1, 0),
219+
expectedFormats: []string{},
220+
},
203221
}
204222

205223
for _, tc := range testCases {

pkg/registry/customresourcedefinition/strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func getUnrecognizedFormatsInSchema(schema *apiextensions.JSONSchemaProps, compa
223223
validation.SchemaHas(schema, func(s *apiextensions.JSONSchemaProps) bool {
224224
if len(s.Format) > 0 {
225225
// Convert to spec.Schema for format validation
226-
specSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Format: s.Format}}
226+
specSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Format: s.Format, Type: []string{s.Type}}}
227227
if formats := apiservervalidation.GetUnrecognizedFormats(specSchema, compatibilityVersion); len(formats) > 0 {
228228
unrecognizedFormats = append(unrecognizedFormats, formats...)
229229
}

0 commit comments

Comments
 (0)