-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarvin_k8s_job.go
208 lines (189 loc) · 5.25 KB
/
marvin_k8s_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package orchestrator
import (
"os"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
KindJob = "Job"
APIVersionV1 = "batch/v1"
)
var (
// TODO: Make these configurable
manualSelector = true
backoffLimit = int32(0)
activeDeadlineSeconds = int64(300)
shareProcessNamespace = true
uid = int64(1000)
gid = int64(3000)
fsGroup = int64(2000)
runAsNonRoot = true
allowPrivilegeEscalation = false
)
type MarvinK8sJob struct {
JobCreator
}
func (j *MarvinK8sJob) Job() (*batchv1.Job, error) {
job := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: APIVersionV1,
Kind: KindJob,
},
ObjectMeta: metav1.ObjectMeta{
Name: j.Name(),
Namespace: j.Namespace(),
Labels: j.JobLabels(),
},
Spec: batchv1.JobSpec{
ManualSelector: &manualSelector,
ActiveDeadlineSeconds: &activeDeadlineSeconds,
BackoffLimit: &backoffLimit,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"application": j.Name(),
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: j.PodLabels(),
},
Spec: corev1.PodSpec{
ShareProcessNamespace: &shareProcessNamespace,
Volumes: j.volumes(),
SecurityContext: j.podSecContext(),
NodeSelector: j.NodeSelector(),
ImagePullSecrets: j.imagePullSecrets(),
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{*j.container()},
},
},
},
}
initContainer := j.initContainer()
if initContainer != nil {
job.Spec.Template.Spec.InitContainers = []corev1.Container{*initContainer}
}
return job, nil
}
// Container returns a corev1.Container object from the IDriverJob.
func (j *MarvinK8sJob) container() *corev1.Container {
c := j.Container()
return &corev1.Container{
Name: c.Name,
Image: c.Image,
ImagePullPolicy: corev1.PullPolicy("Always"),
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse(c.Limit.CPU),
"memory": resource.MustParse(c.Limit.Memory),
},
},
Command: c.Cmd,
Args: c.Args,
Env: j.env(c),
VolumeMounts: j.mounts(c),
SecurityContext: j.conSecContext(),
}
}
// GetInitContainer returns a corev1.Container object from the IDriverJob.
func (j *MarvinK8sJob) initContainer() *corev1.Container {
c := j.InitContainer()
if c == nil {
return nil
}
return &corev1.Container{
Name: c.Name,
Image: c.Image,
ImagePullPolicy: corev1.PullPolicy("Always"),
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.MustParse(c.Limit.CPU),
"memory": resource.MustParse(c.Limit.Memory),
},
Requests: corev1.ResourceList{
"cpu": resource.MustParse(c.Requests.CPU),
"memory": resource.MustParse(c.Requests.Memory),
},
},
Command: c.Cmd,
Args: c.Args,
Env: j.env(c),
VolumeMounts: j.mounts(c),
SecurityContext: j.conSecContext(),
}
}
func (j *MarvinK8sJob) volumes() []corev1.Volume {
var volumes []corev1.Volume
for _, v := range j.Volumes() {
volumes = append(volumes, corev1.Volume{
Name: v,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
})
}
// Append the volume for mounting the service account for uploading/downloading
// artifacts from remote storage.
volumes = append(volumes, corev1.Volume{
Name: "credentialsdir",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: os.Getenv(EnvNameArtifactsSecretName),
},
},
})
return volumes
}
func (j *MarvinK8sJob) imagePullSecrets() []corev1.LocalObjectReference {
var imagePullSecrets []corev1.LocalObjectReference
for _, v := range j.ImagePullSecrets() {
imagePullSecrets = append(imagePullSecrets, corev1.LocalObjectReference{
Name: v,
})
}
return imagePullSecrets
}
func (*MarvinK8sJob) podSecContext() *corev1.PodSecurityContext {
return &corev1.PodSecurityContext{
RunAsUser: &uid,
RunAsGroup: &gid,
FSGroup: &fsGroup,
RunAsNonRoot: &runAsNonRoot,
}
}
func (*MarvinK8sJob) conSecContext() *corev1.SecurityContext {
return &corev1.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"all",
},
},
}
}
// env is a helper function to convert the IDriverJob's Env to corev1.EnvVars.
func (*MarvinK8sJob) env(c *Container) []corev1.EnvVar {
var env []corev1.EnvVar
for k, v := range c.Env {
env = append(env, corev1.EnvVar{Name: k, Value: v})
}
return env
}
// mounts is a helper function to convert the IDriverJob's VolumeMounts to
// corev1.VolumeMounts.
func (*MarvinK8sJob) mounts(c *Container) []corev1.VolumeMount {
var mounts []corev1.VolumeMount
for k, v := range c.VolumeMounts {
mounts = append(mounts, corev1.VolumeMount{
Name: k,
MountPath: v,
})
}
mounts = append(mounts, corev1.VolumeMount{
Name: "credentialsdir",
MountPath: "/credentials",
})
return mounts
}