-
Notifications
You must be signed in to change notification settings - Fork 444
✨ +enum
validation marker
#1179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
1bf22cb
b338e3f
bb6e2b6
ccd6c36
410a208
040149f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,8 @@ var ValidationIshMarkers = []*definitionWithHelp{ | |
WithHelp(XPreserveUnknownFields{}.Help()), | ||
must(markers.MakeDefinition("kubebuilder:pruning:PreserveUnknownFields", markers.DescribesType, XPreserveUnknownFields{})). | ||
WithHelp(XPreserveUnknownFields{}.Help()), | ||
must(markers.MakeDefinition("enum", markers.DescribesType, InferredEnum{})). | ||
WithHelp(InferredEnum{}.Help()), | ||
} | ||
|
||
func init() { | ||
|
@@ -468,6 +470,11 @@ func (m MaxProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error { | |
} | ||
|
||
func (m Enum) ApplyToSchema(schema *apiext.JSONSchemaProps) error { | ||
// apply this enum only if there were no other enum values specified | ||
// (e.g. via a "+enum" marker) | ||
if len(schema.Enum) != 0 { | ||
return nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any other markers that do this kind of thing? Is there precedence? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. But enum markers are kind of the only ones that do the same thing differently so some decision on precedence here is needed in any case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CC @alvaroaleman @sbueringer do you have an opinion on this? At present, when you have the
I wonder if, to be consistent, this case should also do the same? It would encourage developers to deduplicate their markers which I think is probably a positive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe this is less of a usecase here since both markers ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we want to make the preference configurable. The default behaviour though I think needs to be that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think configurable precedence would complicate things too much - we can just make the For known core types it can be made configurable, but, honestly, the now-generated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, but depending on the use case, this is likely a breaking change for the people using the CRDs built by this tooling
I would lean towards this for now to be safe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
so, a configuration flag? but an opt-in or opt-out one is what we need? I personally would argue for the latter since the change seems to be beneficial in most of the cases (at least when something like PodTemplate is used) |
||
// TODO(directxman12): this is a bit hacky -- we should | ||
// probably support AnyType better + using the schema structure | ||
vals := make([]apiext.JSON, len(m)) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,29 @@ import ( | |
|
||
const DefaultRefValue = "defaultRefValue" | ||
|
||
// +enum | ||
type EnumType string | ||
|
||
const ( | ||
EnumType_One EnumType = "one" | ||
EnumType_Two EnumType = "two" | ||
EnumType_Three EnumType = "three" | ||
) | ||
|
||
// This enum type tests for whether when both "+enum" and | ||
// "+kubebuilder:validation:Enum" are defined the former takes precedence. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The latter should be taking precedence no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. The tests show There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MishimaPorte Can we get this comment updated please |
||
// It should. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this last sentence. |
||
// | ||
// +enum | ||
// +kubebuilder:validation:Enum=Allow;Forbid;Replace | ||
type AnotherEnumType string | ||
|
||
const ( | ||
AnotherEnumType_One AnotherEnumType = "another_one" | ||
AnotherEnumType_Two AnotherEnumType = "another_two" | ||
AnotherEnumType_Three AnotherEnumType = "another_three" | ||
) | ||
|
||
// CronJobSpec defines the desired state of CronJob | ||
// +kubebuilder:validation:XValidation:rule="has(oldSelf.forbiddenInt) || !has(self.forbiddenInt)",message="forbiddenInt is not allowed",fieldPath=".forbiddenInt",reason="FieldValueForbidden" | ||
type CronJobSpec struct { | ||
|
@@ -332,6 +355,9 @@ type CronJobSpec struct { | |
// +kubebuilder:validation:items:Enum=0;1;3 | ||
EnumSlice []int `json:"enumSlice,omitempty"` | ||
|
||
EnumValue EnumType `json:"enumValue,omitempty"` | ||
AnotherEnumValue AnotherEnumType `json:"anotherEnumValue,omitempty"` | ||
|
||
HostsAlias Hosts `json:"hostsAlias,omitempty"` | ||
|
||
// This tests that alias imported from a package is handled correctly. The | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any other markers that do this kind of thing? Is there precedence?