/
jobs.go
79 lines (72 loc) · 2.16 KB
/
jobs.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
package generators
import (
"bytes"
"fmt"
"github.com/google/uuid"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
// DefaultRequiredCompletionsForJobs indicates the number of Pod
// completions that will be required by default for any generated
// Job resources.
DefaultRequiredCompletionsForJobs int32 = 1
)
// GenerateBashJob will create a ConfigMap and a Job that can be deployed to run
// a bash script given the container image you want the job to run and the shell
// commands you want the job to execute as arguments. By default this will only
// generate a single container, and you can provide env vars to be loaded into it.
func GenerateBashJob(image, imageTag string, commands ...string) (*corev1.ConfigMap, *batchv1.Job) {
// build the job script
script := new(bytes.Buffer)
script.WriteString("#!/bin/bash\n")
script.WriteString("set -euox pipefail\n")
for _, command := range commands {
script.WriteString(fmt.Sprintf("%s\n", command))
}
// configure the script in a configmap for mounting
jobName := uuid.New().String()
cfgmap := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
},
Data: map[string]string{
jobName: script.String(),
},
}
// build the job itself
mountPath := fmt.Sprintf("/opt/%s", jobName)
job := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: jobName,
Image: fmt.Sprintf("%s:%s", image, imageTag),
Command: []string{"/bin/bash", mountPath},
VolumeMounts: []corev1.VolumeMount{{
Name: jobName,
MountPath: mountPath,
SubPath: jobName,
}},
}},
Volumes: []corev1.Volume{{
Name: jobName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: jobName},
},
},
}},
RestartPolicy: corev1.RestartPolicyOnFailure,
},
},
Completions: &DefaultRequiredCompletionsForJobs,
},
}
return &cfgmap, &job
}