-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollout.go
497 lines (486 loc) · 17.8 KB
/
rollout.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package cage
import (
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"time"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"github.com/aws/aws-sdk-go/service/ecs"
"encoding/json"
"encoding/base64"
"github.com/aws/aws-sdk-go/aws"
"math"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/aws/aws-sdk-go/service/elbv2"
"strconv"
)
type Context struct {
Ecs ecsiface.ECSAPI
Cw cloudwatchiface.CloudWatchAPI
Alb elbv2iface.ELBV2API
}
type RollOutResult struct {
StartTime *time.Time
EndTime *time.Time
HandledError error
Rolledback *bool
}
func (envars *Envars) StartGradualRollOut(
ctx *Context,
) (*RollOutResult, error) {
ret := &RollOutResult{
StartTime: aws.Time(now()),
Rolledback: aws.Bool(false),
}
log.Infof("ensuring next task definition...")
nextTaskDefinition, err := envars.CreateNextTaskDefinition(ctx.Ecs)
if err != nil {
log.Errorf("failed to register next task definition due to: %s", err)
return nil, err
}
log.Infof("ensuring next service '%s'...", *envars.NextServiceName)
if err := envars.CreateNextService(ctx.Ecs, nextTaskDefinition.TaskDefinitionArn); err != nil {
log.Errorf("failed to create next service due to: %s", err)
return nil, err
}
// ロールバックのためのデプロイを始める前の現在のサービスのタスク数
var originalDesiredCount int64
out, err := ctx.Ecs.DescribeServices(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{
envars.CurrentServiceName,
envars.NextServiceName,
},
})
if err != nil {
log.Errorf("failed to describe current service due to: %s", err.Error())
return nil, err
}
currentService := out.Services[0]
nextService := out.Services[1]
originalDesiredCount = *currentService.DesiredCount
var (
targetGroupArn *string
)
if len(nextService.LoadBalancers) > 0 {
targetGroupArn = nextService.LoadBalancers[0].TargetGroupArn
}
log.Infof("service '%s' ensured. start rolling out", *envars.NextServiceName)
if err := envars.RollOut(ctx, targetGroupArn, originalDesiredCount); err != nil {
log.Errorf("failed to roll out due to: %s", err)
if err := envars.Rollback(ctx, &originalDesiredCount, targetGroupArn); err != nil {
log.Errorf("😱 failed to rollback service '%s' due to: %s", err)
return nil, err
}
ret.Rolledback = aws.Bool(true)
ret.HandledError = err
log.Infof("😓 service '%s' has successfully rolledback", *envars.CurrentServiceName)
}
ret.EndTime = aws.Time(now())
return ret, nil
}
func (envars *Envars) CanaryTest(
cw cloudwatchiface.CloudWatchAPI,
loadBalancerArn *string,
targetGroupArn *string,
totalRollOutCnt int64,
) error {
startTime := now()
endTime := startTime.Add(time.Duration(*envars.RollOutPeriod) * time.Second)
standBy := time.Duration(60-math.Floor(float64(startTime.Second()))) * time.Second
// ロールアウトの検証期間待つ
log.Infof(
"waiting for %d sec and standing by %f sec for CloudWatch aggregation",
*envars.RollOutPeriod, standBy.Seconds(),
)
<-newTimer(time.Duration(*envars.RollOutPeriod)*time.Second + standBy).C
log.Infof(
"start accumulating periodic service health of '%s' during %s ~ %s",
*envars.NextServiceName, startTime.String(), endTime.String(),
)
health, err := envars.AccumulatePeriodicServiceHealth(cw, loadBalancerArn, targetGroupArn, startTime, endTime)
if err != nil {
return err
}
log.Infof("periodic health accumulated: availability=%f, responseTime=%f", health.availability, health.responseTime)
if *envars.AvailabilityThreshold > health.availability || health.responseTime > *envars.ResponseTimeThreshold {
return NewErrorf(
"😢 %dth canary test has failed: availability=%f (thresh: %f), responseTime=%f (thresh: %f)",
totalRollOutCnt, health.availability, *envars.AvailabilityThreshold, health.responseTime, *envars.ResponseTimeThreshold,
)
}
log.Infof(
"😙 %dth canary test has passed: availability=%f (thresh: %f), responseTime=%f (thresh: %f)",
totalRollOutCnt, health.availability, *envars.AvailabilityThreshold, health.responseTime, *envars.ResponseTimeThreshold,
)
return nil
}
func (envars *Envars) RollOut(
ctx *Context,
targetGroupArn *string,
originalDesiredCount int64,
) (error) {
var (
// ロールアウトで置き換えられたタスクの数
totalReplacedCnt int64 = 0
// 総ロールアウト実行回数
totalRollOutCnt int64 = 0
// 推定ロールアウト施行回数
estimatedRollOutCount = EstimateRollOutCount(originalDesiredCount)
)
log.Infof(
"currently %d tasks running on '%s', %d times roll out estimated",
originalDesiredCount, *envars.CurrentServiceName, estimatedRollOutCount,
)
var (
loadBalancerArn *string
)
if targetGroupArn != nil {
o, err := ctx.Alb.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
TargetGroupArns: []*string{targetGroupArn},
})
if err != nil {
log.Errorf("failed to describe target groups due to: %s", err)
return err
}
loadBalancerArn = o.TargetGroups[0].LoadBalancerArns[0]
} else {
// LBがないサービスはCanaryTestは行わない
log.Infof("service '%s' has no load balancer. will skip canary tests", *envars.NextServiceName)
envars.SkipCanary = aws.Bool(true)
}
// next serviceのperiodic healthが安定し、current serviceのtaskの数が0になるまで繰り返す
for {
log.Infof("=== preparing for %dth roll out ===", totalRollOutCnt)
if estimatedRollOutCount <= totalRollOutCnt {
return NewErrorf(
"estimated roll out attempts count exceeded: estimated=%d, rolledOut=%d, replaced=%d/%d",
estimatedRollOutCount, totalRollOutCnt, totalReplacedCnt, originalDesiredCount,
)
}
replaceCnt := int64(EnsureReplaceCount(totalReplacedCnt, totalRollOutCnt, originalDesiredCount))
scaleCnt := totalReplacedCnt + replaceCnt
// Phase1: service-nextにtask-nextを指定数配置
log.Infof("%dth roll out starting: will replace %d tasks", totalRollOutCnt, replaceCnt)
log.Infof("start adding of next tasks. this will update '%s' desired count %d to %d", *envars.NextServiceName, totalReplacedCnt, scaleCnt)
err := envars.UpdateDesiredCount(ctx, envars.NextServiceName, targetGroupArn, &originalDesiredCount, &scaleCnt, true)
if err != nil {
log.Errorf("failed to add next tasks due to: %s", err)
return err
}
// Phase2: service-nextのperiodic healthを計測
if *envars.SkipCanary {
log.Infof("🤫 %dth canary test skipped.", totalRollOutCnt)
} else if err := envars.CanaryTest(ctx.Cw, loadBalancerArn, targetGroupArn, totalRollOutCnt); err != nil {
return err
}
// Phase3: service-currentからタスクを指定数消す
descaledCnt := originalDesiredCount - totalReplacedCnt - replaceCnt
log.Infof("updating service '%s' desired count to %d", *envars.CurrentServiceName, descaledCnt)
if err := envars.UpdateDesiredCount(ctx, envars.CurrentServiceName, targetGroupArn, &originalDesiredCount, &descaledCnt, false); err != nil {
log.Errorf("failed to roll out tasks due to: %s", err.Error())
return err
}
totalReplacedCnt += replaceCnt
log.Infof(
"%dth roll out successfully completed. %d/%d tasks rolled out",
totalRollOutCnt, totalReplacedCnt, originalDesiredCount,
)
totalRollOutCnt += 1
// Phase4: ロールアウトが終わったかどうかを確認
var (
oldTaskCount int64
newTaskCount int64
)
if out, err := ctx.Ecs.DescribeServices(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{
envars.CurrentServiceName,
envars.NextServiceName,
},
}); err != nil {
log.Errorf("failed to list tasks due to: %s", err.Error())
return err
} else {
oldTaskCount = *out.Services[0].DesiredCount
newTaskCount = *out.Services[1].DesiredCount
}
if oldTaskCount == 0 && newTaskCount >= originalDesiredCount {
// ロールアウトが終わったら最終検証を行う
log.Infof("estimated roll out completed. Do final canary test...")
if *envars.SkipCanary {
log.Infof("😑 final canary test skipped...")
} else if err := envars.CanaryTest(ctx.Cw, loadBalancerArn, targetGroupArn, totalRollOutCnt); err != nil {
log.Errorf("final canary test has failed due to: %s", err)
return err
}
log.Infof("☀️all tasks successfully have been rolled out!☀️")
// service-currentを消す
log.Infof("deleting service '%s'...", *envars.CurrentServiceName)
if _, err := ctx.Ecs.DeleteService(&ecs.DeleteServiceInput{
Cluster: envars.Cluster,
Service: envars.CurrentServiceName,
}); err != nil {
log.Errorf("failed to delete service '%s due to: %s'", *envars.CurrentServiceName, err)
return err
}
log.Infof("service '%s' has been successfully deleted", *envars.CurrentServiceName)
return nil
} else {
log.Infof(
"roll out is continuing. %d tasks running on '%s', %d tasks on '%s'",
oldTaskCount, *envars.CurrentServiceName,
newTaskCount, *envars.NextServiceName,
)
}
}
}
func (envars *Envars) CreateNextTaskDefinition(awsEcs ecsiface.ECSAPI) (*ecs.TaskDefinition, error) {
if !isEmpty(envars.NextTaskDefinitionArn) {
o, err := awsEcs.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: envars.NextTaskDefinitionArn,
})
if err != nil {
log.Errorf(
"failed to describe next task definition '%s' due to: %s",
*envars.NextTaskDefinitionArn, err,
)
return nil, err
}
return o.TaskDefinition, nil
}
data, err := base64.StdEncoding.DecodeString(*envars.NextTaskDefinitionBase64)
if err != nil {
log.Errorf("failed to decode task definition base64 due to :%s", err)
return nil, err
}
td := &ecs.RegisterTaskDefinitionInput{}
if err := json.Unmarshal(data, td); err != nil {
log.Errorf("failed to unmarshal task definition due to: %s", err)
return nil, err
}
if out, err := awsEcs.RegisterTaskDefinition(td); err != nil {
return nil, err
} else {
return out.TaskDefinition, nil
}
}
func (envars *Envars) CreateNextService(awsEcs ecsiface.ECSAPI, nextTaskDefinitionArn *string) (error) {
service := &ecs.CreateServiceInput{}
if envars.NextServiceDefinitionBase64 == nil {
// サービス定義が与えられなかった場合はタスク定義と名前だけ変えたservice-currentのレプリカを作成する
log.Infof("nextServiceDefinitionBase64 not provided. try to create replica service")
out, err := awsEcs.DescribeServices(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{envars.CurrentServiceName},
})
if len(out.Failures) > 0 || err != nil {
log.Errorf("failed to describe current service due to: %s", err)
return err
}
s := out.Services[0]
service = &ecs.CreateServiceInput{
Cluster: envars.Cluster,
DeploymentConfiguration: s.DeploymentConfiguration,
DesiredCount: aws.Int64(1),
HealthCheckGracePeriodSeconds: s.HealthCheckGracePeriodSeconds,
LaunchType: s.LaunchType,
LoadBalancers: s.LoadBalancers,
NetworkConfiguration: s.NetworkConfiguration,
PlacementConstraints: s.PlacementConstraints,
PlacementStrategy: s.PlacementStrategy,
PlatformVersion: s.PlatformVersion,
SchedulingStrategy: s.SchedulingStrategy,
ServiceName: envars.NextServiceName,
ServiceRegistries: s.ServiceRegistries,
TaskDefinition: nextTaskDefinitionArn,
}
} else {
data, err := base64.StdEncoding.DecodeString(*envars.NextServiceDefinitionBase64)
if err != nil {
log.Errorf("failed to decode service definition base64 due to : %s", err)
return err
}
if err := json.Unmarshal(data, service); err != nil {
log.Errorf("failed to unmarshal service definition base64 due to: %s", err)
return err
}
service.ServiceName = envars.NextServiceName
service.TaskDefinition = nextTaskDefinitionArn
*service.DesiredCount = 1
}
log.Infof("creating next service '%s' with desiredCount=1", *envars.NextServiceName)
if _, err := awsEcs.CreateService(service); err != nil {
log.Errorf("failed to create next service due to: %s", err)
return err
}
log.Infof("waiting for service '%s' to become STABLE", *envars.NextServiceName)
if err := awsEcs.WaitUntilServicesStable(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{envars.NextServiceName},
}); err != nil {
log.Errorf("'%s' hasn't reached STABLE state within maximum attempt windows due to: %s", err)
return err
}
log.Infof("service '%s' has reached STABLE state", *envars.NextServiceName)
return nil
}
func (envars *Envars) EnsureHealthyTargets(
alb elbv2iface.ELBV2API,
targetGroupArn *string,
originalCount *int64,
retryCount int,
) error {
o, err := alb.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{
TargetGroupArn: targetGroupArn,
})
if err != nil {
log.Errorf("failed to describe target group '%s' due to: %s", *targetGroupArn, err)
return err
}
healthyCount := int64(0)
for _, v := range o.TargetHealthDescriptions {
if *v.TargetHealth.State == "healthy" {
healthyCount++
}
}
if healthyCount < *originalCount {
if retryCount < 1 {
// healthyターゲットが足りない場合は、一度だけリトライする
log.Warnf(
"healthy targets count in tg '%s' is less than original count(%d). retry checking after %d sec",
*targetGroupArn, *originalCount, *envars.RollOutPeriod,
)
<-newTimer(time.Duration(*envars.RollOutPeriod) * time.Second).C
return envars.EnsureHealthyTargets(alb, targetGroupArn, originalCount, retryCount+1)
} else {
return NewErrorf(
"tg '%s' currently doesn't have enough healthy targets (%d/%d)",
*targetGroupArn, healthyCount, *originalCount,
)
}
}
return nil
}
func (envars *Envars) UpdateDesiredCount(
ctx *Context,
serviceName *string,
targetGroupArn *string,
originalCount *int64,
count *int64,
increase bool,
) error {
log.Infof("start ensuring healthy targets...")
var service *ecs.Service
if o, err := ctx.Ecs.UpdateService(&ecs.UpdateServiceInput{
Cluster: envars.Cluster,
Service: serviceName,
DesiredCount: count,
}); err != nil {
log.Errorf("failed to update '%s' desired count to %d due to: %s", *serviceName, *count, err)
return err
} else {
service = o.Service
}
log.Infof(
"waiting until %d tasks running on service '%s'...",
*count, *serviceName,
)
if err := ctx.Ecs.WaitUntilServicesStable(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{serviceName},
}); err != nil {
log.Errorf("failed to wait service-stable due to: %s", err)
return err
}
// LBがないサービスはここで終わり
if targetGroupArn == nil {
return nil
}
o, err := ctx.Alb.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
TargetGroupArns: []*string{targetGroupArn},
})
if err != nil {
log.Errorf("failed to describe target group '%s' due to: %s", *targetGroupArn, err)
return err
}
tg := o.TargetGroups[0]
var standBy time.Duration
if increase {
// TargetGroupに新しいタスクが登録されるまで 待つ
standBy = time.Duration(
*service.HealthCheckGracePeriodSeconds+*tg.HealthCheckIntervalSeconds*(*tg.HealthyThresholdCount),
) * time.Second
log.Infof(
"waiting for %f seconds until new tasks are registered to target group '%s'",
standBy.Seconds(), *tg.TargetGroupName,
)
} else {
// target groupのderegistration delayだけ待つ
var delay int64
if o, err := ctx.Alb.DescribeTargetGroupAttributes(&elbv2.DescribeTargetGroupAttributesInput{
TargetGroupArn: targetGroupArn,
}); err != nil {
return err
} else {
for _, v := range o.Attributes {
if *v.Key == "deregistration_delay.timeout_seconds" {
o, err := strconv.ParseInt(*v.Value, 10, 64)
if err != nil {
log.Warnf("failed to parse deregistration_delay.timeout_seconds due to: %s", err)
} else {
delay = o
}
}
}
}
standBy = time.Duration(delay) * time.Second
log.Infof(
"waiting for %f seconds until old tasks are deregistered from target group '%s'",
standBy.Seconds(), *tg.TargetGroupName,
)
}
<-newTimer(standBy).C
if err := envars.EnsureHealthyTargets(ctx.Alb, targetGroupArn, originalCount, 0); err != nil {
log.Errorf("failed to ensure healthy target due to: %s", err)
return err
}
return nil
}
func (envars *Envars) Rollback(
ctx *Context,
originalTaskCount *int64,
targetGroupArn *string,
) error {
// service-currentをもとのdesiredCountに戻す
log.Infof("rollback '%s' desired count to %d", *envars.CurrentServiceName, *originalTaskCount)
if err := envars.UpdateDesiredCount(ctx, envars.CurrentServiceName, targetGroupArn, originalTaskCount, originalTaskCount, true); err != nil {
log.Errorf("failed to rollback desired count of %s due to: %s", *envars.CurrentServiceName, err)
return err
}
// service-nextを消す
log.Infof("'%s' desired count is now %d", *envars.NextServiceName, *originalTaskCount)
if err := envars.UpdateDesiredCount(ctx, envars.NextServiceName, targetGroupArn, originalTaskCount, aws.Int64(0), false); err != nil {
log.Errorf("failed to update '%s' desired count to 0 due to: %s", *envars.NextServiceName, err)
return err
}
log.Infof("deleting service '%s'...", *envars.NextServiceName)
if _, err := ctx.Ecs.DeleteService(&ecs.DeleteServiceInput{
Cluster: envars.Cluster,
Service: envars.NextServiceName,
}); err != nil {
log.Errorf("failed to delete service '%s' due to: %s", *envars.NextServiceName, err)
return err
}
log.Infof("service '%s' has successfully deleted", *envars.NextServiceName)
log.Infof("waiting for service become to be inactive...")
if err := ctx.Ecs.WaitUntilServicesInactive(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{envars.NextServiceName},
}); err != nil {
log.Errorf("deleted service '%s' hasn't reached INACTIVE state within maximum attempt windows due to: %s", err)
return err
}
log.Infof("service '%s' has been eliminated", *envars.NextServiceName)
// TODO: 2018/08/24 ロールバック後もカナリアテストを行うべきか? by sakurai
return nil
}