/
gang_job.go
149 lines (125 loc) · 4.3 KB
/
gang_job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 k8s
import (
"encoding/json"
"strconv"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/apache/yunikorn-k8shim/pkg/cache"
"github.com/apache/yunikorn-k8shim/pkg/common/constants"
"github.com/apache/yunikorn-k8shim/pkg/common/utils"
)
type TestJobConfig struct {
JobName string
Parallelism int32
Completions int32
}
func InitTestJob(jobName string, parallelism, completions int32, pod *v1.Pod) *batchv1.Job {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
},
Spec: batchv1.JobSpec{
Parallelism: ¶llelism,
Completions: &completions,
Template: v1.PodTemplateSpec{
Spec: pod.Spec,
ObjectMeta: pod.ObjectMeta,
},
},
}
return job
}
func getGangSchedulingAnnotations(placeholderTimeout int,
schedulingStyle string,
taskGroupName string,
taskGroups []*cache.TaskGroup) map[string]string {
annotations := make(map[string]string)
var schedulingParams string
if placeholderTimeout != 0 {
schedulingParams = "placeholderTimeoutInSeconds=" + strconv.Itoa(placeholderTimeout)
}
if schedulingStyle != "" {
if schedulingParams != "" {
schedulingParams += " gangSchedulingStyle=" + schedulingStyle
} else {
schedulingParams = "gangSchedulingStyle" + schedulingStyle
}
}
if schedulingParams != "" {
annotations[constants.DomainYuniKorn+"schedulingPolicyParameters"] = schedulingParams
}
annotations[constants.DomainYuniKorn+"task-group-name"] = taskGroupName
taskGroupJSON, err := json.Marshal(taskGroups)
if err != nil {
panic("Unable to marshal taskGroups")
}
annotations[constants.DomainYuniKorn+"task-groups"] = string(taskGroupJSON)
return annotations
}
func DecoratePodForGangScheduling(
placeholderTimeout int,
schedulingStyle string,
taskGroupName string,
taskGroups []*cache.TaskGroup,
pod *v1.Pod) *v1.Pod {
gangSchedulingAnnotations := getGangSchedulingAnnotations(placeholderTimeout, schedulingStyle, taskGroupName, taskGroups)
pod.Annotations = utils.MergeMaps(pod.Annotations, gangSchedulingAnnotations)
return pod
}
func InitTaskGroups(conf SleepPodConfig, mainTaskGroupName, secondTaskGroupName string, parallelism int) []*cache.TaskGroup {
tg1 := &cache.TaskGroup{
MinMember: int32(parallelism),
Name: mainTaskGroupName,
MinResource: map[string]resource.Quantity{
"cpu": resource.MustParse(strconv.FormatInt(conf.CPU, 10) + "m"),
"memory": resource.MustParse(strconv.FormatInt(conf.Mem, 10) + "M"),
},
}
// create TG2 more with more members than needed, also make sure that
// placeholders will stay in Pending state
tg2 := &cache.TaskGroup{
MinMember: int32(parallelism + 1),
Name: secondTaskGroupName,
MinResource: map[string]resource.Quantity{
"cpu": resource.MustParse(strconv.FormatInt(conf.CPU, 10) + "m"),
"memory": resource.MustParse(strconv.FormatInt(conf.Mem, 10) + "M"),
},
NodeSelector: map[string]string{
"kubernetes.io/hostname": "nonexistingnode",
},
}
tGroups := make([]*cache.TaskGroup, 2)
tGroups[0] = tg1
tGroups[1] = tg2
return tGroups
}
func InitTaskGroup(conf SleepPodConfig, taskGroupName string, parallelism int32) []*cache.TaskGroup {
tg1 := &cache.TaskGroup{
MinMember: parallelism,
Name: taskGroupName,
MinResource: map[string]resource.Quantity{
"cpu": resource.MustParse(strconv.FormatInt(conf.CPU, 10) + "m"),
"memory": resource.MustParse(strconv.FormatInt(conf.Mem, 10) + "M"),
},
}
tGroups := make([]*cache.TaskGroup, 1)
tGroups[0] = tg1
return tGroups
}