Skip to content

Commit

Permalink
Support job level priorityClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLike committed May 16, 2019
1 parent 124d38d commit 91f4c3a
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 10 deletions.
16 changes: 16 additions & 0 deletions pkg/admission/admit_job_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package admission

import (
Expand Down
33 changes: 23 additions & 10 deletions pkg/admission/mutate_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,41 @@ func MutateJobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {

func createPatch(job v1alpha1.Job) ([]byte, error) {
var patch []patchOperation
patch = append(patch, mutateSpec(job.Spec.Tasks, "/spec/tasks")...)
pathQueue := patchDefaultQueue(job)
if pathQueue != nil {
patch = append(patch, *pathQueue)
}
pathSpec := mutateSpec(job.Spec.Tasks, "/spec/tasks", job.Spec.PriorityClassName)
if pathSpec != nil {
patch = append(patch, *pathSpec)
}
return json.Marshal(patch)
}

func patchDefaultQueue(job v1alpha1.Job) *patchOperation {
//Add default queue if not specified.
if job.Spec.Queue == "" {
patch = append(patch, patchOperation{Op: "add", Path: "/spec/queue", Value: DefaultQueue})
return &patchOperation{Op: "add", Path: "/spec/queue", Value: DefaultQueue}
}

return json.Marshal(patch)
return nil
}

func mutateSpec(tasks []v1alpha1.TaskSpec, basePath string) (patch []patchOperation) {
for index := range tasks {
func mutateSpec(tasks []v1alpha1.TaskSpec, basePath, priorityClass string) *patchOperation {
for index, task := range tasks {
// add default task name
taskName := tasks[index].Name
if len(taskName) == 0 {
tasks[index].Name = v1alpha1.DefaultTaskSpec + strconv.Itoa(index)
}
//We patch the priorityClassName here in admission service, since there would be more
//than one places that will behavior depends on task/pod's priorityClassName.
if len(priorityClass) != 0 && len(task.Template.Spec.PriorityClassName) == 0 {
tasks[index].Template.Spec.PriorityClassName = priorityClass
}
}
patch = append(patch, patchOperation{
return &patchOperation{
Op: "replace",
Path: basePath,
Value: tasks,
})

return patch
}
}
145 changes: 145 additions & 0 deletions pkg/admission/mutate_job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package admission

import (
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
"volcano.sh/volcano/pkg/apis/batch/v1alpha1"
)

func TestCreatePatchExecution(t *testing.T) {

namespace := "test"
jobPriority := "job_priority"

testCase := struct {
Name string
Job v1alpha1.Job
operation patchOperation
}{
Name: "patch default task name & priority class",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "path-task-name",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Tasks: []v1alpha1.TaskSpec{
{
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
PriorityClassName: "task_priority",
},
},
},
{
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
},
},
operation: patchOperation{
Op: "replace",
Path: "/spec/tasks",
Value: []v1alpha1.TaskSpec{
{
Name: v1alpha1.DefaultTaskSpec + "0",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
PriorityClassName: "task_priority",
},
},
},
{
Name: v1alpha1.DefaultTaskSpec + "1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
PriorityClassName: jobPriority,
},
},
},
},
},
}

ret := mutateSpec(testCase.Job.Spec.Tasks, "/spec/tasks", jobPriority)
if ret.Path != testCase.operation.Path || ret.Op != testCase.operation.Op {
t.Errorf("testCase %s's expected patch operation %v, but got %v",
testCase.Name, testCase.operation, *ret)
}

actualTasks, ok := ret.Value.([]v1alpha1.TaskSpec)
if !ok {
t.Errorf("testCase '%s' path value expected to be '[]v1alpha1.TaskSpec', but negative",
testCase.Name)
}
expectedTasks, _ := testCase.operation.Value.([]v1alpha1.TaskSpec)
for index, task := range expectedTasks {
aTask := actualTasks[index]
if aTask.Name != task.Name || aTask.Template.Spec.PriorityClassName != task.Template.Spec.PriorityClassName {
t.Errorf("testCase '%s's expected patch operation with value %v, but got %v",
testCase.Name, testCase.operation.Value, ret.Value)
}
}

}
5 changes: 5 additions & 0 deletions pkg/apis/batch/v1alpha1/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ type JobSpec struct {
// the Job becomes eligible to be deleted immediately after it finishes.
// +optional
TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty" protobuf:"varint,9,opt,name=ttlSecondsAfterFinished"`

// If specified, indicates the job's priority. This value will be overwritten by 'PriorityClassName'
// defined in task/pod spec.
// +optional
PriorityClassName string `json:"priorityClassName,omitempty" protobuf:"bytes,10,opt,name=priorityClassName"`
}

// VolumeSpec defines the specification of Volume, e.g. PVC
Expand Down

0 comments on commit 91f4c3a

Please sign in to comment.