Skip to content

Commit

Permalink
Merge pull request #653 from benluddy/xvalidations-crd-marker
Browse files Browse the repository at this point in the history
✨ Add XValidation CRD marker for KEP-2876 support.
  • Loading branch information
k8s-ci-robot committed Feb 12, 2022
2 parents cf1ea27 + b0d2867 commit 99e7fbc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
20 changes: 20 additions & 0 deletions pkg/crd/markers/validation.go
Expand Up @@ -67,6 +67,7 @@ var ValidationMarkers = mustMakeAllWithPrefix("kubebuilder:validation", markers.
XPreserveUnknownFields{},
XEmbeddedResource{},
XIntOrString{},
XValidation{},
)

// FieldOnlyMarkers list field-specific validation markers (i.e. those markers that don't make
Expand Down Expand Up @@ -251,6 +252,17 @@ type XIntOrString struct{}
// to be used only as a last resort.
type Schemaless struct{}

// +controllertools:marker:generateHelp:category="CRD validation"
// XValidation marks a field as requiring a value for which a given
// expression evaluates to true.
//
// This marker may be repeated to specify multiple expressions, all of
// which must evaluate to true.
type XValidation struct {
Rule string
Message string `marker:",optional"`
}

func (m Maximum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
if schema.Type != "integer" {
return fmt.Errorf("must apply maximum to an integer")
Expand Down Expand Up @@ -428,3 +440,11 @@ func (m XIntOrString) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
}

func (m XIntOrString) ApplyFirst() {}

func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
schema.XValidations = append(schema.XValidations, apiext.ValidationRule{
Rule: m.Rule,
Message: m.Message,
})
return nil
}
20 changes: 20 additions & 0 deletions pkg/crd/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/crd/testdata/cronjob_types.go
Expand Up @@ -187,7 +187,12 @@ type CronJobSpec struct {

// This tests that both unexported and exported inline fields are not skipped in the schema generation
unexportedStruct `json:",inline"`
ExportedStruct `json:",inline"`
ExportedStruct `json:",inline"`

// Test of the expression-based validation rule marker, with optional message.
// +kubebuilder:validation:XValidation:rule="self.size() % 2 == 0",message="must have even length"
// +kubebuilder:validation:XValidation:rule="true"
StringWithEvenLength string `json:"stringWithEvenLength,omitempty"`
}

type ContainsNestedMap struct {
Expand Down
8 changes: 8 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Expand Up @@ -7267,6 +7267,14 @@ spec:
type: array
description: This tests string slices are allowed as map values.
type: object
stringWithEvenLength:
description: Test of the expression-based validation rule marker,
with optional message.
type: string
x-kubernetes-validations:
- message: must have even length
rule: self.size() % 2 == 0
- rule: "true"
structWithSeveralFields:
description: A struct that can only be entirely replaced
properties:
Expand Down

0 comments on commit 99e7fbc

Please sign in to comment.