generated from pravega/pravega-operator
/
status.go
303 lines (255 loc) · 9.4 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* Copyright (c) 2018 Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package v1alpha1
import (
"log"
"time"
corev1 "k8s.io/api/core/v1"
)
type ClusterConditionType string
const (
ClusterConditionPodsReady ClusterConditionType = "PodsReady"
ClusterConditionUpgrading = "Upgrading"
ClusterConditionRollback = "RollbackInProgress"
ClusterConditionError = "Error"
// Reasons for cluster upgrading condition
UpdatingBookkeeperReason = "Updating Bookkeeper"
UpgradeErrorReason = "Upgrade Error"
RollbackErrorReason = "Rollback Error"
)
// BookkeeperClusterStatus defines the observed state of BookkeeperCluster
type BookkeeperClusterStatus struct {
// Conditions list all the applied conditions
Conditions []ClusterCondition `json:"conditions,omitempty"`
// CurrentVersion is the current cluster version
CurrentVersion string `json:"currentVersion,omitempty"`
// TargetVersion is the version the cluster upgrading to.
// If the cluster is not upgrading, TargetVersion is empty.
TargetVersion string `json:"targetVersion,omitempty"`
VersionHistory []string `json:"versionHistory,omitempty"`
// Replicas is the number of desired replicas in the cluster
// +optional
Replicas int32 `json:"replicas"`
// CurrentReplicas is the number of current replicas in the cluster
// +optional
CurrentReplicas int32 `json:"currentReplicas"`
// ReadyReplicas is the number of ready replicas in the cluster
// +optional
ReadyReplicas int32 `json:"readyReplicas"`
// Members is the Bookkeeper members in the cluster
// +optional
Members MembersStatus `json:"members"`
}
// MembersStatus is the status of the members of the cluster with both
// ready and unready node membership lists
type MembersStatus struct {
// +optional
// +nullable
Ready []string `json:"ready"`
// +optional
// +nullable
Unready []string `json:"unready"`
}
// ClusterCondition shows the current condition of a Bookkeeper cluster.
// Comply with k8s API conventions
type ClusterCondition struct {
// Type of Bookkeeper cluster condition.
// +optional
Type ClusterConditionType `json:"type"`
// Status of the condition, one of True, False, Unknown.
// +optional
Status corev1.ConditionStatus `json:"status"`
// The reason for the condition's last transition.
Reason string `json:"reason,omitempty"`
// A human readable message indicating details about the transition.
Message string `json:"message,omitempty"`
// The last time this condition was updated.
LastUpdateTime string `json:"lastUpdateTime,omitempty"`
// Last time the condition transitioned from one status to another.
LastTransitionTime string `json:"lastTransitionTime,omitempty"`
}
func (ps *BookkeeperClusterStatus) Init() {
// Initialise conditions
conditionTypes := []ClusterConditionType{
ClusterConditionPodsReady,
ClusterConditionUpgrading,
ClusterConditionError,
}
for _, conditionType := range conditionTypes {
if _, condition := ps.GetClusterCondition(conditionType); condition == nil {
c := newClusterCondition(conditionType, corev1.ConditionFalse, "", "")
ps.setClusterCondition(*c)
}
}
// Set current cluster version in version history,
// so if the first upgrade fails we can rollback to this version
if ps.VersionHistory == nil && ps.CurrentVersion != "" {
ps.VersionHistory = []string{ps.CurrentVersion}
}
}
func (ps *BookkeeperClusterStatus) SetPodsReadyConditionTrue() {
c := newClusterCondition(ClusterConditionPodsReady, corev1.ConditionTrue, "", "")
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetPodsReadyConditionFalse() {
c := newClusterCondition(ClusterConditionPodsReady, corev1.ConditionFalse, "", "")
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetUpgradingConditionTrue(reason, message string) {
c := newClusterCondition(ClusterConditionUpgrading, corev1.ConditionTrue, reason, message)
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetUpgradingConditionFalse() {
c := newClusterCondition(ClusterConditionUpgrading, corev1.ConditionFalse, "", "")
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetErrorConditionTrue(reason, message string) {
c := newClusterCondition(ClusterConditionError, corev1.ConditionTrue, reason, message)
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetErrorConditionFalse() {
c := newClusterCondition(ClusterConditionError, corev1.ConditionFalse, "", "")
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetRollbackConditionTrue(reason, message string) {
c := newClusterCondition(ClusterConditionRollback, corev1.ConditionTrue, reason, message)
ps.setClusterCondition(*c)
}
func (ps *BookkeeperClusterStatus) SetRollbackConditionFalse() {
c := newClusterCondition(ClusterConditionRollback, corev1.ConditionFalse, "", "")
ps.setClusterCondition(*c)
}
func newClusterCondition(condType ClusterConditionType, status corev1.ConditionStatus, reason, message string) *ClusterCondition {
return &ClusterCondition{
Type: condType,
Status: status,
Reason: reason,
Message: message,
LastUpdateTime: "",
LastTransitionTime: "",
}
}
func (ps *BookkeeperClusterStatus) GetClusterCondition(t ClusterConditionType) (int, *ClusterCondition) {
for i, c := range ps.Conditions {
if t == c.Type {
return i, &c
}
}
return -1, nil
}
func (ps *BookkeeperClusterStatus) setClusterCondition(newCondition ClusterCondition) {
now := time.Now().Format(time.RFC3339)
position, existingCondition := ps.GetClusterCondition(newCondition.Type)
if existingCondition == nil {
ps.Conditions = append(ps.Conditions, newCondition)
return
}
if existingCondition.Status != newCondition.Status {
existingCondition.Status = newCondition.Status
existingCondition.LastTransitionTime = now
existingCondition.LastUpdateTime = now
}
if existingCondition.Reason != newCondition.Reason || existingCondition.Message != newCondition.Message {
existingCondition.Reason = newCondition.Reason
existingCondition.Message = newCondition.Message
existingCondition.LastUpdateTime = now
}
ps.Conditions[position] = *existingCondition
}
func (ps *BookkeeperClusterStatus) AddToVersionHistory(version string) {
lastIndex := len(ps.VersionHistory) - 1
if version != "" && ps.VersionHistory[lastIndex] != version {
ps.VersionHistory = append(ps.VersionHistory, version)
log.Printf("Updating version history adding version %v", version)
}
}
func (ps *BookkeeperClusterStatus) GetLastVersion() (previousVersion string) {
len := len(ps.VersionHistory)
return ps.VersionHistory[len-1]
}
func (ps *BookkeeperClusterStatus) IsClusterInErrorState() bool {
_, errorCondition := ps.GetClusterCondition(ClusterConditionError)
if errorCondition != nil && errorCondition.Status == corev1.ConditionTrue {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) IsClusterInUpgradeFailedState() bool {
_, errorCondition := ps.GetClusterCondition(ClusterConditionError)
if errorCondition == nil {
return false
}
if errorCondition.Status == corev1.ConditionTrue && errorCondition.Reason == "UpgradeFailed" {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) IsClusterInUpgradeFailedOrRollbackState() bool {
if ps.IsClusterInUpgradeFailedState() || ps.IsClusterInRollbackState() {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) IsClusterInRollbackState() bool {
_, rollbackCondition := ps.GetClusterCondition(ClusterConditionRollback)
if rollbackCondition == nil {
return false
}
if rollbackCondition.Status == corev1.ConditionTrue {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) IsClusterInUpgradingState() bool {
_, upgradeCondition := ps.GetClusterCondition(ClusterConditionUpgrading)
if upgradeCondition == nil {
return false
}
if upgradeCondition.Status == corev1.ConditionTrue {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) IsClusterInRollbackFailedState() bool {
_, errorCondition := ps.GetClusterCondition(ClusterConditionError)
if errorCondition == nil {
return false
}
if errorCondition.Status == corev1.ConditionTrue && errorCondition.Reason == "RollbackFailed" {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) IsClusterInReadyState() bool {
_, readyCondition := ps.GetClusterCondition(ClusterConditionPodsReady)
if readyCondition != nil && readyCondition.Status == corev1.ConditionTrue {
return true
}
return false
}
func (ps *BookkeeperClusterStatus) UpdateProgress(reason, updatedReplicas string) {
if ps.IsClusterInUpgradingState() {
// Set the upgrade condition reason to be UpgradingBookkeeperReason, message to be 0
ps.SetUpgradingConditionTrue(reason, updatedReplicas)
} else {
ps.SetRollbackConditionTrue(reason, updatedReplicas)
}
}
func (ps *BookkeeperClusterStatus) GetLastCondition() (lastCondition *ClusterCondition) {
if ps.IsClusterInUpgradingState() {
_, lastCondition := ps.GetClusterCondition(ClusterConditionUpgrading)
return lastCondition
} else if ps.IsClusterInRollbackState() {
_, lastCondition := ps.GetClusterCondition(ClusterConditionRollback)
return lastCondition
}
// nothing to do if we are neither upgrading nor rolling back,
return nil
}