-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
232 lines (189 loc) · 5.9 KB
/
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package controller
import (
"context"
"fmt"
"strings"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
defaultv1alpha1 "github.com/CHORUS-TRE/workbench-operator/api/v1alpha1"
)
// Default resource requirements for workbench applications
var defaultResources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("750m"),
corev1.ResourceMemory: resource.MustParse("768Mi"),
corev1.ResourceEphemeralStorage: resource.MustParse("10Gi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("512Mi"),
corev1.ResourceEphemeralStorage: resource.MustParse("1Gi"),
},
}
func initJob(workbench defaultv1alpha1.Workbench, config Config, index int, app defaultv1alpha1.WorkbenchApp, service corev1.Service) *batchv1.Job {
job := &batchv1.Job{}
// The name of the app is there for human consumption.
job.Name = fmt.Sprintf("%s-%d-%s", workbench.Name, index, app.Name)
job.Namespace = workbench.Namespace
labels := map[string]string{
matchingLabel: workbench.Name,
}
job.Labels = labels
var shmDir *corev1.Volume
if app.ShmSize != nil {
shmDir = &corev1.Volume{
Name: "shm",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: "Memory",
SizeLimit: app.ShmSize,
},
},
}
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, *shmDir)
}
// The pod will be cleaned up after a day.
// https://kubernetes.io/docs/concepts/workloads/controllers/job/#ttl-mechanism-for-finished-jobs
oneDay := int32(24 * 3600)
job.Spec.TTLSecondsAfterFinished = &oneDay
// Service account is an alternative to the image Pull Secrets
serviceAccountName := workbench.Spec.ServiceAccount
if serviceAccountName != "" {
job.Spec.Template.Spec.ServiceAccountName = serviceAccountName
}
var appImage string
imagePullPolicy := corev1.PullIfNotPresent
if app.Image == nil {
// Fix empty version
appVersion := app.Version
if appVersion == "" {
appVersion = "latest"
imagePullPolicy = corev1.PullAlways
}
// Non-empty registry requires a / to concatenate with the app one.
registry := config.Registry
if registry != "" {
registry = strings.TrimRight(registry, "/") + "/"
}
appsRepository := config.AppsRepository
if appsRepository != "" {
appsRepository = strings.Trim(appsRepository, "/") + "/"
}
appImage = fmt.Sprintf("%s%s%s:%s", registry, appsRepository, app.Name, appVersion)
} else {
// Fix empty version
appVersion := app.Image.Tag
if appVersion == "" {
appVersion = "latest"
imagePullPolicy = corev1.PullAlways
}
appImage = fmt.Sprintf("%s/%s:%s", app.Image.Registry, app.Image.Repository, appVersion)
}
appContainer := corev1.Container{
Name: app.Name,
Image: appImage,
ImagePullPolicy: imagePullPolicy,
Resources: defaultResources, // Set default resources
Env: []corev1.EnvVar{
{
Name: "DISPLAY",
Value: fmt.Sprintf("%s.%s:80", service.Name, service.Namespace), // FIXME: 80 from 6080
},
},
}
// Override with custom resources if specified
if app.Resources != nil {
appContainer.Resources = *app.Resources
}
// Mounting the /dev/shm volume.
if shmDir != nil {
appContainer.VolumeMounts = append(appContainer.VolumeMounts, corev1.VolumeMount{
Name: shmDir.Name,
MountPath: "/dev/shm",
})
}
job.Spec.Template.Spec.Containers = []corev1.Container{
appContainer,
}
for _, imagePullSecret := range workbench.Spec.ImagePullSecrets {
job.Spec.Template.Spec.ImagePullSecrets = append(job.Spec.Template.Spec.ImagePullSecrets, corev1.LocalObjectReference{
Name: imagePullSecret,
})
}
// Hide the pod name in favour of the app name.
job.Spec.Template.Spec.Hostname = app.Name
// This allows the end user to stop the application from within Xpra.
job.Spec.Template.Spec.RestartPolicy = "OnFailure"
appState := app.State
if appState == "" {
appState = "Running"
}
if appState != "Running" {
tru := true
job.Spec.Suspend = &tru
} else {
fal := false
job.Spec.Suspend = &fal
}
return job
}
// updateJob makes the destination batch Job (app), like the source one.
//
// It's not allowed to modify the Job definition outside of suspending it.
func updateJob(source batchv1.Job, destination *batchv1.Job) bool {
updated := false
suspend := source.Spec.Suspend
if suspend != nil && (destination.Spec.Suspend == nil || *destination.Spec.Suspend != *suspend) {
destination.Spec.Suspend = suspend
updated = true
}
return updated
}
func (r *WorkbenchReconciler) findJobs(ctx context.Context, workbench defaultv1alpha1.Workbench) (*batchv1.JobList, error) {
jobList := batchv1.JobList{}
err := r.List(
ctx,
&jobList,
client.MatchingLabels{
matchingLabel: workbench.Name,
},
)
return &jobList, err
}
// Delete the given job.
func (r *WorkbenchReconciler) deleteJob(ctx context.Context, job *batchv1.Job) error {
log := log.FromContext(ctx)
log.V(1).Info("Delete a job", "job", job.Name)
return r.Delete(
ctx,
job,
client.PropagationPolicy("Background"),
)
}
// Delete all the jobs of the given workbench.
func (r *WorkbenchReconciler) deleteJobs(ctx context.Context, workbench defaultv1alpha1.Workbench) (int, error) {
log := log.FromContext(ctx)
// Find all the jobs linked with the workbench.
jobList, err := r.findJobs(ctx, workbench)
if err != nil {
return 0, err
}
// Done.
if len(jobList.Items) == 0 {
return 0, nil
}
log.V(1).Info("Delete all jobs")
if err := r.DeleteAllOf(
ctx,
&batchv1.Job{},
client.InNamespace(workbench.Namespace),
client.PropagationPolicy("Background"),
client.MatchingLabels{matchingLabel: workbench.Name},
); err != nil {
return 0, err
}
return len(jobList.Items), nil
}