Skip to content
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

Merged
merged 1 commit into from
Jun 1, 2021

Conversation

anencore94
Copy link
Member

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:

  1. Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Release note:

NONE

@aws-kf-ci-bot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@johnugeorge
Copy link
Member

/ok-to-test

Copy link
Member

@andreyvelich andreyvelich left a 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])$")
Copy link
Member

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 ?

Suggested change
namingConvention, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])$")
namingConvention, _ := regexp.Compile("[a-z]([-a-z0-9]*[a-z0-9])?")

Copy link
Member Author

@anencore94 anencore94 May 28, 2021

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])?"

Copy link
Member

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.

Copy link
Member

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 ?

Copy link
Member Author

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 $.

pkg/webhook/v1beta1/experiment/validator/validator.go Outdated Show resolved Hide resolved
pkg/webhook/v1beta1/experiment/validator/validator_test.go Outdated Show resolved Hide resolved
- check experiments' naming convention on validating webhook
Copy link
Member

@andreyvelich andreyvelich left a 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

@johnugeorge
Copy link
Member

/approve

@google-oss-robot
Copy link

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-robot google-oss-robot merged commit e3ccbcf into kubeflow:master Jun 1, 2021
@anencore94 anencore94 deleted the validating_name branch June 2, 2021 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

experiment naming convention
5 participants