-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
232 lines (220 loc) · 6.33 KB
/
tools.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 cronjob
import (
"fmt"
"time"
batchv1 "k8s.io/api/batch/v1"
)
// GetJobs get all jobs created by the cronjob.
func (h *Handler) GetJobs(object interface{}) ([]batchv1.Job, error) {
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getJobs(cj)
case *batchv1.CronJob:
return h.getJobs(val)
case batchv1.CronJob:
return h.getJobs(&val)
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getJobs(cj *batchv1.CronJob) ([]batchv1.Job, error) {
// list all job in the same namespace as the cronjob
listOptions := h.Options.ListOptions.DeepCopy()
listOptions.LabelSelector = ""
jobList, err := h.clientset.BatchV1().Jobs(h.namespace).List(h.ctx, *listOptions)
if err != nil {
return nil, err
}
var jl []batchv1.Job
for _, j := range jobList.Items {
for _, ownerRef := range j.OwnerReferences {
if ownerRef.Name == cj.Name {
jl = append(jl, j)
}
}
}
return jl, nil
}
// NumActive returns the number of running job created by cronjob.
func (h *Handler) NumActive(object interface{}) (int, error) {
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return 0, err
}
return len(cj.Status.Active), nil
case *batchv1.CronJob:
return len(val.Status.Active), nil
case batchv1.CronJob:
return len(val.Status.Active), nil
default:
return 0, ErrInvalidToolsType
}
}
// DurationOfLastScheduled returns the duration from last time the job successfully scheduled.
func (h *Handler) DurationOfLastScheduled(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return time.Duration(0), err
}
if cj.Status.LastScheduleTime == nil {
return time.Duration(0), fmt.Errorf("the last time the job was successfully scheduled not found")
}
return time.Now().Sub(cj.Status.LastScheduleTime.Time), nil
case *batchv1.CronJob:
if val.Status.LastScheduleTime == nil {
return time.Duration(0), fmt.Errorf("the last time the job was successfully scheduled not found")
}
return time.Now().Sub(val.Status.LastScheduleTime.Time), nil
case batchv1.CronJob:
if val.Status.LastScheduleTime == nil {
return time.Duration(0), fmt.Errorf("the last time the job was successfully scheduled not found")
}
return time.Now().Sub(val.Status.LastScheduleTime.Time), nil
default:
return time.Duration(0), ErrInvalidToolsType
}
}
// DurationOfCompleted returns the duration from last time the job successful completed.
func (h *Handler) DurationOfCompleted(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return time.Duration(0), err
}
if cj.Status.LastSuccessfulTime == nil {
return time.Duration(0), fmt.Errorf("the last time the job successfully completed not found")
}
return time.Now().Sub(cj.Status.LastSuccessfulTime.Time), nil
case *batchv1.CronJob:
if val.Status.LastSuccessfulTime == nil {
return time.Duration(0), fmt.Errorf("the last time the job successfully completed not found")
}
return time.Now().Sub(val.Status.LastSuccessfulTime.Time), nil
case batchv1.CronJob:
if val.Status.LastSuccessfulTime == nil {
return time.Duration(0), fmt.Errorf("the last time the job successfully completed not found")
}
return time.Now().Sub(val.Status.LastSuccessfulTime.Time), nil
default:
return time.Duration(0), ErrInvalidToolsType
}
}
// GetSchedule returns the schedule in Cron format.
func (h *Handler) GetSchedule(object interface{}) (string, error) {
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return "", err
}
return cj.Spec.Schedule, nil
case *batchv1.CronJob:
return val.Spec.Schedule, nil
case batchv1.CronJob:
return val.Spec.Schedule, nil
default:
return "", ErrInvalidToolsType
}
}
// IsSuspend check whether the controller will suspend subsequent executions.
func (h *Handler) IsSuspend(object interface{}) (bool, error) {
// TODO: 当 suspend 没有设置时,是设置成默认值还是返回 "not set" 类型的错误
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return false, err
}
// if suspend field not set, set suspend to false.
if cj.Spec.Suspend == nil {
return false, nil
}
return *cj.Spec.Suspend, nil
case *batchv1.CronJob:
if val.Spec.Suspend == nil {
return false, nil
}
return *val.Spec.Suspend, nil
case batchv1.CronJob:
if val.Spec.Suspend == nil {
return false, nil
}
return *val.Spec.Suspend, nil
default:
return false, ErrInvalidToolsType
}
}
// GetAge returns cronjob age.
func (h *Handler) GetAge(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
cj, err := h.Get(val)
if err != nil {
return time.Duration(0), err
}
return time.Now().Sub(cj.CreationTimestamp.Time), nil
case *batchv1.CronJob:
return time.Now().Sub(val.CreationTimestamp.Time), nil
case batchv1.CronJob:
return time.Now().Sub(val.CreationTimestamp.Time), nil
default:
return time.Duration(0), ErrInvalidToolsType
}
}
// GetContainers get all container of this cronjob.
func (h *Handler) GetContainers(object interface{}) ([]string, error) {
switch val := object.(type) {
case string:
sts, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getContainers(sts), nil
case *batchv1.CronJob:
return h.getContainers(val), nil
case batchv1.CronJob:
return h.getContainers(&val), nil
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getContainers(sts *batchv1.CronJob) []string {
var cl []string
for _, container := range sts.Spec.JobTemplate.Spec.Template.Spec.Containers {
cl = append(cl, container.Name)
}
return cl
}
// GetImages get all container images of this cronjob.
func (h *Handler) GetImages(object interface{}) ([]string, error) {
switch val := object.(type) {
case string:
sts, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getImages(sts), nil
case *batchv1.CronJob:
return h.getImages(val), nil
case batchv1.CronJob:
return h.getImages(&val), nil
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getImages(sts *batchv1.CronJob) []string {
var il []string
for _, container := range sts.Spec.JobTemplate.Spec.Template.Spec.Containers {
il = append(il, container.Image)
}
return il
}