-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
176 lines (155 loc) · 4.15 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
package main
import (
"context"
log "github.com/sirupsen/logrus"
funk "github.com/thoas/go-funk"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
watch "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
var jobPermittedStates = []ResourceState{ResourceComplete, ResourceFailed, ResourceRunning}
type JobMatcher struct {
clientset kubernetes.Interface
watcher watch.Interface
description StateDescription
done chan bool
jobstate map[string]ResourceState
}
type JobValidator struct {
BaseValidator
}
func (v *JobValidator) Validate(ctx context.Context, description StateDescription) error {
err := v.BaseValidator.Validate(ctx, description)
if err != nil {
return err
}
for _, requiredState := range description.RequiredStates {
if !funk.Contains(jobPermittedStates, requiredState) {
return ErrStateNotValidForResourceType(description, requiredState)
}
}
return nil
}
func NewJobValidator() Validator {
return &JobValidator{}
}
func NewJobMatcher(clientset kubernetes.Interface, description StateDescription) Matcher {
return &JobMatcher{
clientset: clientset,
watcher: nil,
description: description,
done: make(chan bool, 1),
jobstate: make(map[string]ResourceState),
}
}
func (m *JobMatcher) Start(ctx context.Context) error {
options := metav1.ListOptions{
LabelSelector: m.description.LabelSelector,
}
log.WithFields(log.Fields{
"namespace": m.description.Namespace,
"type": m.description.Type,
"labelselector": m.description.LabelSelector,
}).Debug("fetching initial context")
jobs, err := m.clientset.BatchV1().Jobs(m.description.Namespace).List(options)
if err != nil {
return err
}
for _, job := range jobs.Items {
state := getJobResourceState(&job)
m.jobstate[job.Name] = state
log.WithFields(log.Fields{
"jobName": job.Name,
"jobState": state,
}).Debug("added to jobstate")
}
if MatchStateMap(m.jobstate, m.description.RequiredStates) {
return nil
}
m.watcher, err = m.clientset.BatchV1().Jobs(m.description.Namespace).Watch(options)
if err != nil {
return err
}
log.Debug("watching for updates")
for event := range m.watcher.ResultChan() {
ctxLogger := log.WithFields(log.Fields{
"eventType": event.Type,
})
switch event.Type {
case watch.Added:
job := event.Object.(*batchv1.Job)
state := getJobResourceState(job)
m.jobstate[job.Name] = state
ctxLogger.WithFields(log.Fields{
"jobName": job.Name,
"jobState": state,
}).Debug("added to job state")
case watch.Modified:
job := event.Object.(*batchv1.Job)
state := getJobResourceState(job)
m.jobstate[job.Name] = state
ctxLogger.WithFields(log.Fields{
"jobName": job.Name,
"jobState": state,
}).Debug("updated job state")
case watch.Deleted:
job := event.Object.(*batchv1.Job)
_, ok := m.jobstate[job.Name]
if ok {
ctxLogger.WithFields(log.Fields{
"jobName": job.Name,
}).Debug("deleted from job state")
}
case watch.Error:
// TODO: do something with this error
}
if MatchStateMap(m.jobstate, m.description.RequiredStates) {
log.Info("state description matched by cluster")
select {
case <-m.done:
default:
close(m.done)
}
break
}
}
return nil
}
func (m *JobMatcher) Done() <-chan bool {
return m.done
}
func (m *JobMatcher) Stop(ctx context.Context) error {
defer func() {
select {
case <-m.done:
default:
close(m.done)
}
}()
if m.watcher != nil {
m.watcher.Stop()
}
return nil
}
func getJobResourceState(job *batchv1.Job) ResourceState {
for _, condition := range job.Status.Conditions {
// An explicit check is added for JobFailed to allow for addition
// of more job conditions in future Kubernetes releases
if condition.Type == batchv1.JobComplete {
if condition.Status == v1.ConditionTrue {
return ResourceComplete
}
} else if condition.Type == batchv1.JobFailed {
if condition.Status == v1.ConditionTrue {
return ResourceFailed
}
}
}
// check if any containers are active in the pod to check if pod is running
if job.Status.Active != 0 {
return ResourceRunning
}
return resourceWaiting
}