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

fix: only validate Kubernetes Job #2025

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 13 additions & 17 deletions pkg/webhook/v1beta1/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,26 +376,22 @@ func (g *DefaultValidator) validateTrialTemplate(instance *experimentsv1beta1.Ex
func (g *DefaultValidator) validateTrialJob(runSpec *unstructured.Unstructured) error {
gvk := runSpec.GroupVersionKind()

// Validate only Job
switch gvk.Kind {
case consts.JobKindJob:
batchJob := batchv1.Job{}

// Validate that RunSpec can be converted to Batch Job
err := runtime.DefaultUnstructuredConverter.FromUnstructured(runSpec.Object, &batchJob)
if err != nil {
return fmt.Errorf("unable to convert spec.TrialTemplate: %v to %v: %v", runSpec.Object, gvk.Kind, err)
}
// Validate only Kubernetes Job
if gvk.GroupVersion() != batchv1.SchemeGroupVersion || gvk.Kind != consts.JobKindJob {
return nil
}

// Try to patch runSpec to Batch Job
// TODO (andreyvelich): Do we want to remove it completely ?
err = validatePatchJob(runSpec, batchJob, gvk.Kind)
if err != nil {
return err
}
batchJob := batchv1.Job{}

// Validate that RunSpec can be converted to Batch Job
err := runtime.DefaultUnstructuredConverter.FromUnstructured(runSpec.Object, &batchJob)
if err != nil {
return fmt.Errorf("unable to convert spec.TrialTemplate: %v to %v: %v", runSpec.Object, gvk.Kind, err)
}

return nil
// Try to patch runSpec to Batch Job
// TODO (andreyvelich): Do we want to remove it completely ?
return validatePatchJob(runSpec, batchJob, gvk.Kind)
}

func validatePatchJob(runSpec *unstructured.Unstructured, job interface{}, jobType string) error {
Expand Down
19 changes: 19 additions & 0 deletions pkg/webhook/v1beta1/experiment/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,19 @@ spec:
t.Errorf("ConvertStringToUnstructured failed: %v", err)
}

notKubernetesBatchJob := `apiVersion: test/v1
kind: Job
spec:
template:
spec:
containers:
- name: container`

notKubernetesBatchJobUnstr, err := util.ConvertStringToUnstructured(notKubernetesBatchJob)
if err != nil {
t.Errorf("ConvertStringToUnstructured failed: %v", err)
}

tcs := []struct {
RunSpec *unstructured.Unstructured
Err bool
Expand All @@ -835,6 +848,12 @@ spec:
Err: false,
testDescription: "Valid case with nvidia.com/gpu resource in Trial template",
},
// Not kubernetes batch job
{
RunSpec: notKubernetesBatchJobUnstr,
Err: false,
testDescription: "Only validate Kuernetes Job",
},
}

for _, tc := range tcs {
Expand Down