forked from argoproj/argo-rollouts
/
bluegreen.go
359 lines (313 loc) · 13.1 KB
/
bluegreen.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
package rollout
import (
"sort"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/controller"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/argoproj/argo-rollouts/utils/annotations"
"github.com/argoproj/argo-rollouts/utils/defaults"
logutil "github.com/argoproj/argo-rollouts/utils/log"
replicasetutil "github.com/argoproj/argo-rollouts/utils/replicaset"
serviceutil "github.com/argoproj/argo-rollouts/utils/service"
)
// rolloutBlueGreen implements the logic for rolling a new replica set.
func (c *RolloutController) rolloutBlueGreen(r *v1alpha1.Rollout, rsList []*appsv1.ReplicaSet) error {
previewSvc, activeSvc, err := c.getPreviewAndActiveServices(r)
if err != nil {
return err
}
newRS, oldRSs, err := c.getAllReplicaSetsAndSyncRevision(r, rsList, true)
if err != nil {
return err
}
roCtx := newBlueGreenCtx(r, newRS, oldRSs)
logCtx := roCtx.Log()
allRSs := roCtx.AllRSs()
if reconcileBlueGreenTemplateChange(roCtx) {
roCtx.PauseContext().ClearPauseConditions()
roCtx.PauseContext().RemoveAbort()
logCtx.Infof("New pod template or template change detected")
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
// Scale up, if we can.
logCtx.Infof("Reconciling new ReplicaSet '%s'", newRS.Name)
scaledUp, err := c.reconcileNewReplicaSet(roCtx)
if err != nil {
return err
}
// Scale down old non-active replicasets, if we can.
_, filteredOldRS := replicasetutil.GetReplicaSetByTemplateHash(oldRSs, activeSvc.Spec.Selector[v1alpha1.DefaultRolloutUniqueLabelKey])
logCtx.Info("Reconciling old replica sets")
scaledDown, err := c.reconcileOldReplicaSets(controller.FilterActiveReplicaSets(filteredOldRS), roCtx)
if err != nil {
return err
}
logCtx.Info("Cleaning up old replicasets")
if err := c.cleanupRollouts(filteredOldRS, roCtx); err != nil {
return err
}
switchPreviewSvc, err := c.reconcilePreviewService(roCtx, previewSvc)
if err != nil {
return err
}
if scaledUp {
logCtx.Infof("Not finished reconciling new ReplicaSet '%s'", newRS.Name)
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
if scaledDown {
logCtx.Info("Not finished reconciling old replica sets")
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
if switchPreviewSvc {
logCtx.Infof("Not finished reconciling preview service' %s'", previewSvc.Name)
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
if !replicasetutil.ReadyForPause(r, newRS, allRSs) {
logutil.WithRollout(r).Infof("New RS '%s' is not ready to pause", newRS.Name)
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
noFastRollback := true
if newRS != nil {
_, hasScaleDownDeadlineAnnotationKey := newRS.Annotations[v1alpha1.DefaultReplicaSetScaleDownDeadlineAnnotationKey]
if hasScaleDownDeadlineAnnotationKey {
logCtx.Infof("Detected scale down annotation for ReplicaSet '%s' and will skip pause", newRS.Name)
noFastRollback = false
}
}
if noFastRollback {
logCtx.Info("Reconciling pause")
pauseBeforeSwitchActive := c.reconcileBlueGreenPause(activeSvc, previewSvc, roCtx)
if pauseBeforeSwitchActive {
logCtx.Info("Not finished reconciling pause")
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
}
logCtx.Infof("Reconciling active service '%s'", activeSvc.Name)
if !annotations.IsSaturated(r, newRS) {
logCtx.Infof("New RS '%s' is not fully saturated", newRS.Name)
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
switchActiveSvc, err := c.reconcileActiveService(roCtx, activeSvc)
if err != nil {
return err
}
if switchActiveSvc {
logCtx.Infof("Not finished reconciling active service '%s'", activeSvc.Name)
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
if _, ok := newRS.Annotations[v1alpha1.DefaultReplicaSetScaleDownDeadlineAnnotationKey]; ok {
// SetScaleDownDeadlineAnnotation should be removed from the new RS to ensure a new value is set
// when the active service changes to a different RS
err := c.removeScaleDownDelay(roCtx, newRS)
if err != nil {
return err
}
}
return c.syncRolloutStatusBlueGreen(previewSvc, activeSvc, roCtx)
}
// reconcileBlueGreenTemplateChange returns true if we detect there was a change in the pod template
// from our current pod hash, or the newRS does not yet exist
func reconcileBlueGreenTemplateChange(roCtx *blueGreenContext) bool {
r := roCtx.Rollout()
newRS := roCtx.NewRS()
if newRS == nil {
return true
}
return r.Status.CurrentPodHash != newRS.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
}
func (c *RolloutController) reconcileBlueGreenPause(activeSvc, previewSvc *corev1.Service, roCtx *blueGreenContext) bool {
rollout := roCtx.Rollout()
if rollout.Spec.Paused {
return false
}
if defaults.GetAutoPromotionEnabledOrDefault(rollout) {
return false
}
newRS := roCtx.NewRS()
if newRS == nil {
return false
}
newRSPodHash := newRS.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
if _, ok := activeSvc.Spec.Selector[v1alpha1.DefaultRolloutUniqueLabelKey]; !ok {
return false
}
cond := getPauseCondition(rollout, v1alpha1.PauseReasonBlueGreenPause)
// If the rollout is not paused and the active service is not point at the newRS, we should pause the rollout.
if cond == nil && !rollout.Status.ControllerPause && !rollout.Status.BlueGreen.ScaleUpPreviewCheckPoint && activeSvc.Spec.Selector[v1alpha1.DefaultRolloutUniqueLabelKey] != newRSPodHash {
roCtx.PauseContext().AddPauseCondition(v1alpha1.PauseReasonBlueGreenPause)
return true
}
autoPromoteActiveServiceDelaySeconds := rollout.Spec.Strategy.BlueGreen.AutoPromotionSeconds
if autoPromoteActiveServiceDelaySeconds != nil && cond != nil {
c.checkEnqueueRolloutDuringWait(rollout, cond.StartTime, *autoPromoteActiveServiceDelaySeconds)
switchDeadline := cond.StartTime.Add(time.Duration(*autoPromoteActiveServiceDelaySeconds) * time.Second)
now := metav1.Now()
if now.After(switchDeadline) {
roCtx.PauseContext().RemovePauseCondition(v1alpha1.PauseReasonBlueGreenPause)
return false
}
}
return cond != nil && rollout.Status.ControllerPause
}
// scaleDownOldReplicaSetsForBlueGreen scales down old replica sets when rollout strategy is "Blue Green".
func (c *RolloutController) scaleDownOldReplicaSetsForBlueGreen(oldRSs []*appsv1.ReplicaSet, rollout *v1alpha1.Rollout) (bool, error) {
logCtx := logutil.WithRollout(rollout)
sort.Sort(sort.Reverse(replicasetutil.ReplicaSetsByRevisionNumber(oldRSs)))
hasScaled := false
annotationedRSs := int32(0)
rolloutReplicas := defaults.GetReplicasOrDefault(rollout.Spec.Replicas)
for _, targetRS := range oldRSs {
desiredReplicaCount := int32(0)
if scaleDownAtStr, ok := targetRS.Annotations[v1alpha1.DefaultReplicaSetScaleDownDeadlineAnnotationKey]; ok {
annotationedRSs++
scaleDownAtTime, err := time.Parse(time.RFC3339, scaleDownAtStr)
if err != nil {
logCtx.Warnf("Unable to read scaleDownAt label on rs '%s'", targetRS.Name)
} else if rollout.Spec.Strategy.BlueGreen.ScaleDownDelayRevisionLimit != nil && annotationedRSs == *rollout.Spec.Strategy.BlueGreen.ScaleDownDelayRevisionLimit {
logCtx.Info("At ScaleDownDelayRevisionLimit and scaling down the rest")
} else {
now := metav1.Now()
scaleDownAt := metav1.NewTime(scaleDownAtTime)
if scaleDownAt.After(now.Time) {
logCtx.Infof("RS '%s' has not reached the scaleDownTime", targetRS.Name)
remainingTime := scaleDownAt.Sub(now.Time)
if remainingTime < c.resyncPeriod {
c.enqueueRolloutAfter(rollout, remainingTime)
}
desiredReplicaCount = rolloutReplicas
}
}
}
if *(targetRS.Spec.Replicas) == desiredReplicaCount {
// at desired account
continue
}
// Scale down.
_, _, err := c.scaleReplicaSetAndRecordEvent(targetRS, desiredReplicaCount, rollout)
if err != nil {
return false, err
}
hasScaled = true
}
return hasScaled, nil
}
func (c *RolloutController) syncRolloutStatusBlueGreen(previewSvc *corev1.Service, activeSvc *corev1.Service, roCtx *blueGreenContext) error {
r := roCtx.Rollout()
newRS := roCtx.NewRS()
oldRSs := roCtx.OlderRSs()
allRSs := roCtx.AllRSs()
newStatus := c.calculateBaseStatus(roCtx)
if replicasetutil.CheckPodSpecChange(r, newRS) {
roCtx.PauseContext().ClearPauseConditions()
roCtx.PauseContext().RemoveAbort()
}
newStatus.AvailableReplicas = replicasetutil.GetAvailableReplicaCountForReplicaSets([]*appsv1.ReplicaSet{newRS})
previewSelector, ok := serviceutil.GetRolloutSelectorLabel(previewSvc)
if !ok {
previewSelector = ""
}
newStatus.BlueGreen.PreviewSelector = previewSelector
activeSelector, ok := serviceutil.GetRolloutSelectorLabel(activeSvc)
if !ok {
activeSelector = ""
}
newStatus.BlueGreen.ActiveSelector = activeSelector
if newStatus.BlueGreen.ActiveSelector != r.Status.BlueGreen.ActiveSelector {
previousActiveRS, _ := replicasetutil.GetReplicaSetByTemplateHash(oldRSs, r.Status.BlueGreen.ActiveSelector)
if replicasetutil.GetReplicaCountForReplicaSets([]*appsv1.ReplicaSet{previousActiveRS}) > 0 {
err := c.addScaleDownDelay(roCtx, previousActiveRS)
if err != nil {
return err
}
}
}
activeRS, _ := replicasetutil.GetReplicaSetByTemplateHash(allRSs, newStatus.BlueGreen.ActiveSelector)
if activeRS != nil {
newStatus.HPAReplicas = replicasetutil.GetActualReplicaCountForReplicaSets([]*appsv1.ReplicaSet{activeRS})
newStatus.Selector = metav1.FormatLabelSelector(activeRS.Spec.Selector)
} else {
newStatus.HPAReplicas = replicasetutil.GetActualReplicaCountForReplicaSets(allRSs)
newStatus.Selector = metav1.FormatLabelSelector(r.Spec.Selector)
}
newStatus.BlueGreen.ScaleUpPreviewCheckPoint = calculateScaleUpPreviewCheckPoint(roCtx, activeRS)
newStatus = c.calculateRolloutConditions(roCtx, newStatus)
return c.persistRolloutStatus(roCtx, &newStatus)
}
func calculateScaleUpPreviewCheckPoint(roCtx *blueGreenContext, activeRS *appsv1.ReplicaSet) bool {
r := roCtx.Rollout()
newRS := roCtx.NewRS()
if reconcileBlueGreenTemplateChange(roCtx) || r.Spec.Strategy.BlueGreen.PreviewReplicaCount == nil {
return false
}
if newRS == nil || activeRS == nil || activeRS.Name == newRS.Name {
return false
}
// Once the ScaleUpPreviewCheckPoint is set to true, the rollout should keep that value until
// the newRS becomes the new activeRS or there is a template change.
if r.Status.BlueGreen.ScaleUpPreviewCheckPoint {
return r.Status.BlueGreen.ScaleUpPreviewCheckPoint
}
newRSAvailableCount := replicasetutil.GetAvailableReplicaCountForReplicaSets([]*appsv1.ReplicaSet{newRS})
return newRSAvailableCount == *r.Spec.Strategy.BlueGreen.PreviewReplicaCount
}
// Should run only on scaling events and not during the normal rollout process.
func (c *RolloutController) scaleBlueGreen(rollout *v1alpha1.Rollout, newRS *appsv1.ReplicaSet, oldRSs []*appsv1.ReplicaSet, previewSvc *corev1.Service, activeSvc *corev1.Service) error {
rolloutReplicas := defaults.GetReplicasOrDefault(rollout.Spec.Replicas)
// If there is only one replica set with pods, then we should scale that up to the full count of the
// rollout. If there is no replica set with pods, then we should scale up the newest replica set.
if activeOrLatest := replicasetutil.FindActiveOrLatest(newRS, oldRSs); activeOrLatest != nil {
if *(activeOrLatest.Spec.Replicas) != rolloutReplicas {
_, _, err := c.scaleReplicaSetAndRecordEvent(activeOrLatest, rolloutReplicas, rollout)
return err
}
}
allRS := append([]*appsv1.ReplicaSet{newRS}, oldRSs...)
activeRS, _ := replicasetutil.GetReplicaSetByTemplateHash(allRS, rollout.Status.BlueGreen.ActiveSelector)
if activeRS != nil {
if *(activeRS.Spec.Replicas) != rolloutReplicas {
_, _, err := c.scaleReplicaSetAndRecordEvent(activeRS, rolloutReplicas, rollout)
return err
}
}
if newRS != nil {
newRSReplicaCount, err := replicasetutil.NewRSNewReplicas(rollout, allRS, newRS)
if err != nil {
return err
}
if *(newRS.Spec.Replicas) != newRSReplicaCount {
_, _, err := c.scaleReplicaSetAndRecordEvent(newRS, newRSReplicaCount, rollout)
return err
}
}
sort.Sort(sort.Reverse(replicasetutil.ReplicaSetsByRevisionNumber(oldRSs)))
annotationedRSs := int32(0)
logCtx := logutil.WithRollout(rollout)
for _, targetRS := range oldRSs {
newDesiredReplicas := int32(0)
if scaleDownAtStr, ok := targetRS.Annotations[v1alpha1.DefaultReplicaSetScaleDownDeadlineAnnotationKey]; ok {
annotationedRSs++
scaleDownAtTime, err := time.Parse(time.RFC3339, scaleDownAtStr)
if err != nil {
logCtx.Warnf("Unable to read scaleDownAt label on rs '%s'", targetRS.Name)
} else if rollout.Spec.Strategy.BlueGreen.ScaleDownDelayRevisionLimit != nil && annotationedRSs == *rollout.Spec.Strategy.BlueGreen.ScaleDownDelayRevisionLimit {
logCtx.Info("At ScaleDownDelayRevisionLimit and scaling down the rest")
} else {
now := metav1.Now()
scaleDownAt := metav1.NewTime(scaleDownAtTime)
if scaleDownAt.After(now.Time) {
newDesiredReplicas = rolloutReplicas
}
}
if *(targetRS.Spec.Replicas) != newDesiredReplicas {
_, _, err := c.scaleReplicaSetAndRecordEvent(targetRS, newDesiredReplicas, rollout)
if err != nil {
return err
}
}
}
}
return nil
}