/
jobs.go
67 lines (55 loc) · 1.68 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
package utils
import (
"crypto/md5"
"encoding/hex"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
)
// getJobConditionStatus gets the status of the condition in the job. If the
// condition is not found in the job, then returns False.
func getJobConditionStatus(job *batchv1.Job, conditionType batchv1.JobConditionType) corev1.ConditionStatus {
for _, condition := range job.Status.Conditions {
if condition.Type == conditionType {
return condition.Status
}
}
return corev1.ConditionFalse
}
// IsSuccessful returns true if the job was successful
func IsSuccessful(job *batchv1.Job) bool {
return getJobConditionStatus(job, batchv1.JobComplete) == corev1.ConditionTrue
}
// IsFailed returns true if the job failed
func IsFailed(job *batchv1.Job) bool {
return getJobConditionStatus(job, batchv1.JobFailed) == corev1.ConditionTrue
}
// IsDeadlineExceeded returns true if the job failed due to deadline being exceeded
func IsDeadlineExceeded(job *batchv1.Job) bool {
if !IsFailed(job) {
return false
}
for _, condition := range job.Status.Conditions {
if condition.Type == batchv1.JobFailed {
return condition.Reason == "DeadlineExceeded"
}
}
return false
}
// IsFinished returns true if the job completed (succeeded or failed)
func IsFinished(job *batchv1.Job) bool {
return IsSuccessful(job) || IsFailed(job)
}
// CalculateJobSpecHash returns a hash of the job.Spec.
func CalculateJobSpecHash(job *batchv1.Job) (string, error) {
hasher := md5.New()
jobSpecBytes, err := job.Spec.Marshal()
if err != nil {
return "", err
}
_, err = hasher.Write(jobSpecBytes)
if err != nil {
return "", err
}
sum := hex.EncodeToString(hasher.Sum(nil))
return sum, nil
}