controller-tools
controller-tools copied to clipboard
Invalid CRD generated when XValidation is used on both field and struct
When the XValidation marker is used on both a field and the type for that field the generated CRD is invalid because it tries to use an allOf property to merge the validations.
For example, given the definition of the following structs:
type FooSpec struct {
// +kubebuilder:validation:XValidation:rule="size(self.field) > 2",message="validation 1"
Thing Thing `json:"thing"`
}
// +kubebuilder:validation:XValidation:rule="has(self.field)",message="validation 2"
type Thing struct {
Field *string `json:"field"`
}
The generated CRD will include:
properties:
thing:
allOf:
- x-kubernetes-validations:
- message: validation 2
rule: has(self.field)
- x-kubernetes-validations:
- message: validation 1
rule: size(self.field) > 2
properties:
field:
type: string
required:
- field
type: object
Where the expected result would have been:
properties:
thing:
x-kubernetes-validations:
- message: validation 2
rule: has(self.field)
- message: validation 1
rule: size(self.field) > 2
properties:
field:
type: string
required:
- field
type: object
I have a full reproduction of the bug including the invalid CRDs on https://github.com/cezarsa/validationbug/blob/a8148487551b92b4f3b6669cde9c4050e35cb98d/main.go.