-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollout.go
498 lines (482 loc) · 16.3 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
498
package cage
import (
"context"
"fmt"
"time"
"github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"golang.org/x/xerrors"
)
type RollOutInput struct {
// UpdateService is a flag to update service with changed configurations except for task definition
UpdateService bool
}
type RollOutResult struct {
StartTime time.Time
EndTime time.Time
ServiceIntact bool
}
func (c *cage) RollOut(ctx context.Context, input *RollOutInput) (*RollOutResult, error) {
ret := &RollOutResult{
StartTime: c.Time.Now(),
ServiceIntact: true,
}
var aggregatedError error
throw := func(err error) (*RollOutResult, error) {
ret.EndTime = c.Time.Now()
aggregatedError = err
return ret, err
}
defer func(result *RollOutResult) {
ret.EndTime = c.Time.Now()
}(ret)
var service ecstypes.Service
if out, err := c.Ecs.DescribeServices(ctx, &ecs.DescribeServicesInput{
Cluster: &c.Env.Cluster,
Services: []string{
c.Env.Service,
},
}); err != nil {
log.Errorf("failed to describe current service due to: %s", err)
return throw(err)
} else if len(out.Services) == 0 {
return throw(xerrors.Errorf("service '%s' doesn't exist. Run 'cage up' or create service before rolling out", c.Env.Service))
} else {
service = out.Services[0]
}
if *service.Status != "ACTIVE" {
return throw(xerrors.Errorf("😵 '%s' status is '%s'. Stop rolling out", c.Env.Service, *service.Status))
}
if service.LaunchType == ecstypes.LaunchTypeEc2 && c.Env.CanaryInstanceArn == "" {
return throw(xerrors.Errorf("🥺 --canaryInstanceArn is required when LaunchType = 'EC2'"))
}
log.Infof("ensuring next task definition...")
var nextTaskDefinition *ecstypes.TaskDefinition
if o, err := c.CreateNextTaskDefinition(ctx); err != nil {
log.Errorf("failed to register next task definition due to: %s", err)
return throw(err)
} else {
nextTaskDefinition = o
}
if input.UpdateService {
log.Info("--updateService flag is set. use provided service configurations for canary test instead of current service")
}
log.Infof("starting canary task...")
canaryTask, startCanaryTaskErr := c.StartCanaryTask(ctx, nextTaskDefinition, input)
// ensure canary task stopped after rolling out either success or failure
defer func(canaryTask *CanaryTask, result *RollOutResult) {
if canaryTask.taskArn == nil {
return
}
if err := c.StopCanaryTask(ctx, canaryTask); err != nil {
log.Fatalf("failed to stop canary task '%s': %s", *canaryTask.taskArn, err)
}
if aggregatedError == nil {
log.Infof(
"🐥 service '%s' successfully rolled out to '%s:%d'!",
c.Env.Service, *nextTaskDefinition.Family, nextTaskDefinition.Revision,
)
} else {
log.Errorf("😥 %s", aggregatedError)
}
}(&canaryTask, ret)
if startCanaryTaskErr != nil {
log.Errorf("failed to start canary task due to: %s", startCanaryTaskErr)
return throw(startCanaryTaskErr)
}
log.Infof("😷 ensuring canary task container(s) to become healthy...")
if err := c.waitUntilContainersBecomeHealthy(ctx, *canaryTask.taskArn, nextTaskDefinition); err != nil {
return throw(err)
}
log.Info("🤩 canary task container(s) is healthy!")
log.Infof("canary task '%s' ensured.", *canaryTask.taskArn)
if canaryTask.target != nil {
log.Infof("😷 ensuring canary task to become healthy...")
if err := c.EnsureTaskHealthy(
ctx,
*canaryTask.taskArn,
canaryTask.target,
); err != nil {
return throw(err)
}
log.Info("🤩 canary task is healthy!")
}
ret.ServiceIntact = false
log.Infof(
"updating the task definition of '%s' into '%s:%d'...",
c.Env.Service, *nextTaskDefinition.Family, nextTaskDefinition.Revision,
)
updateInput := &ecs.UpdateServiceInput{
Cluster: &c.Env.Cluster,
Service: &c.Env.Service,
TaskDefinition: nextTaskDefinition.TaskDefinitionArn,
}
if input.UpdateService {
updateInput.LoadBalancers = c.Env.ServiceDefinitionInput.LoadBalancers
updateInput.NetworkConfiguration = c.Env.ServiceDefinitionInput.NetworkConfiguration
updateInput.ServiceConnectConfiguration = c.Env.ServiceDefinitionInput.ServiceConnectConfiguration
updateInput.ServiceRegistries = c.Env.ServiceDefinitionInput.ServiceRegistries
updateInput.PlatformVersion = c.Env.ServiceDefinitionInput.PlatformVersion
updateInput.VolumeConfigurations = c.Env.ServiceDefinitionInput.VolumeConfigurations
}
if _, err := c.Ecs.UpdateService(ctx, updateInput); err != nil {
return throw(err)
}
log.Infof("waiting for service '%s' to be stable...", c.Env.Service)
if err := ecs.NewServicesStableWaiter(c.Ecs).Wait(ctx, &ecs.DescribeServicesInput{
Cluster: &c.Env.Cluster,
Services: []string{c.Env.Service},
}, c.MaxWait); err != nil {
return throw(err)
}
log.Infof("🥴 service '%s' has become to be stable!", c.Env.Service)
ret.EndTime = c.Time.Now()
return ret, nil
}
func (c *cage) EnsureTaskHealthy(
ctx context.Context,
taskArn string,
p *CanaryTarget,
) error {
log.Infof("checking the health state of canary task...")
var unusedCount = 0
var initialized = false
var recentState *elbv2types.TargetHealthStateEnum
for {
<-c.Time.NewTimer(time.Duration(15) * time.Second).C
if o, err := c.Alb.DescribeTargetHealth(ctx, &elbv2.DescribeTargetHealthInput{
TargetGroupArn: &p.targetGroupArn,
Targets: []elbv2types.TargetDescription{{
Id: &p.targetId,
Port: &p.targetPort,
AvailabilityZone: &p.availabilityZone,
}},
}); err != nil {
return err
} else {
recentState = GetTargetIsHealthy(o, &p.targetId, &p.targetPort)
if recentState == nil {
return xerrors.Errorf("'%s' is not registered to the target group '%s'", p.targetId, p.targetGroupArn)
}
log.Infof("canary task '%s' (%s:%d) state is: %s", taskArn, p.targetId, p.targetPort, *recentState)
switch *recentState {
case "healthy":
return nil
case "initial":
initialized = true
log.Infof("still checking the state...")
continue
case "unused":
unusedCount++
if !initialized && unusedCount < 5 {
continue
}
default:
}
}
// unhealthy, draining, unused
log.Errorf("😨 canary task '%s' is unhealthy", taskArn)
return xerrors.Errorf(
"canary task '%s' (%s:%d) hasn't become to be healthy. The most recent state: %s",
taskArn, p.targetId, p.targetPort, *recentState,
)
}
}
func GetTargetIsHealthy(o *elbv2.DescribeTargetHealthOutput, targetId *string, targetPort *int32) *elbv2types.TargetHealthStateEnum {
for _, desc := range o.TargetHealthDescriptions {
if *desc.Target.Id == *targetId && *desc.Target.Port == *targetPort {
return &desc.TargetHealth.State
}
}
return nil
}
func (c *cage) DescribeSubnet(ctx context.Context, subnetId *string) (ec2types.Subnet, error) {
if o, err := c.Ec2.DescribeSubnets(ctx, &ec2.DescribeSubnetsInput{
SubnetIds: []string{*subnetId},
}); err != nil {
return ec2types.Subnet{}, err
} else {
return o.Subnets[0], nil
}
}
type CanaryTarget struct {
targetGroupArn string
targetId string
targetPort int32
availabilityZone string
}
type CanaryTask struct {
taskArn *string
target *CanaryTarget
}
func (c *cage) StartCanaryTask(
ctx context.Context,
nextTaskDefinition *ecstypes.TaskDefinition,
input *RollOutInput,
) (CanaryTask, error) {
var networkConfiguration *ecstypes.NetworkConfiguration
var platformVersion *string
var loadBalancers []ecstypes.LoadBalancer
var result CanaryTask
if input.UpdateService {
networkConfiguration = c.Env.ServiceDefinitionInput.NetworkConfiguration
platformVersion = c.Env.ServiceDefinitionInput.PlatformVersion
loadBalancers = c.Env.ServiceDefinitionInput.LoadBalancers
} else {
if o, err := c.Ecs.DescribeServices(ctx, &ecs.DescribeServicesInput{
Cluster: &c.Env.Cluster,
Services: []string{c.Env.Service},
}); err != nil {
return result, err
} else {
service := o.Services[0]
networkConfiguration = service.NetworkConfiguration
platformVersion = service.PlatformVersion
loadBalancers = service.LoadBalancers
}
}
// Phase 1: Start canary task
var taskArn *string
if c.Env.CanaryInstanceArn != "" {
// ec2
startTask := &ecs.StartTaskInput{
Cluster: &c.Env.Cluster,
Group: aws.String(fmt.Sprintf("cage:canary-task:%s", c.Env.Service)),
NetworkConfiguration: networkConfiguration,
TaskDefinition: nextTaskDefinition.TaskDefinitionArn,
ContainerInstances: []string{c.Env.CanaryInstanceArn},
}
if o, err := c.Ecs.StartTask(ctx, startTask); err != nil {
return result, err
} else {
taskArn = o.Tasks[0].TaskArn
}
} else {
// fargate
if o, err := c.Ecs.RunTask(ctx, &ecs.RunTaskInput{
Cluster: &c.Env.Cluster,
Group: aws.String(fmt.Sprintf("cage:canary-task:%s", c.Env.Service)),
NetworkConfiguration: networkConfiguration,
TaskDefinition: nextTaskDefinition.TaskDefinitionArn,
LaunchType: ecstypes.LaunchTypeFargate,
PlatformVersion: platformVersion,
}); err != nil {
return result, err
} else {
taskArn = o.Tasks[0].TaskArn
}
}
result.taskArn = taskArn
// Phase 2: Wait until canary task is running
log.Infof("🥚 waiting for canary task '%s' is running...", *taskArn)
if err := ecs.NewTasksRunningWaiter(c.Ecs).Wait(ctx, &ecs.DescribeTasksInput{
Cluster: &c.Env.Cluster,
Tasks: []string{*taskArn},
}, c.MaxWait); err != nil {
return result, err
}
log.Infof("🐣 canary task '%s' is running!", *taskArn)
if len(loadBalancers) == 0 {
log.Infof("no load balancer is attached to service '%s'. skip registration to target group", c.Env.Service)
log.Infof("wait %d seconds for ensuring the task goes stable", c.Env.CanaryTaskIdleDuration)
wait := make(chan bool)
go func() {
duration := c.Env.CanaryTaskIdleDuration
for duration > 0 {
log.Infof("still waiting...; %d seconds left", duration)
wt := 10
if duration < 10 {
wt = duration
}
<-c.Time.NewTimer(time.Duration(wt) * time.Second).C
duration -= 10
}
wait <- true
}()
<-wait
o, err := c.Ecs.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &c.Env.Cluster,
Tasks: []string{*taskArn},
})
if err != nil {
return result, err
}
task := o.Tasks[0]
if *task.LastStatus != "RUNNING" {
return result, xerrors.Errorf("😫 canary task has stopped: %s", *task.StoppedReason)
}
return result, nil
}
// Phase 3: Get task details after network interface is attached
var task ecstypes.Task
if o, err := c.Ecs.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &c.Env.Cluster,
Tasks: []string{*taskArn},
}); err != nil {
return result, err
} else {
task = o.Tasks[0]
}
var targetId *string
var targetPort *int32
var subnet ec2types.Subnet
for _, container := range nextTaskDefinition.ContainerDefinitions {
if *container.Name == *loadBalancers[0].ContainerName {
targetPort = container.PortMappings[0].HostPort
}
}
if targetPort == nil {
return result, xerrors.Errorf("couldn't find host port in container definition")
}
if c.Env.CanaryInstanceArn == "" { // Fargate
details := task.Attachments[0].Details
var subnetId *string
var privateIp *string
for _, v := range details {
if *v.Name == "subnetId" {
subnetId = v.Value
} else if *v.Name == "privateIPv4Address" {
privateIp = v.Value
}
}
if subnetId == nil || privateIp == nil {
return result, xerrors.Errorf("couldn't find subnetId or privateIPv4Address in task details")
}
if o, err := c.Ec2.DescribeSubnets(ctx, &ec2.DescribeSubnetsInput{
SubnetIds: []string{*subnetId},
}); err != nil {
return result, err
} else {
subnet = o.Subnets[0]
}
targetId = privateIp
log.Infof("canary task was placed: privateIp = '%s', hostPort = '%d', az = '%s'", *targetId, *targetPort, *subnet.AvailabilityZone)
} else {
var containerInstance ecstypes.ContainerInstance
if outputs, err := c.Ecs.DescribeContainerInstances(ctx, &ecs.DescribeContainerInstancesInput{
Cluster: &c.Env.Cluster,
ContainerInstances: []string{c.Env.CanaryInstanceArn},
}); err != nil {
return result, err
} else {
containerInstance = outputs.ContainerInstances[0]
}
if o, err := c.Ec2.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: []string{*containerInstance.Ec2InstanceId},
}); err != nil {
return result, err
} else if sn, err := c.DescribeSubnet(ctx, o.Reservations[0].Instances[0].SubnetId); err != nil {
return result, err
} else {
targetId = containerInstance.Ec2InstanceId
subnet = sn
}
log.Infof("canary task was placed: instanceId = '%s', hostPort = '%d', az = '%s'", *targetId, *targetPort, *subnet.AvailabilityZone)
}
if _, err := c.Alb.RegisterTargets(ctx, &elbv2.RegisterTargetsInput{
TargetGroupArn: loadBalancers[0].TargetGroupArn,
Targets: []elbv2types.TargetDescription{{
AvailabilityZone: subnet.AvailabilityZone,
Id: targetId,
Port: targetPort,
}},
}); err != nil {
return result, err
}
result.target = &CanaryTarget{
targetGroupArn: *loadBalancers[0].TargetGroupArn,
targetId: *targetId,
targetPort: *targetPort,
availabilityZone: *subnet.AvailabilityZone,
}
return result, nil
}
func (c *cage) waitUntilContainersBecomeHealthy(ctx context.Context, taskArn string, nextTaskDefinition *ecstypes.TaskDefinition) error {
containerHasHealthChecks := map[string]struct{}{}
for _, definition := range nextTaskDefinition.ContainerDefinitions {
if definition.HealthCheck != nil {
containerHasHealthChecks[*definition.Name] = struct{}{}
}
}
for count := 0; count < 10; count++ {
<-c.Time.NewTimer(time.Duration(15) * time.Second).C
log.Infof("canary task '%s' waits until %d container(s) become healthy", taskArn, len(containerHasHealthChecks))
if o, err := c.Ecs.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &c.Env.Cluster,
Tasks: []string{taskArn},
}); err != nil {
return err
} else {
task := o.Tasks[0]
if *task.LastStatus != "RUNNING" {
return xerrors.Errorf("😫 canary task has stopped: %s", *task.StoppedReason)
}
for _, container := range task.Containers {
if _, ok := containerHasHealthChecks[*container.Name]; !ok {
continue
}
if container.HealthStatus != ecstypes.HealthStatusHealthy {
log.Infof("container '%s' is not healthy: %s", *container.Name, container.HealthStatus)
continue
}
delete(containerHasHealthChecks, *container.Name)
}
if len(containerHasHealthChecks) == 0 {
return nil
}
}
}
return xerrors.Errorf("😨 canary task hasn't become to be healthy")
}
func (c *cage) StopCanaryTask(ctx context.Context, input *CanaryTask) error {
if input.target == nil {
log.Info("no load balancer is attached to service. Skip deregisteration.")
} else {
log.Infof("deregistering the canary task from target group '%s'...", input.target.targetId)
if _, err := c.Alb.DeregisterTargets(ctx, &elbv2.DeregisterTargetsInput{
TargetGroupArn: &input.target.targetGroupArn,
Targets: []elbv2types.TargetDescription{{
AvailabilityZone: &input.target.availabilityZone,
Id: &input.target.targetId,
Port: &input.target.targetPort,
}},
}); err != nil {
return err
}
if err := elbv2.NewTargetDeregisteredWaiter(c.Alb).Wait(ctx, &elbv2.DescribeTargetHealthInput{
TargetGroupArn: &input.target.targetGroupArn,
Targets: []elbv2types.TargetDescription{{
AvailabilityZone: &input.target.availabilityZone,
Id: &input.target.targetId,
Port: &input.target.targetPort,
}},
}, c.MaxWait); err != nil {
return err
}
log.Infof(
"canary task '%s' has successfully been deregistered from target group '%s'",
*input.taskArn, input.target.targetId,
)
}
log.Infof("stopping the canary task '%s'...", *input.taskArn)
if _, err := c.Ecs.StopTask(ctx, &ecs.StopTaskInput{
Cluster: &c.Env.Cluster,
Task: input.taskArn,
}); err != nil {
return err
}
if err := ecs.NewTasksStoppedWaiter(c.Ecs).Wait(ctx, &ecs.DescribeTasksInput{
Cluster: &c.Env.Cluster,
Tasks: []string{*input.taskArn},
}, c.MaxWait); err != nil {
return err
}
log.Infof("canary task '%s' has successfully been stopped", *input.taskArn)
return nil
}