Skip to content

Commit b7a48d5

Browse files
authored
Merge pull request #1224 from shashankram/oneof-order
🐛markers/OneOf: make order deterministic relative to XValidation
2 parents 2d026a6 + a8e8d46 commit b7a48d5

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

pkg/crd/markers/validation.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,10 @@ func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
660660
return nil
661661
}
662662

663+
func (XValidation) ApplyPriority() ApplyPriority {
664+
return ApplyPriorityDefault
665+
}
666+
663667
func (fields AtMostOneOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
664668
if len(fields) == 0 {
665669
return nil
@@ -672,6 +676,11 @@ func (fields AtMostOneOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
672676
return xvalidation.ApplyToSchema(schema)
673677
}
674678

679+
func (AtMostOneOf) ApplyPriority() ApplyPriority {
680+
// explicitly go after XValidation markers so that the ordering is deterministic
681+
return XValidation{}.ApplyPriority() + 1
682+
}
683+
675684
func (fields ExactlyOneOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
676685
if len(fields) == 0 {
677686
return nil
@@ -684,6 +693,11 @@ func (fields ExactlyOneOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
684693
return xvalidation.ApplyToSchema(schema)
685694
}
686695

696+
func (ExactlyOneOf) ApplyPriority() ApplyPriority {
697+
// explicitly go after XValidation markers so that the ordering is deterministic
698+
return XValidation{}.ApplyPriority() + 1
699+
}
700+
687701
// fieldsToOneOfCelRuleStr converts a slice of field names to a string representation
688702
// [has(self.field1),has(self.field1),...].filter(x, x == true).size()
689703
func fieldsToOneOfCelRuleStr(fields []string) string {

pkg/crd/testdata/oneof/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type OneofSpec struct {
3434
SecondTypeWithExactOneof *TypeWithMultipleExactOneofs `json:"secondTypeWithExactOneof,omitempty"`
3535
}
3636

37+
// +kubebuilder:validation:XValidation:message="only one of foo|bar may be set",rule="!(has(self.foo) && has(self.bar))"
3738
// +kubebuilder:validation:AtMostOneOf=foo;bar
3839
type TypeWithOneofs struct {
3940
Foo *string `json:"foo,omitempty"`

pkg/crd/testdata/testdata.kubebuilder.io_oneofs.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ spec:
5656
type: string
5757
type: object
5858
x-kubernetes-validations:
59+
- message: only one of foo|bar may be set
60+
rule: '!(has(self.foo) && has(self.bar))'
5961
- message: at most one of the fields in [foo bar] may be set
6062
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
6163
1'

0 commit comments

Comments
 (0)