/
job.go
154 lines (141 loc) · 4.25 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
package periodic
import (
"context"
"fmt"
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"
apiWatch "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/watch"
)
// JobController type responsible for
// creating a new gathering jobs
type JobController struct {
kubeClient kubernetes.Interface
}
func NewJobController(kubeClient kubernetes.Interface) *JobController {
return &JobController{
kubeClient: kubeClient,
}
}
// CreateGathererJob creates a new Kubernetes Job with provided image, volume mount path used for storing data archives and name
// derived from the provided data gather name
func (j *JobController) CreateGathererJob(ctx context.Context, dataGatherName, image, archiveVolumeMountPath string) (*batchv1.Job, error) {
gj := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: dataGatherName,
Namespace: insightsNamespace,
},
Spec: batchv1.JobSpec{
// backoff limit is 0 - we dont' want to restart the gathering immediately in case of failure
BackoffLimit: new(int32),
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
PriorityClassName: "system-cluster-critical",
RestartPolicy: corev1.RestartPolicyNever,
ServiceAccountName: "operator",
SecurityContext: &corev1.PodSecurityContext{
RunAsNonRoot: &trueB,
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
Volumes: []corev1.Volume{
{
Name: "archives-path",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: serviceCABundle,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: serviceCABundle,
},
Optional: &trueB,
},
},
},
},
Containers: []corev1.Container{
{
Name: "insights-gathering",
Image: image,
Args: []string{"gather-and-upload", "-v=4", "--config=/etc/insights-operator/server.yaml"},
Env: []corev1.EnvVar{
{
Name: "DATAGATHER_NAME",
Value: dataGatherName,
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("70Mi"),
},
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: falseB,
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "archives-path",
MountPath: archiveVolumeMountPath,
},
{
Name: serviceCABundle,
MountPath: serviceCABundlePath,
},
},
},
},
},
},
},
}
return j.kubeClient.BatchV1().Jobs(insightsNamespace).Create(ctx, gj, metav1.CreateOptions{})
}
// WaitForJobCompletion listen the Kubernetes events to check if job finished.
func (j *JobController) WaitForJobCompletion(ctx context.Context, job *batchv1.Job) error {
watcherFnc := func(options metav1.ListOptions) (apiWatch.Interface, error) {
return j.kubeClient.BatchV1().Jobs(insightsNamespace).
Watch(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("metadata.name=%s", job.Name)})
}
retryWatcher, err := watch.NewRetryWatcher(job.ResourceVersion, &cache.ListWatch{WatchFunc: watcherFnc})
if err != nil {
return err
}
defer retryWatcher.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case event, ok := <-retryWatcher.ResultChan():
if !ok {
return fmt.Errorf("watcher channel was closed unexpectedly")
}
if event.Type == apiWatch.Deleted {
return nil
}
if event.Type != apiWatch.Modified {
continue
}
job, ok := event.Object.(*batchv1.Job)
if !ok {
return fmt.Errorf("failed to cast job event: %v", event.Object)
}
if job.Status.Succeeded > 0 {
return nil
}
if job.Status.Failed > 0 {
return fmt.Errorf("job %s failed", job.Name)
}
}
}
}