-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add naming regex check on validating webhook #1541
Conversation
Hi @anencore94. Thanks for your PR. I'm waiting for a kubeflow member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/ok-to-test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for your contribution @anencore94!
I left few comments.
@@ -67,6 +67,14 @@ func (g *DefaultValidator) InjectClient(c client.Client) { | |||
// ValidateExperiment validates experiment for the given instance. | |||
// oldInst is specified when experiment is edited. | |||
func (g *DefaultValidator) ValidateExperiment(instance, oldInst *experimentsv1beta1.Experiment) error { | |||
namingConvention, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])$") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we follow the regex syntax from the controller-runtime suggestion ?
namingConvention, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])$") | |
namingConvention, _ := regexp.Compile("[a-z]([-a-z0-9]*[a-z0-9])?") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andreyvelich
I'm not an expert with golang, I'm a bit confused with this:
package main
import (
"fmt"
"regexp"
)
func main() {
namingConvention, _ := regexp.Compile("[a-z]([-a-z0-9]*[a-z0-9])?")
fmt.Println(namingConvention.MatchString("test")) // true
fmt.Println(namingConvention.MatchString("test-1234")) // true
fmt.Println(namingConvention.MatchString("1234-1234")) // false
fmt.Println(namingConvention.MatchString("1234-test")) // true!! this is what i didn't expected
nc2, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?") // '^' was added
fmt.Println(nc2.MatchString("test")) // true
fmt.Println(nc2.MatchString("test-1234")) // true
fmt.Println(nc2.MatchString("1234-1234")) // false
fmt.Println(nc2.MatchString("1234-test")) // false!!, so I followed this regexp pattern.
}
That's why I used the pattern "^[a-z]([-a-z0-9]*[a-z0-9])?"
, not "[a-z]([-a-z0-9]*[a-z0-9])?"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anencore94 Thank you for investigating this, you are right.
As I can see, kubebuilder also adds ^
and $
to the regex: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/internal/validation/dns.go#L45.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anencore94 do you want to keep your first regex: ^[a-z]([-a-z0-9]*[a-z0-9])$
or with ?
at the end ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my first regex was wrong since this case:
package main
import (
"fmt"
"regexp"
)
func main() {
nc1, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?")
fmt.Println(nc1.MatchString("test")) // true
fmt.Println(nc1.MatchString("test-1234")) // true
fmt.Println(nc1.MatchString("1234-1234")) // false
fmt.Println(nc1.MatchString("1234-test")) // false
fmt.Println(nc1.MatchString("t")) // true, That's what we expected
fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
nc2, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])$")
fmt.Println(nc2.MatchString("test")) // true
fmt.Println(nc2.MatchString("test-1234")) // true
fmt.Println(nc2.MatchString("1234-1234")) // false
fmt.Println(nc2.MatchString("1234-test")) // false
fmt.Println(nc2.MatchString("t")) // false!! this is what we didn't expected
fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
nc3, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?$")
fmt.Println(nc3.MatchString("test")) // true
fmt.Println(nc3.MatchString("test-1234")) // true
fmt.Println(nc3.MatchString("1234-1234")) // false
fmt.Println(nc3.MatchString("1234-test")) // false
fmt.Println(nc3.MatchString("t")) // true, That's what we expected
}
I'm not sure what to use between nc
and nc3
.
But I guess we should use nc3
: ^[a-z]([-a-z0-9]*[a-z0-9])?$
, since you mentioned, It seems golang always needs ^
and $
.
2a28caa
to
f4c650c
Compare
- check experiments' naming convention on validating webhook
f4c650c
to
2659d7b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the updates @anencore94!
/lgtm
/cc @johnugeorge
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: anencore94, johnugeorge 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 |
What this PR does / why we need it:
When invalid-named experiment CREATE API was requested, such experiment status was shown as CREATED, but in fact it was not created.
Therefore, naming convention should be checked in validating webhook
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #1538
Special notes for your reviewer:
Release note: