-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcr-status.go
155 lines (145 loc) · 5.54 KB
/
cr-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
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
package sdk
import (
v1 "github.com/openshift/custom-resource-status/conditions/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"kubevirt.io/controller-lifecycle-operator-sdk/api"
)
// IsUpgrading checks whether cr status represents upgrade in progress
func IsUpgrading(crStatus *api.Status) bool {
deploying := crStatus.Phase == api.PhaseDeploying
return (crStatus.ObservedVersion != "" || !deploying) && crStatus.ObservedVersion != crStatus.TargetVersion
}
// GetConditionValues gets the conditions and put them into a map for easy comparison
func GetConditionValues(conditionList []v1.Condition) map[v1.ConditionType]corev1.ConditionStatus {
result := make(map[v1.ConditionType]corev1.ConditionStatus)
for _, cond := range conditionList {
result[cond.Type] = cond.Status
}
return result
}
// ConditionsChanged compares condition maps and return true if any of the conditions changed, false otherwise.
func ConditionsChanged(originalValues, newValues map[v1.ConditionType]corev1.ConditionStatus) bool {
if len(originalValues) != len(newValues) {
return true
}
for k, v := range newValues {
oldV, ok := originalValues[k]
if !ok || oldV != v {
return true
}
}
return false
}
// MarkCrHealthyMessage marks the passed in CR as healthy. The CR object needs to be updated by the caller afterwards.
// Healthy means the following status conditions are set:
// ApplicationAvailable: true
// Progressing: false
// Degraded: false
func MarkCrHealthyMessage(cr client.Object, crStatus *api.Status, reason, message string, recorder record.EventRecorder) {
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionAvailable,
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionProgressing,
Status: corev1.ConditionFalse,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionDegraded,
Status: corev1.ConditionFalse,
})
recorder.Event(cr, corev1.EventTypeNormal, reason, message)
}
// MarkCrUpgradeHealingDegraded marks the passed CR as upgrading and degraded. The CR object needs to be updated by the caller afterwards.
// Failed means the following status conditions are set:
// ApplicationAvailable: true
// Progressing: true
// Degraded: true
func MarkCrUpgradeHealingDegraded(cr client.Object, crStatus *api.Status, reason, message string, recorder record.EventRecorder) {
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionAvailable,
Status: corev1.ConditionTrue,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionProgressing,
Status: corev1.ConditionTrue,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionDegraded,
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
})
recorder.Event(cr, corev1.EventTypeNormal, reason, message)
}
// MarkCrFailed marks the passed CR as failed and requiring human intervention. The CR object needs to be updated by the caller afterwards.
// Failed means the following status conditions are set:
// ApplicationAvailable: false
// Progressing: false
// Degraded: true
func MarkCrFailed(cr client.Object, crStatus *api.Status, reason, message string, recorder record.EventRecorder) {
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionAvailable,
Status: corev1.ConditionFalse,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionProgressing,
Status: corev1.ConditionFalse,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionDegraded,
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
})
recorder.Event(cr, corev1.EventTypeWarning, reason, message)
}
// MarkCrFailedHealing marks the passed CR as failed and healing. The CR object needs to be updated by the caller afterwards.
// FailedAndHealing means the following status conditions are set:
// ApplicationAvailable: false
// Progressing: true
// Degraded: true
func MarkCrFailedHealing(cr client.Object, crStatus *api.Status, reason, message string, recorder record.EventRecorder) {
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionAvailable,
Status: corev1.ConditionFalse,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionProgressing,
Status: corev1.ConditionTrue,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionDegraded,
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
})
recorder.Event(cr, corev1.EventTypeWarning, reason, message)
}
// MarkCrDeploying marks the passed CR as currently deploying. The CR object needs to be updated by the caller afterwards.
// Deploying means the following status conditions are set:
// ApplicationAvailable: false
// Progressing: true
// Degraded: false
func MarkCrDeploying(cr runtime.Object, crStatus *api.Status, reason, message string, recorder record.EventRecorder) {
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionAvailable,
Status: corev1.ConditionFalse,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionProgressing,
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
})
v1.SetStatusCondition(&crStatus.Conditions, v1.Condition{
Type: v1.ConditionDegraded,
Status: corev1.ConditionFalse,
})
recorder.Event(cr, corev1.EventTypeNormal, reason, message)
}