feat(operator): enforce RFC 1035 validation for TrainJob name#2767
Conversation
6d4897e to
45be2cc
Compare
kannon92
left a comment
There was a problem hiding this comment.
Curious if you should just add this validation to the api?
you could easily do this via a CEL expression and avoid having to add it to the webhooks.
|
/retest |
Hmm. I’ll leave that to the maintainers. I like to follow similar patterns as the other code. |
|
@juniemariam you can find some information about CEL validation rules in https://kubernetes.io/blog/2022/09/23/crd-validation-rules-beta/ and the documentation at The idea is to specify a CEL rule directly in the Golang API using the There is also the format library that conveniently exposes the |
Pull Request Test Coverage Report for Build 16923747863Details
💛 - Coveralls |
|
Update: Switched from webhook-based validation to CEL-based validation for TrainJob.metadata.name.
Verified with:
Let me know if further changes are needed! |
|
Looking really good! It would be nice to see some integration tests for this logic in this code |
| // +kubebuilder:storageversion | ||
| // +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.conditions[-1:].type` | ||
| // +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` | ||
| // +kubebuilder:validation:XValidation:rule="self.metadata.name.matches('^[a-z]([-a-z0-9]*[a-z0-9])?$')", message="metadata.name must match RFC 1035 DNS label format" |
There was a problem hiding this comment.
would using !format.dns1123Label().validate(self.metadata.name).hasValue() be slightly better?
There was a problem hiding this comment.
Thanks for the suggestion @astefanutti ! I initially tried using the format library expression as recommended in the Kubernetes docs (e.g., !format.dns1035Label().validate(self.metadata.name).hasValue()), but it failed to pass the CEL cost estimation and could not be applied due to cost budget errors in the CRD. Hence I changed to regex check
Please let me know if you'd like me to try any other alternatives or if this existing approach works for now.
There was a problem hiding this comment.
@juniemariam thanks for the explanation. That looks good to me as it is.
| wantError: nil, | ||
| wantWarnings: nil, | ||
| }, | ||
| "invalid trainjob name with uppercase letters": { |
There was a problem hiding this comment.
I had assumed this test would/could be kept?
There was a problem hiding this comment.
@astefanutti Just to clarify—now that the validation is enforced in the CRD using CEL, do we still need to keep the Go/webhook logic and its related unit tests?
I moved the tests out of the webhook layer because the validation logic now lives in the CRD, so the webhook no longer performs this check.
Could you please advise whether it’s better to keep both the Go logic and tests (alongside the CEL rule and integration test), or just rely on CEL + integration tests?
There was a problem hiding this comment.
@juniemariam that makes sense. Yes an integration test could be useful.
Signed-off-by: Junie <juniemariam@gmail.com>
…Job name Signed-off-by: Junie <juniemariam@gmail.com>
Signed-off-by: Junie <juniemariam@gmail.com>
e4487b4 to
8a0b2ca
Compare
|
/ok-to-test |
|
/lgtm Thanks @juniemariam! |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: tenzen-y The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
…ow#2767) * feat(webhook): enforce RFC 1035 validation for TrainJob name Signed-off-by: Junie <juniemariam@gmail.com> * feat(operator): enforce RFC 1035 validation using CEL rules for TrainJob name Signed-off-by: Junie <juniemariam@gmail.com> * test(integration): added integration test for TrainJob name validation Signed-off-by: Junie <juniemariam@gmail.com> --------- Signed-off-by: Junie <juniemariam@gmail.com>
What this PR does / why we need it:
TrainJob.metadata.name-, etc.)Why this is needed
This is a follow-up to #2734. Previously, invalid
TrainJobnames caused JobSet webhook rejections during reconciliation. This PR ensures invalid names are caught earlier at the TrainJob admission webhook level, improving clarity and user experience.Related Issues
Fixes #2735
Related to #2621, #2734
Tests
go test ./pkg/webhooks/...andmake test