-
Notifications
You must be signed in to change notification settings - Fork 2
/
job.go
165 lines (140 loc) · 3.96 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
package main
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)
var (
errJobNotCreated = errors.New("job not created")
errJobNotFound = errors.New("job not found")
errPodNotFound = errors.New("pod not found")
errLogsNotFound = errors.New("pod logs not found")
)
type logger interface {
Debugf(msg string, args ...interface{})
Errorf(msg string, args ...interface{})
Fatalf(msg string, args ...interface{})
Warningf(msg string, args ...interface{})
}
type jobClient interface {
Create(*v1.Job) (*v1.Job, error)
Get(name string, options metav1.GetOptions) (*v1.Job, error)
}
type podClient interface {
GetLogs(name string, opts *corev1.PodLogOptions) *rest.Request
List(opts metav1.ListOptions) (*corev1.PodList, error)
}
type JobRunner struct {
jc jobClient
pc podClient
pollInterval time.Duration
log logger
}
func NewJobRunner(jc jobClient, pc podClient, pollInterval time.Duration, log logger) JobRunner {
return JobRunner{
jc: jc,
pc: pc,
pollInterval: pollInterval,
log: log,
}
}
func (j *JobRunner) RunJob(ctx context.Context, jobPrefix, namespace, image string) (string, error) {
job, err := j.jc.Create(&v1.Job{
TypeMeta: metav1.TypeMeta{
Kind: "Job",
APIVersion: "batch/v1",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", jobPrefix),
Namespace: namespace,
},
Spec: v1.JobSpec{
BackoffLimit: intPtr(0),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-pod", jobPrefix),
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: fmt.Sprintf("%s-con", jobPrefix),
Image: image,
},
},
},
},
},
})
if err != nil {
return "", errors.Wrapf(errJobNotCreated, "error starting job: %v", err)
}
err = j.pollJobStatus(ctx, job.GetName())
if err != nil {
if errors.Is(err, errJobNotFound) {
return "", err
}
logString, _ := j.getLogs(ctx, job.GetName())
return logString, err
}
return j.getLogs(ctx, job.GetName())
}
func (j *JobRunner) pollJobStatus(ctx context.Context, jobName string) error {
ticker := time.NewTicker(j.pollInterval)
for {
select {
case <-ticker.C:
job, err := j.getJob(ctx, jobName)
if err != nil {
return errors.Wrapf(errJobNotFound, "could not find job: %v", err)
}
if job != nil {
if i := findCondition(job.Status.Conditions, v1.JobFailed); i > -1 {
return errors.Errorf("job failed with %s: %s", job.Status.Conditions[i].Reason, job.Status.Conditions[i].Message)
}
if i := findCondition(job.Status.Conditions, v1.JobComplete); i > -1 {
return nil
}
}
j.log.Debugf("job running, awaiting status")
case <-ctx.Done():
ticker.Stop()
return errors.New("job exceeded timeout limit")
}
}
}
func (j *JobRunner) getJob(ctx context.Context, jobName string) (*v1.Job, error) {
return j.jc.Get(jobName, metav1.GetOptions{})
}
func (j *JobRunner) getLogs(ctx context.Context, jobName string) (string, error) {
pods, err := j.pc.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("job-name=%s", jobName),
})
if err != nil {
return "", errors.Wrapf(errPodNotFound, "could not find pod: %v", err)
}
// Need to figure out how to handle jobs where
// multiple pods are created because of backoffPolicy
podName := pods.Items[0].GetName()
logBytes, err := j.pc.GetLogs(podName, &corev1.PodLogOptions{}).DoRaw()
if err != nil {
return "", errors.Wrapf(errLogsNotFound, "could not read logs: %v", err)
}
return string(logBytes), err
}
func findCondition(conditions []v1.JobCondition, condition v1.JobConditionType) int {
for i, c := range conditions {
if c.Type == condition && c.Status == corev1.ConditionTrue {
return i
}
}
return -1
}
func intPtr(i int32) *int32 {
return &i
}