Skip to content

Commit be71a1e

Browse files
committed
Prevent XValidation duplication by verifying if the rule already exists
1 parent 97ea62c commit be71a1e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

pkg/crd/markers/validation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,14 @@ func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
666666
}
667667
}
668668

669+
// Check if this validation rule already exists to prevent duplication
670+
for _, existingRule := range schema.XValidations {
671+
if existingRule.Rule == m.Rule {
672+
// Rule already exists, don't add it again
673+
return nil
674+
}
675+
}
676+
669677
schema.XValidations = append(schema.XValidations, apiext.ValidationRule{
670678
Rule: m.Rule,
671679
Message: m.Message,

pkg/crd/testdata/oneof/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type OneofSpec struct {
3636
TypeWithMultipleAtLeastOneofs *TypeWithMultipleAtLeastOneofs `json:"typeWithMultipleAtLeastOneOf,omitempty"`
3737

3838
TypeWithAllOneOf *TypeWithAllOneofs `json:"typeWithAllOneOf,omitempty"`
39+
40+
// The validation rule on TypeWithOneofs should not be duplicated.
41+
SameTypeWithOneof *TypeWithOneofs `json:"sameTypeWithOneof,omitempty"`
3942
}
4043

4144
// +kubebuilder:validation:XValidation:message="only one of foo|bar may be set",rule="!(has(self.foo) && has(self.bar))"

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ spec:
6060
- message: at most one of the fields in [foo bar] may be set
6161
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
6262
1'
63+
sameTypeWithOneof:
64+
description: The validation rule on TypeWithOneofs should not be duplicated.
65+
properties:
66+
bar:
67+
type: string
68+
foo:
69+
type: string
70+
type: object
71+
x-kubernetes-validations:
72+
- message: only one of foo|bar may be set
73+
rule: '!(has(self.foo) && has(self.bar))'
74+
- message: at most one of the fields in [foo bar] may be set
75+
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
76+
1'
6377
secondTypeWithExactOneof:
6478
properties:
6579
a:

0 commit comments

Comments
 (0)