Skip to content

Commit

Permalink
Merge pull request #1078 from mtardy/pr/mtardy/type-alias-validation
Browse files Browse the repository at this point in the history
🐛 pkg/crd: support validation on type alias to basic types
  • Loading branch information
k8s-ci-robot authored Oct 25, 2024
2 parents 5656666 + e443da3 commit 49ae6f8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,30 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("unknown type %s", ident.Name), ident))
return &apiext.JSONSchemaProps{}
}
// This reproduces the behavior we had pre gotypesalias=1 (needed if this
// project is compiled with default settings and Go >= 1.23).
if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias {
typeInfo = aliasInfo.Underlying()
}
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
if err != nil {
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
}
// Check for type aliasing to a basic type for gotypesalias=0. See more
// in documentation 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 @@ -335,6 +335,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 @@ -364,6 +372,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 @@ -8984,6 +8984,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 49ae6f8

Please sign in to comment.