Skip to content

Commit a57ec68

Browse files
authored
Merge pull request #1150 from everettraven/feature/optional-oldself-xvalidation
✨ markers: add support for optionalOldSelf in XValidation marker
2 parents 4e1fb05 + 5e25270 commit a57ec68

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

Diff for: pkg/crd/markers/validation.go

+2
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ type XValidation struct {
335335
MessageExpression string `marker:"messageExpression,optional"`
336336
Reason string `marker:"reason,optional"`
337337
FieldPath string `marker:"fieldPath,optional"`
338+
OptionalOldSelf *bool `marker:"optionalOldSelf,optional"`
338339
}
339340

340341
func (m Maximum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
@@ -603,6 +604,7 @@ func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
603604
MessageExpression: m.MessageExpression,
604605
Reason: reason,
605606
FieldPath: m.FieldPath,
607+
OptionalOldSelf: m.OptionalOldSelf,
606608
})
607609
return nil
608610
}

Diff for: pkg/crd/markers/zz_generated.markerhelp.go

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/crd/testdata/cronjob_types.go

+4
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ type CronJobSpec struct {
381381

382382
// This tests that embedded struct, which is an alias type, is handled correctly.
383383
InlineAlias `json:",inline"`
384+
385+
// Test that we can add a field that can only be set to a non-default value on updates using XValidation OptionalOldSelf.
386+
// +kubebuilder:validation:XValidation:rule="oldSelf.hasValue() || self == 0",message="must be set to 0 on creation. can be set to any value on an update.",optionalOldSelf=true
387+
OnlyAllowSettingOnUpdate int32 `json:"onlyAllowSettingOnUpdate,omitempty"`
384388
}
385389

386390
type InlineAlias = EmbeddedStruct

Diff for: pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -8967,6 +8967,16 @@ spec:
89678967
This flag is like suspend, but for when you really mean it.
89688968
It helps test the +kubebuilder:validation:Type marker.
89698969
type: string
8970+
onlyAllowSettingOnUpdate:
8971+
description: Test that we can add a field that can only be set to
8972+
a non-default value on updates using XValidation OptionalOldSelf.
8973+
format: int32
8974+
type: integer
8975+
x-kubernetes-validations:
8976+
- message: must be set to 0 on creation. can be set to any value on
8977+
an update.
8978+
optionalOldSelf: true
8979+
rule: oldSelf.hasValue() || self == 0
89708980
patternObject:
89718981
description: This tests that pattern validator is properly applied.
89728982
pattern: ^$|^((https):\/\/?)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/?))$

0 commit comments

Comments
 (0)