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

Add validation webhooks for maxFailedTrialCount and parallelTrialCount #1936

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/template-setup-e2e-test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ runs:
uses: docker/setup-buildx-action@v2

- name: Set Up Go env
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.17.10
go-version-file: go.mod
19 changes: 15 additions & 4 deletions pkg/webhook/v1beta1/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,26 @@ func (g *DefaultValidator) ValidateExperiment(instance, oldInst *experimentsv1be
return fmt.Errorf(msg)
}

if instance.Spec.MaxFailedTrialCount != nil && *instance.Spec.MaxFailedTrialCount < 0 {
return fmt.Errorf("spec.maxFailedTrialCount should not be less than 0")
if instance.Spec.MaxFailedTrialCount != nil {
if *instance.Spec.MaxFailedTrialCount < 0 {
return fmt.Errorf("spec.maxFailedTrialCount should not be less than 0")
}
if instance.Spec.MaxTrialCount != nil && *instance.Spec.MaxFailedTrialCount > *instance.Spec.MaxTrialCount {
return fmt.Errorf("spec.maxFailedTrialCount should be less than or equal to spec.maxTrialCount")
}
}
if instance.Spec.MaxTrialCount != nil && *instance.Spec.MaxTrialCount <= 0 {
return fmt.Errorf("spec.maxTrialCount must be greater than 0")
}
if instance.Spec.ParallelTrialCount != nil && *instance.Spec.ParallelTrialCount <= 0 {
return fmt.Errorf("spec.parallelTrialCount must be greater than 0")
if instance.Spec.ParallelTrialCount != nil {
if *instance.Spec.ParallelTrialCount <= 0 {
return fmt.Errorf("spec.parallelTrialCount must be greater than 0")
}
if instance.Spec.MaxTrialCount != nil && *instance.Spec.ParallelTrialCount > *instance.Spec.MaxTrialCount {
return fmt.Errorf("spec.paralelTrialCount should be less than or equal to spec.maxTrialCount")
}
tenzen-y marked this conversation as resolved.
Show resolved Hide resolved
}

if oldInst != nil {
// We should validate restart only if appropriate fields are changed.
// Otherwise check below is triggered when experiment is deleted.
Expand Down
48 changes: 48 additions & 0 deletions pkg/webhook/v1beta1/experiment/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,54 @@ func TestValidateExperiment(t *testing.T) {
Err: true,
testDescription: "Invalid feasible space in parameters",
},
{
Instance: func() *experimentsv1beta1.Experiment {
maxTrialCount := int32(5)
invalidMaxFailedTrialCount := int32(6)
i := newFakeInstance()
i.Spec.MaxTrialCount = &maxTrialCount
i.Spec.MaxFailedTrialCount = &invalidMaxFailedTrialCount
return i
}(),
Err: true,
testDescription: "maxFailedTrialCount greater than maxTrialCount",
},
{
Instance: func() *experimentsv1beta1.Experiment {
maxTrialCount := int32(5)
validMaxFailedTrialCount := int32(5)
i := newFakeInstance()
i.Spec.MaxTrialCount = &maxTrialCount
i.Spec.MaxFailedTrialCount = &validMaxFailedTrialCount
return i
}(),
Err: false,
testDescription: "maxFailedTrialCount equal to maxTrialCount",
},
{
Instance: func() *experimentsv1beta1.Experiment {
maxTrialCount := int32(5)
invalidParallelTrialCount := int32(6)
i := newFakeInstance()
i.Spec.MaxTrialCount = &maxTrialCount
i.Spec.ParallelTrialCount = &invalidParallelTrialCount
return i
}(),
Err: true,
testDescription: "parallelTrialCount greater than maxTrialCount",
},
{
Instance: func() *experimentsv1beta1.Experiment {
maxTrialCount := int32(5)
validParallelTrialCount := int32(5)
i := newFakeInstance()
i.Spec.MaxTrialCount = &maxTrialCount
i.Spec.ParallelTrialCount = &validParallelTrialCount
return i
}(),
Err: false,
testDescription: "parallelTrialCount equal to maxTrialCount",
},
}

for _, tc := range tcs {
Expand Down