Skip to content

Commit 7587d2c

Browse files
mtardychristarazi
andcommitted
pkg/crd: support validation on type alias to basic types
Without the type Alias being its own type (Go 1.22 see gotypesalias), in localNamedToSchema, the link is missing. [ Upstream port of cilium/controller-tools@d944debcff34 ("Support type aliasing to basic types") ] Previously, validation schema generation was skipped on a type alias: type A = B Whereas, a new type was fine: type A B This commit implements support for generating validation schemas for type aliases to basic types, i.e. string, int, etc. Co-authored-by: Chris Tarazi <[email protected]> Signed-off-by: Mahe Tardy <[email protected]>
1 parent d0b7b74 commit 7587d2c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

pkg/crd/schema.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
247247
if err != nil {
248248
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
249249
}
250+
// Check for type aliasing to a basic type. Note that this is no longer
251+
// needed with gotypesalias=1 as the above isBasic check is false. See
252+
// more https://pkg.go.dev/go/types#Alias:
253+
// > For gotypesalias=1, alias declarations produce an Alias type.
254+
// > Otherwise, the alias information is only in the type name, which
255+
// > points directly to the actual (aliased) type.
256+
if basicInfo.Name() != ident.Name {
257+
ctx.requestSchema("", ident.Name)
258+
link := TypeRefLink("", ident.Name)
259+
return &apiext.JSONSchemaProps{
260+
Type: typ,
261+
Format: fmt,
262+
Ref: &link,
263+
}
264+
}
250265
return &apiext.JSONSchemaProps{
251266
Type: typ,
252267
Format: fmt,

pkg/crd/testdata/cronjob_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ type CronJobSpec struct {
330330
// This tests that string alias is handled correctly.
331331
StringAlias StringAlias `json:"stringAlias,omitempty"`
332332

333+
// This tests that validation on a string alias type is handled correctly.
334+
// +kubebuilder:validation:MinLength=1
335+
// +kubebuilder:validation:MaxLength=255
336+
StringAliasAddedValidation StringAlias `json:"stringAliasAddedValidation,omitempty"`
337+
338+
// This tests that validation on a the string alias type itself is handled correctly.
339+
StringAliasAlreadyValidated StringAliasWithValidation `json:"stringAliasAlreadyValidated,omitempty"`
340+
333341
// This tests string slice validation.
334342
// +kubebuilder:validation:MinItems=2
335343
// +kubebuilder:validation:MaxItems=2
@@ -359,6 +367,10 @@ type CronJobSpec struct {
359367

360368
type StringAlias = string
361369

370+
// +kubebuilder:validation:MinLength=1
371+
// +kubebuilder:validation:MaxLength=255
372+
type StringAliasWithValidation = string
373+
362374
type ContainsNestedMap struct {
363375
InnerMap map[string]string `json:"innerMap,omitempty"`
364376
}

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8978,6 +8978,16 @@ spec:
89788978
stringAlias:
89798979
description: This tests that string alias is handled correctly.
89808980
type: string
8981+
stringAliasAddedValidation:
8982+
description: This tests that validation on a string alias type is handled correctly.
8983+
maxLength: 255
8984+
minLength: 1
8985+
type: string
8986+
stringAliasAlreadyValidated:
8987+
description: This tests that validation on a the string alias type itself is handled correctly.
8988+
maxLength: 255
8989+
minLength: 1
8990+
type: string
89818991
stringPair:
89828992
description: This tests string slice validation.
89838993
items:

0 commit comments

Comments
 (0)