Skip to content

Conversation

@everettraven
Copy link
Contributor

Adds a new field to the +kubebuilder:validation:XValidation marker to specify optionalOldSelf. Adding this configuration option allows writing validation rules that behave one way on create vs update. For example, allow setting a field if it is an update, but don't allow the field to be set at creation.

For more explicit information on the optionalOldSelf field for validation rules, see https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-optional-oldself

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 17, 2025
@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Feb 17, 2025
@everettraven everettraven force-pushed the feature/optional-oldself-xvalidation branch from 50be6ae to 1e35f95 Compare February 17, 2025 16:35
@JoelSpeed
Copy link
Contributor

Given previous PR to work on the path and reason that I did, this looks pretty much the same
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 17, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: ab13142695d853aa9b83d90622888af8a971579a


// CronJobSpec defines the desired state of CronJob
// +kubebuilder:validation:XValidation:rule="has(oldSelf.forbiddenInt) || !has(self.forbiddenInt)",message="forbiddenInt is not allowed",fieldPath=".forbiddenInt",reason="FieldValueForbidden"
// +kubebuilder:validation:XValidation:rule="has(oldSelf.forbiddenInt) || !has(self.forbiddenInt)",message="forbiddenInt is not allowed",fieldPath=".forbiddenInt",reason="FieldValueForbidden",optionalOldSelf=true
Copy link
Member

@joelanford joelanford Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the rule need to change to account for the fact that oldSelf is now optional?

EDIT: I know it doesn't in order to actually test that this field propagates to the CRD, but it these examples are useful for illustrative purposes as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in the latest push to include a new field with an explicit example of oldSelf being optional.

@everettraven everettraven force-pushed the feature/optional-oldself-xvalidation branch from 1e35f95 to d28c772 Compare February 17, 2025 21:13
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 17, 2025
@joelanford
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 18, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 8240734ad15205a1bd7965d262eac4af5d1cdfe0

@JoelSpeed
Copy link
Contributor

@sbueringer @alvaroaleman Either you able to help us get this merged please?

InlineAlias `json:",inline"`

// Test that we can add a field that can only be set on updates using XValidation OptionalOldSelf.
// The validation is applied to the spec struct itself and not the field.
Copy link
Member

@sbueringer sbueringer Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// The validation is applied to the spec struct itself and not the field.

I'm a bit confused about this statement. Is it correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops - forgot to update that after I decided to move it from the spec. Will update that soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest push.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 18, 2025
@sbueringer
Copy link
Member

sbueringer commented Feb 18, 2025

/hold
@JoelSpeed feel free to merge once the godoc is updated

@sbueringer
Copy link
Member

/approve

@sbueringer sbueringer added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 18, 2025
@everettraven everettraven force-pushed the feature/optional-oldself-xvalidation branch from d28c772 to 31acd94 Compare February 18, 2025 13:47
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 18, 2025
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
@everettraven everettraven force-pushed the feature/optional-oldself-xvalidation branch from 31acd94 to 5e25270 Compare February 18, 2025 14:26
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: everettraven, sbueringer

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@JoelSpeed
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 18, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 100575e3126e862f349604addfcaeb8b2caf933d

@JoelSpeed
Copy link
Contributor

/hold cancel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

5 participants