Skip to content

Commit f4c650c

Browse files
committed
feat: add naming regex check on validating webhook
- check experiments' naming convention on validating webhook
1 parent 778403b commit f4c650c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/webhook/v1beta1/experiment/validator/validator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func (g *DefaultValidator) InjectClient(c client.Client) {
6767
// ValidateExperiment validates experiment for the given instance.
6868
// oldInst is specified when experiment is edited.
6969
func (g *DefaultValidator) ValidateExperiment(instance, oldInst *experimentsv1beta1.Experiment) error {
70+
namingConvention, _ := regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?")
71+
if !namingConvention.MatchString(instance.Name) {
72+
msg :="Name must consist of lower case alphanumeric characters or '-'," +
73+
" start with an alphabetic character, and end with an alphanumeric character" +
74+
" (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')"
75+
76+
return fmt.Errorf(msg)
77+
}
78+
7079
if instance.Spec.MaxFailedTrialCount != nil && *instance.Spec.MaxFailedTrialCount < 0 {
7180
return fmt.Errorf("spec.maxFailedTrialCount should not be less than 0")
7281
}

pkg/webhook/v1beta1/experiment/validator/validator_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func TestValidateExperiment(t *testing.T) {
6868
oldInstance *experimentsv1beta1.Experiment
6969
testDescription string
7070
}{
71+
// Name
72+
{
73+
Instance: func() *experimentsv1beta1.Experiment {
74+
i := newFakeInstance()
75+
i.Name = "1234-test"
76+
return i
77+
}(),
78+
Err: true,
79+
testDescription: "Name is invalid",
80+
},
7181
//Objective
7282
{
7383
Instance: func() *experimentsv1beta1.Experiment {

0 commit comments

Comments
 (0)