This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathstatus.go
124 lines (105 loc) · 4.03 KB
/
status.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
package util
import (
apiv1 "github.com/kubeflow/common/pkg/apis/common/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// JobCreatedReason is added in a job when it is created.
JobCreatedReason = "JobCreated"
// JobSucceededReason is added in a job when it is succeeded.
JobSucceededReason = "JobSucceeded"
// JobRunningReason is added in a job when it is running.
JobRunningReason = "JobRunning"
// JobFailedReason is added in a job when it is failed.
JobFailedReason = "JobFailed"
// JobRestartingReason is added in a job when it is restarting.
JobRestartingReason = "JobRestarting"
// JobFailedValidationReason is added in a job when it failed validation
JobFailedValidationReason = "JobFailedValidation"
// labels for pods and servers.
)
// IsSucceeded checks if the job is succeeded
func IsSucceeded(status apiv1.JobStatus) bool {
return hasCondition(status, apiv1.JobSucceeded)
}
// IsFailed checks if the job is failed
func IsFailed(status apiv1.JobStatus) bool {
return hasCondition(status, apiv1.JobFailed)
}
// UpdateJobConditions adds to the jobStatus a new condition if needed, with the conditionType, reason, and message
func UpdateJobConditions(jobStatus *apiv1.JobStatus, conditionType apiv1.JobConditionType, reason, message string) error {
condition := newCondition(conditionType, reason, message)
setCondition(jobStatus, condition)
return nil
}
func hasCondition(status apiv1.JobStatus, condType apiv1.JobConditionType) bool {
for _, condition := range status.Conditions {
if condition.Type == condType && condition.Status == v1.ConditionTrue {
return true
}
}
return false
}
// newCondition creates a new job condition.
func newCondition(conditionType apiv1.JobConditionType, reason, message string) apiv1.JobCondition {
return apiv1.JobCondition{
Type: conditionType,
Status: v1.ConditionTrue,
LastUpdateTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: message,
}
}
// getCondition returns the condition with the provided type.
func getCondition(status apiv1.JobStatus, condType apiv1.JobConditionType) *apiv1.JobCondition {
for _, condition := range status.Conditions {
if condition.Type == condType {
return &condition
}
}
return nil
}
// setCondition updates the job to include the provided condition.
// If the condition that we are about to add already exists
// and has the same status and reason then we are not going to update.
func setCondition(status *apiv1.JobStatus, condition apiv1.JobCondition) {
// Do nothing if JobStatus have failed condition
if IsFailed(*status) {
return
}
currentCond := getCondition(*status, condition.Type)
// Do nothing if condition doesn't change
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
return
}
// Do not update lastTransitionTime if the status of the condition doesn't change.
if currentCond != nil && currentCond.Status == condition.Status {
condition.LastTransitionTime = currentCond.LastTransitionTime
}
// Append the updated condition to the conditions
newConditions := filterOutCondition(status.Conditions, condition.Type)
status.Conditions = append(newConditions, condition)
}
// filterOutCondition returns a new slice of job conditions without conditions with the provided type.
func filterOutCondition(conditions []apiv1.JobCondition, condType apiv1.JobConditionType) []apiv1.JobCondition {
var newConditions []apiv1.JobCondition
for _, c := range conditions {
if condType == apiv1.JobRestarting && c.Type == apiv1.JobRunning {
continue
}
if condType == apiv1.JobRunning && c.Type == apiv1.JobRestarting {
continue
}
if c.Type == condType {
continue
}
// Set the running condition status to be false when current condition failed or succeeded
if (condType == apiv1.JobFailed || condType == apiv1.JobSucceeded) && c.Type == apiv1.JobRunning {
c.Status = v1.ConditionFalse
}
newConditions = append(newConditions, c)
}
return newConditions
}