Skip to content

Commit ccd6c36

Browse files
committed
added a testcase for ensuring correct different enum type precedence
1 parent bb6e2b6 commit ccd6c36

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

pkg/crd/markers/validation.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ func (m MaxProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
470470
}
471471

472472
func (m Enum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
473+
// apply this enum only if there were no other enum values specified
474+
// (e.g. via a "+enum" marker)
475+
if len(schema.Enum) != 0 {
476+
return nil
477+
}
473478
// TODO(directxman12): this is a bit hacky -- we should
474479
// probably support AnyType better + using the schema structure
475480
vals := make([]apiext.JSON, len(m))

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crd/testdata/cronjob_types.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ const (
5151
EnumType_Three EnumType = "three"
5252
)
5353

54+
// This enum type tests for whether when both "+enum" and
55+
// "+kubebuilder:validation:Enum" are defined the former takes precedence.
56+
// It should.
57+
//
58+
// +enum
59+
// +kubebuilder:validation:Enum=Allow;Forbid;Replace
60+
type AnotherEnumType string
61+
62+
const (
63+
AnotherEnumType_One AnotherEnumType = "another_one"
64+
AnotherEnumType_Two AnotherEnumType = "another_two"
65+
AnotherEnumType_Three AnotherEnumType = "another_three"
66+
)
67+
5468
// CronJobSpec defines the desired state of CronJob
5569
// +kubebuilder:validation:XValidation:rule="has(oldSelf.forbiddenInt) || !has(self.forbiddenInt)",message="forbiddenInt is not allowed",fieldPath=".forbiddenInt",reason="FieldValueForbidden"
5670
type CronJobSpec struct {
@@ -341,7 +355,8 @@ type CronJobSpec struct {
341355
// +kubebuilder:validation:items:Enum=0;1;3
342356
EnumSlice []int `json:"enumSlice,omitempty"`
343357

344-
EnumValue EnumType `json:"enumValue,omitempty"`
358+
EnumValue EnumType `json:"enumValue,omitempty"`
359+
AnotherEnumValue AnotherEnumType `json:"anotherEnumValue,omitempty"`
345360

346361
HostsAlias Hosts `json:"hostsAlias,omitempty"`
347362

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ spec:
5151
- PreferDualStack
5252
- RequireDualStack
5353
type: string
54+
anotherEnumValue:
55+
description: |-
56+
This enum type tests for whether when both "+enum" and
57+
"+kubebuilder:validation:Enum" are defined the former takes precedence.
58+
It should.
59+
enum:
60+
- another_one
61+
- another_two
62+
- another_three
63+
type: string
5464
array:
5565
description: Checks that fixed-length arrays work
5666
items:

0 commit comments

Comments
 (0)