Skip to content

Commit

Permalink
pkg/crd: support validation on type alias to basic types
Browse files Browse the repository at this point in the history
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 <tarazichris@gmail.com>
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
  • Loading branch information
mtardy and christarazi committed Oct 21, 2024
1 parent d0b7b74 commit 7587d2c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
if err != nil {
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
}
// Check for type aliasing to a basic type. Note that this is no longer
// needed with gotypesalias=1 as the above isBasic check is false. See
// more https://pkg.go.dev/go/types#Alias:
// > For gotypesalias=1, alias declarations produce an Alias type.
// > Otherwise, the alias information is only in the type name, which
// > points directly to the actual (aliased) type.
if basicInfo.Name() != ident.Name {
ctx.requestSchema("", ident.Name)
link := TypeRefLink("", ident.Name)
return &apiext.JSONSchemaProps{
Type: typ,
Format: fmt,
Ref: &link,
}
}
return &apiext.JSONSchemaProps{
Type: typ,
Format: fmt,
Expand Down
12 changes: 12 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,14 @@ type CronJobSpec struct {
// This tests that string alias is handled correctly.
StringAlias StringAlias `json:"stringAlias,omitempty"`

// This tests that validation on a string alias type is handled correctly.
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=255
StringAliasAddedValidation StringAlias `json:"stringAliasAddedValidation,omitempty"`

// This tests that validation on a the string alias type itself is handled correctly.
StringAliasAlreadyValidated StringAliasWithValidation `json:"stringAliasAlreadyValidated,omitempty"`

// This tests string slice validation.
// +kubebuilder:validation:MinItems=2
// +kubebuilder:validation:MaxItems=2
Expand Down Expand Up @@ -359,6 +367,10 @@ type CronJobSpec struct {

type StringAlias = string

// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=255
type StringAliasWithValidation = string

type ContainsNestedMap struct {
InnerMap map[string]string `json:"innerMap,omitempty"`
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8978,6 +8978,16 @@ spec:
stringAlias:
description: This tests that string alias is handled correctly.
type: string
stringAliasAddedValidation:
description: This tests that validation on a string alias type is handled correctly.
maxLength: 255
minLength: 1
type: string
stringAliasAlreadyValidated:
description: This tests that validation on a the string alias type itself is handled correctly.
maxLength: 255
minLength: 1
type: string
stringPair:
description: This tests string slice validation.
items:
Expand Down

0 comments on commit 7587d2c

Please sign in to comment.