forked from argoproj/gitops-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_job.go
73 lines (68 loc) · 1.7 KB
/
health_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
package health
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"github.com/STollenaar/gitops-engine/pkg/utils/kube"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func getJobHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
gvk := obj.GroupVersionKind()
switch gvk {
case batchv1.SchemeGroupVersion.WithKind(kube.JobKind):
var job batchv1.Job
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &job)
if err != nil {
return nil, fmt.Errorf("failed to convert unstructured Job to typed: %v", err)
}
return getBatchv1JobHealth(&job)
default:
return nil, fmt.Errorf("unsupported Job GVK: %s", gvk)
}
}
func getBatchv1JobHealth(job *batchv1.Job) (*HealthStatus, error) {
failed := false
var failMsg string
complete := false
var message string
isSuspended := false
for _, condition := range job.Status.Conditions {
switch condition.Type {
case batchv1.JobFailed:
failed = true
complete = true
failMsg = condition.Message
case batchv1.JobComplete:
complete = true
message = condition.Message
case batchv1.JobSuspended:
complete = true
message = condition.Message
if condition.Status == corev1.ConditionTrue {
isSuspended = true
}
}
}
if !complete {
return &HealthStatus{
Status: HealthStatusProgressing,
Message: message,
}, nil
} else if failed {
return &HealthStatus{
Status: HealthStatusDegraded,
Message: failMsg,
}, nil
} else if isSuspended {
return &HealthStatus{
Status: HealthStatusSuspended,
Message: failMsg,
}, nil
} else {
return &HealthStatus{
Status: HealthStatusHealthy,
Message: message,
}, nil
}
}