Skip to content

Commit

Permalink
feat: add naming regex check on validating webhook (#1541)
Browse files Browse the repository at this point in the history
- check experiments' naming convention on validating webhook
  • Loading branch information
anencore94 authored Jun 1, 2021
1 parent 778403b commit e3ccbcf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/webhook/v1beta1/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ 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])?")
if !namingConvention.MatchString(instance.Name) {
msg :="Name must consist of lower case alphanumeric characters or '-'," +
" start with an alphabetic character, and end with an alphanumeric character" +
" (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?)'"

return fmt.Errorf(msg)
}

if instance.Spec.MaxFailedTrialCount != nil && *instance.Spec.MaxFailedTrialCount < 0 {
return fmt.Errorf("spec.maxFailedTrialCount should not be less than 0")
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/webhook/v1beta1/experiment/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func TestValidateExperiment(t *testing.T) {
oldInstance *experimentsv1beta1.Experiment
testDescription string
}{
// Name
{
Instance: func() *experimentsv1beta1.Experiment {
i := newFakeInstance()
i.Name = "1234-test"
return i
}(),
Err: true,
testDescription: "Name is invalid",
},
//Objective
{
Instance: func() *experimentsv1beta1.Experiment {
Expand Down

0 comments on commit e3ccbcf

Please sign in to comment.