-
Notifications
You must be signed in to change notification settings - Fork 45
/
rollingupgrade_types.go
384 lines (316 loc) · 13.1 KB
/
rollingupgrade_types.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Copyright 2021 Intuit Inc.
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/keikoproj/upgrade-manager/controllers/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// RollingUpgradeSpec defines the desired state of RollingUpgrade
type RollingUpgradeSpec struct {
PostDrainDelaySeconds int `json:"postDrainDelaySeconds,omitempty"`
NodeIntervalSeconds int `json:"nodeIntervalSeconds,omitempty"`
AsgName string `json:"asgName,omitempty"`
PreDrain PreDrainSpec `json:"preDrain,omitempty"`
PostDrain PostDrainSpec `json:"postDrain,omitempty"`
PostTerminate PostTerminateSpec `json:"postTerminate,omitempty"`
Strategy UpdateStrategy `json:"strategy,omitempty"`
IgnoreDrainFailures *bool `json:"ignoreDrainFailures,omitempty"`
ForceRefresh bool `json:"forceRefresh,omitempty"`
ReadinessGates []NodeReadinessGate `json:"readinessGates,omitempty"`
}
// RollingUpgradeStatus defines the observed state of RollingUpgrade
type RollingUpgradeStatus struct {
CurrentStatus string `json:"currentStatus,omitempty"`
StartTime string `json:"startTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
TotalProcessingTime string `json:"totalProcessingTime,omitempty"`
NodesProcessed int `json:"nodesProcessed,omitempty"`
TotalNodes int `json:"totalNodes,omitempty"`
CompletePercentage string `json:"completePercentage,omitempty"`
Conditions []RollingUpgradeCondition `json:"conditions,omitempty"`
LastNodeTerminationTime *metav1.Time `json:"lastTerminationTime,omitempty"`
LastNodeDrainTime *metav1.Time `json:"lastDrainTime,omitempty"`
Statistics []*RollingUpgradeStatistics `json:"statistics,omitempty"`
// For backward compatibility
LastBatchNodes []string `json:"lastBatchNodes,omitempty"`
NodeInProcessing map[string]*NodeInProcessing `json:"nodeInProcessing,omitempty"`
}
// RollingUpgrade Statistics, includes summary(sum/count) from each step
type RollingUpgradeStatistics struct {
StepName RollingUpgradeStep `json:"stepName,omitempty"`
DurationSum metav1.Duration `json:"durationSum,omitempty"`
DurationCount int32 `json:"durationCount,omitempty"`
}
// RollingUpgrade Node step information
type NodeStepDuration struct {
GroupName string `json:"groupName,omitempty"`
NodeName string `json:"nodeName,omitempty"`
StepName RollingUpgradeStep `json:"stepName,omitempty"`
Duration metav1.Duration `json:"duration,omitempty"`
}
// Node In-processing
type NodeInProcessing struct {
NodeName string `json:"nodeName,omitempty"`
StepName RollingUpgradeStep `json:"stepName,omitempty"`
UpgradeStartTime metav1.Time `json:"upgradeStartTime,omitempty"`
StepStartTime metav1.Time `json:"stepStartTime,omitempty"`
StepEndTime metav1.Time `json:"stepEndTime,omitempty"`
}
func (s *RollingUpgradeStatus) SetCondition(cond RollingUpgradeCondition) {
// if condition exists, overwrite, otherwise append
for ix, c := range s.Conditions {
if c.Type == cond.Type {
s.Conditions[ix] = cond
return
}
}
s.Conditions = append(s.Conditions, cond)
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=rollingupgrades,scope=Namespaced,shortName=ru
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.currentStatus",description="current status of the rollingupgarde"
// +kubebuilder:printcolumn:name="TotalNodes",type="string",JSONPath=".status.totalNodes",description="total nodes involved in the rollingupgarde"
// +kubebuilder:printcolumn:name="NodesProcessed",type="string",JSONPath=".status.nodesProcessed",description="current number of nodes processed in the rollingupgarde"
// +kubebuilder:printcolumn:name="Complete",type="string",JSONPath=".status.completePercentage",description="percentage of completion for the rollingupgrade CR"
// RollingUpgrade is the Schema for the rollingupgrades API
type RollingUpgrade struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec RollingUpgradeSpec `json:"spec,omitempty"`
Status RollingUpgradeStatus `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// RollingUpgradeList contains a list of RollingUpgrade
type RollingUpgradeList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []RollingUpgrade `json:"items"`
}
func init() {
SchemeBuilder.Register(&RollingUpgrade{}, &RollingUpgradeList{})
}
// PreDrainSpec contains the fields for actions taken before draining the node.
type PreDrainSpec struct {
Script string `json:"script,omitempty"`
}
// PostDrainSpec contains the fields for actions taken after draining the node.
type PostDrainSpec struct {
Script string `json:"script,omitempty"`
WaitSeconds int64 `json:"waitSeconds,omitempty"`
PostWaitScript string `json:"postWaitScript,omitempty"`
}
// PostTerminateSpec contains the fields for actions taken after terminating the node.
type PostTerminateSpec struct {
Script string `json:"script,omitempty"`
}
type NodeReadinessGate struct {
MatchLabels map[string]string `json:"matchLabels,omitempty" protobuf:"bytes,1,rep,name=matchLabels"`
}
type RollingUpgradeStep string
const (
// Status
StatusInit = "init"
StatusRunning = "running"
StatusComplete = "completed"
StatusError = "error"
// Conditions
UpgradeComplete UpgradeConditionType = "Complete"
NodeRotationTotal RollingUpgradeStep = "total"
NodeRotationKickoff RollingUpgradeStep = "kickoff"
NodeRotationDesiredNodeReady RollingUpgradeStep = "desired_node_ready"
NodeRotationPredrainScript RollingUpgradeStep = "predrain_script"
NodeRotationDrain RollingUpgradeStep = "drain"
NodeRotationPostdrainScript RollingUpgradeStep = "postdrain_script"
NodeRotationPostWait RollingUpgradeStep = "post_wait"
NodeRotationTerminate RollingUpgradeStep = "terminate"
NodeRotationPostTerminate RollingUpgradeStep = "post_terminate"
NodeRotationTerminated RollingUpgradeStep = "terminated"
NodeRotationCompleted RollingUpgradeStep = "completed"
)
var NodeRotationStepOrders = map[RollingUpgradeStep]int{
NodeRotationKickoff: 10,
NodeRotationDesiredNodeReady: 20,
NodeRotationPredrainScript: 30,
NodeRotationDrain: 40,
NodeRotationPostdrainScript: 50,
NodeRotationPostWait: 60,
NodeRotationTerminate: 70,
NodeRotationPostTerminate: 80,
NodeRotationTerminated: 90,
NodeRotationCompleted: 1000,
}
var (
FiniteStates = []string{StatusComplete, StatusError}
AllowedStrategyType = []string{string(RandomUpdateStrategy), string(UniformAcrossAzUpdateStrategy)}
AllowedStrategyMode = []string{string(UpdateStrategyModeLazy), string(UpdateStrategyModeEager)}
DefaultRequeueTime = time.Second * 30
)
// RollingUpgradeCondition describes the state of the RollingUpgrade
type RollingUpgradeCondition struct {
Type UpgradeConditionType `json:"type,omitempty"`
Status corev1.ConditionStatus `json:"status,omitempty"`
}
type UpdateStrategyType string
type UpdateStrategyMode string
type UpgradeConditionType string
const (
RandomUpdateStrategy UpdateStrategyType = "randomUpdate"
UniformAcrossAzUpdateStrategy UpdateStrategyType = "uniformAcrossAzUpdate"
UpdateStrategyModeLazy UpdateStrategyMode = "lazy"
UpdateStrategyModeEager UpdateStrategyMode = "eager"
)
// UpdateStrategy holds the information needed to perform update based on different update strategies
type UpdateStrategy struct {
Type UpdateStrategyType `json:"type,omitempty"`
Mode UpdateStrategyMode `json:"mode,omitempty"`
MaxUnavailable intstr.IntOrString `json:"maxUnavailable,omitempty"`
DrainTimeout *int `json:"drainTimeout,omitempty"`
}
func (c UpdateStrategyMode) String() string {
return string(c)
}
// NamespacedName returns namespaced name of the object.
func (r *RollingUpgrade) NamespacedName() string {
return fmt.Sprintf("%s/%s", r.Namespace, r.Name)
}
func (r *RollingUpgrade) ScalingGroupName() string {
return r.Spec.AsgName
}
func (r *RollingUpgrade) DrainTimeout() *int {
return r.Spec.Strategy.DrainTimeout
}
func (r *RollingUpgrade) PostTerminateScript() string {
return r.Spec.PostTerminate.Script
}
func (r *RollingUpgrade) PostWaitScript() string {
return r.Spec.PostDrain.PostWaitScript
}
func (r *RollingUpgrade) PreDrainScript() string {
return r.Spec.PreDrain.Script
}
func (r *RollingUpgrade) PostDrainScript() string {
return r.Spec.PostDrain.Script
}
func (r *RollingUpgrade) CurrentStatus() string {
return r.Status.CurrentStatus
}
func (r *RollingUpgrade) UpdateStrategyType() UpdateStrategyType {
return r.Spec.Strategy.Type
}
func (r *RollingUpgrade) MaxUnavailable() intstr.IntOrString {
return r.Spec.Strategy.MaxUnavailable
}
func (r *RollingUpgrade) LastNodeTerminationTime() *metav1.Time {
return r.Status.LastNodeTerminationTime
}
func (r *RollingUpgrade) SetLastNodeTerminationTime(t *metav1.Time) {
r.Status.LastNodeTerminationTime = t
}
func (r *RollingUpgrade) LastNodeDrainTime() *metav1.Time {
return r.Status.LastNodeDrainTime
}
func (r *RollingUpgrade) SetLastNodeDrainTime(t *metav1.Time) {
r.Status.LastNodeDrainTime = t
}
func (r *RollingUpgrade) NodeIntervalSeconds() int {
return r.Spec.NodeIntervalSeconds
}
func (r *RollingUpgrade) PostDrainDelaySeconds() int {
return r.Spec.PostDrainDelaySeconds
}
func (r *RollingUpgrade) SetCurrentStatus(status string) {
r.Status.CurrentStatus = status
}
func (r *RollingUpgrade) SetStartTime(t string) {
r.Status.StartTime = t
}
func (r *RollingUpgrade) StartTime() string {
return r.Status.StartTime
}
func (r *RollingUpgrade) SetEndTime(t string) {
r.Status.EndTime = t
}
func (r *RollingUpgrade) EndTime() string {
return r.Status.EndTime
}
func (r *RollingUpgrade) SetTotalProcessingTime(t string) {
r.Status.TotalProcessingTime = t
}
func (r *RollingUpgrade) SetTotalNodes(n int) {
r.Status.TotalNodes = n
}
func (r *RollingUpgrade) SetNodesProcessed(n int) {
r.Status.NodesProcessed = n
}
func (r *RollingUpgrade) SetCompletePercentage(n int) {
r.Status.CompletePercentage = fmt.Sprintf("%s%%", strconv.Itoa(n))
}
func (r *RollingUpgrade) GetStatus() RollingUpgradeStatus {
return r.Status
}
func (r *RollingUpgrade) IsForceRefresh() bool {
return r.Spec.ForceRefresh
}
func (r *RollingUpgrade) IsIgnoreDrainFailures() *bool {
return r.Spec.IgnoreDrainFailures
}
func (r *RollingUpgrade) StrategyMode() UpdateStrategyMode {
return r.Spec.Strategy.Mode
}
func (r *RollingUpgrade) Validate() (bool, error) {
strategy := r.Spec.Strategy
// validating the Type value
if strategy.Type == "" {
r.Spec.Strategy.Type = RandomUpdateStrategy
} else if !common.ContainsEqualFold(AllowedStrategyType, string(strategy.Type)) {
err := fmt.Errorf("%s: Invalid value for startegy Type - %d", r.Name, strategy.MaxUnavailable.IntVal)
return false, err
}
// validating the Mode value
if strategy.Mode == "" {
r.Spec.Strategy.Mode = UpdateStrategyModeLazy
} else if !common.ContainsEqualFold(AllowedStrategyMode, string(strategy.Mode)) {
err := fmt.Errorf("%s: Invalid value for startegy Mode - %d", r.Name, strategy.MaxUnavailable.IntVal)
return false, err
}
// validating the maxUnavailable value
if strategy.MaxUnavailable.Type == intstr.Int && strategy.MaxUnavailable.IntVal == 0 {
r.Spec.Strategy.MaxUnavailable.IntVal = 1
} else if strategy.MaxUnavailable.Type == intstr.Int && strategy.MaxUnavailable.IntVal < 0 {
err := fmt.Errorf("%s: Invalid value for startegy maxUnavailable - %d", r.Name, strategy.MaxUnavailable.IntVal)
return false, err
} else if strategy.MaxUnavailable.Type == intstr.String {
intValue, _ := strconv.Atoi(strings.Trim(strategy.MaxUnavailable.StrVal, "%"))
if intValue <= 0 || intValue > 100 {
err := fmt.Errorf("%s: Invalid value for startegy maxUnavailable - %s", r.Name, strategy.MaxUnavailable.StrVal)
return false, err
}
}
// validating the DrainTimeout value
if strategy.DrainTimeout != nil {
if *strategy.DrainTimeout == 0 {
*r.Spec.Strategy.DrainTimeout = -1
} else if *strategy.DrainTimeout < -1 {
err := fmt.Errorf("%s: Invalid value for startegy DrainTimeout - %d", r.Name, strategy.MaxUnavailable.IntVal)
return false, err
}
}
return true, nil
}