/
asgs.go
695 lines (631 loc) · 21.2 KB
/
asgs.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package ec2
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"text/template"
"time"
"github.com/aws/aws-k8s-tester/ec2config"
awscfn "github.com/aws/aws-k8s-tester/pkg/aws/cloudformation"
awsapiec2 "github.com/aws/aws-k8s-tester/pkg/aws/ec2"
"github.com/aws/aws-k8s-tester/version"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/dustin/go-humanize"
"go.uber.org/zap"
)
// MAKE SURE TO SYNC THE DEFAULT VALUES in "ec2config"
/*
https://docs.aws.amazon.com/eks/latest/userguide/launch-workers.html
https://github.com/awslabs/amazon-eks-ami/blob/master/amazon-eks-nodegroup.yaml
https://raw.githubusercontent.com/awslabs/amazon-eks-ami/master/amazon-eks-nodegroup.yaml
https://aws.amazon.com/about-aws/whats-new/2019/09/amazon-eks-provides-eks-optimized-ami-metadata-via-ssm-parameters/
e.g.
/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
e.g.
/aws/service/eks/optimized-ami/1.15/amazon-linux-2/recommended/image_id
/aws/service/bottlerocket/aws-k8s-1.15/x86_64/latest/image_id
*/
// TemplateASG is the CloudFormation template for ASG.
// "must specify one of the following properties: LaunchConfigurationName,
// LaunchTemplate, InstanceId, or MixedInstancesPolicy"
// ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html
// ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html
// ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata.html
// ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-networkinterface.html
const TemplateASG = `
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Amazon EC2 ASG'
Parameters:
ASGName:
Type: String
Default: aws-k8s-tester-ec2-asg
Description: EC2 AutoScalingGroup name
ASGLaunchTemplateName:
Type: String
Default: aws-k8s-tester-ec2-asg-launch-template
Description: EC2 LaunchConfiguration name
RoleName:
Type: String
Default: aws-k8s-tester-ec2-role
Description: EC2 Role name
PublicSubnetIDs:
Type: List<AWS::EC2::Subnet::Id>
Description: The public subnet IDs where workers can be created.
SecurityGroupID:
Type: AWS::EC2::SecurityGroup::Id
Description: EC2 security group ID
RemoteAccessKeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: EC2 SSH key name
Default: aws-k8s-tester-ec2-key
ImageID:
Type: String
Default: ""
Description: (Optional) Custom image ID. This value overrides any AWS Systems Manager Parameter Store value specified above.
ImageIDSSMParameter:
Type : AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Description: AWS Systems Manager Parameter Store parameter of the AMI ID.
InstanceTypes:
Type: CommaDelimitedList
Default: c5.xlarge
Description: EC2 instance types
InstanceTypesCount:
Type: Number
Default: 1
MinValue: 1
MaxValue: 4
Description: The number of instance types
VolumeSize:
Type: Number
Default: 40
MinValue: 8
MaxValue: 1024
Description: Size of the root disk for the EC2 instances, in GiB.
ASGMinSize:
Type: Number
Description: Minimum size auto scaling group
Default: 1
MinValue: 1
MaxValue: 1000
ASGMaxSize:
Type: Number
Description: Maximum size auto scaling group
Default: 1
MinValue: 1
MaxValue: 1000
ASGDesiredCapacity:
Type: Number
Description: Desired size auto scaling group
Default: 1
MinValue: 1
MaxValue: 1000
Conditions:
HasImageID:
Fn::Not:
- Fn::Equals:
- Ref: ImageID
- ""
Has2InstanceTypes:
Fn::Or:
- Fn::Equals:
- Ref: InstanceTypesCount
- 2
- Fn::Equals:
- Ref: InstanceTypesCount
- 3
- Fn::Equals:
- Ref: InstanceTypesCount
- 4
Has3InstanceTypes:
Fn::Or:
- Fn::Equals:
- Ref: InstanceTypesCount
- 3
- Fn::Equals:
- Ref: InstanceTypesCount
- 4
Has4InstanceTypes:
Fn::Equals:
- Ref: InstanceTypesCount
- 4
Resources:
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- !Ref RoleName
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata.html
NodeLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
DependsOn:
- InstanceProfile
{{ if ne .Metadata "" }}{{.Metadata}}{{ end }}
Properties:
LaunchTemplateName: !Ref ASGLaunchTemplateName
LaunchTemplateData:
IamInstanceProfile:
Arn: !GetAtt InstanceProfile.Arn
ImageId:
Fn::If:
- HasImageID
- !Ref ImageID
- !Ref ImageIDSSMParameter
KeyName: !Ref RemoteAccessKeyName
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp2
VolumeSize: !Ref VolumeSize
DeleteOnTermination: true
Monitoring:
Enabled: true
# need this for public DNS + SSH access
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeleteOnTermination: true
DeviceIndex: 0
Groups:
- !Ref SecurityGroupID
TagSpecifications:
- ResourceType: instance
Tags:
- { Key: Name, Value: !Sub '${ASGName}-instance' }
- ResourceType: volume
Tags:
- { Key: Name, Value: !Sub '${ASGName}-volume' }
{{ if ne .UserData "" }}{{.UserData}}{{ end }}
# specify "MixedInstancesPolicy" or "LaunchConfiguration"
ASG:
Type: AWS::AutoScaling::AutoScalingGroup
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: !Ref ASGDesiredCapacity
MaxBatchSize: 1
SuspendProcesses:
- HealthCheck
- ReplaceUnhealthy
- AZRebalance
- AlarmNotification
- ScheduledActions
Properties:
AutoScalingGroupName: !Ref ASGName
MinSize: !Ref ASGMinSize
MaxSize: !Ref ASGMaxSize
DesiredCapacity: !Ref ASGDesiredCapacity
VPCZoneIdentifier: !Ref PublicSubnetIDs
MetricsCollection:
- Granularity: "1Minute"
Tags:
- Key: Name
PropagateAtLaunch: true
Value: !Ref ASGName
MixedInstancesPolicy:
InstancesDistribution:
OnDemandAllocationStrategy: "prioritized"
LaunchTemplate:
LaunchTemplateSpecification:
LaunchTemplateId: !Ref NodeLaunchTemplate
Version: !GetAtt NodeLaunchTemplate.LatestVersionNumber
Overrides:
- InstanceType: !Select [ 0, !Ref InstanceTypes ]
- Fn::If:
- Has2InstanceTypes
- !Select [ 1, !Ref InstanceTypes ]
- !Ref AWS::NoValue
- Fn::If:
- Has3InstanceTypes
- !Select [ 2, !Ref InstanceTypes ]
- !Ref AWS::NoValue
- Fn::If:
- Has4InstanceTypes
- !Select [ 3, !Ref InstanceTypes ]
- !Ref AWS::NoValue
HealthCheckType: EC2
HealthCheckGracePeriod: 300
Outputs:
ASGName:
Value: !Ref ASG
InstanceProfileARN:
Value: !GetAtt InstanceProfile.Arn
`
const metadataAL2InstallSSM = ` Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- InstallAWSCLI
- InstallSSM
InstallAWSCLI:
packages:
# zsh: most Amazon users stations are set to zsh as a default
# unzip: required to install aws cli
# wget: under the hood SPIE requires wget
yum:
unzip: []
zsh: []
wget: []
commands:
01InstallAWSCLI:
# AL2 doesn't have aws cli installed
command: |
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/bin/aws
which aws
rm -r awscli*
InstallSSM:
packages:
rpm:
ssm:
- Fn::Sub: 'https://s3.${AWS::Region}.${AWS::URLSuffix}/amazon-ssm-${AWS::Region}/latest/linux_amd64/amazon-ssm-agent.rpm'`
const userDataAL2InstallSSM = ` UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash
set -xeu
sudo yum update -y \
&& sudo yum install -y \
gcc \
zlib-devel \
openssl-devel \
ncurses-devel \
git \
wget \
jq \
tar \
curl \
unzip \
screen \
mercurial \
aws-cfn-bootstrap \
awscli \
chrony \
conntrack \
nfs-utils \
socat
# Make sure Amazon Time Sync Service starts on boot.
sudo chkconfig chronyd on
# Make sure that chronyd syncs RTC clock to the kernel.
cat <<EOF | sudo tee -a /etc/chrony.conf
# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync
EOF
# https://docs.aws.amazon.com/inspector/latest/userguide/inspector_installing-uninstalling-agents.html
curl -O https://inspector-agent.amazonaws.com/linux/latest/install
chmod +x install
sudo ./install -u false
rm install
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo amazon-linux-extras install docker -y
sudo systemctl daemon-reload
sudo systemctl enable docker || true
sudo systemctl start docker || true
sudo systemctl restart docker || true
sudo systemctl status docker --full --no-pager || true
sudo usermod -aG docker ec2-user || true
# su - ec2-user
# or logout and login to use docker without 'sudo'
id -nG
sudo docker version
sudo docker info`
type templateASG struct {
Metadata string
UserData string
}
func (ts *Tester) createASGs() (err error) {
createStart := time.Now()
defer func() {
ts.cfg.CreateTook += time.Since(createStart)
ts.cfg.CreateTookString = ts.cfg.CreateTook.String()
ts.cfg.Sync()
}()
if ts.cfg.Up {
ts.lg.Info("cluster is up; no need to create cluster")
return nil
}
ts.lg.Info("creating ASGs using CFN", zap.String("name", ts.cfg.Name))
for asgName, cur := range ts.cfg.ASGs {
timeStart := time.Now()
ts.lg.Info("creating ASG", zap.String("name", asgName))
// TODO: may not be necessary
// "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
// already includes SSM agent + AWS CLI
tg := templateASG{}
switch cur.AMIType {
case ec2config.AMITypeBottleRocketCPU:
// "bottlerocket" comes with SSM agent
case ec2config.AMITypeAL2X8664:
tg.Metadata = metadataAL2InstallSSM
tg.UserData = userDataAL2InstallSSM
case ec2config.AMITypeAL2X8664GPU:
tg.Metadata = metadataAL2InstallSSM
tg.UserData = userDataAL2InstallSSM
}
tpl := template.Must(template.New("TemplateASG").Parse(TemplateASG))
buf := bytes.NewBuffer(nil)
if err := tpl.Execute(buf, tg); err != nil {
return err
}
tmpl := buf.String()
stackInput := &cloudformation.CreateStackInput{
StackName: aws.String(asgName),
Capabilities: aws.StringSlice([]string{"CAPABILITY_NAMED_IAM"}),
OnFailure: aws.String(cloudformation.OnFailureDelete),
TemplateBody: aws.String(tmpl),
Tags: awscfn.NewTags(map[string]string{
"Kind": "aws-k8s-tester",
"Name": ts.cfg.Name,
"aws-k8s-tester-version": version.ReleaseVersion,
}),
Parameters: []*cloudformation.Parameter{
{
ParameterKey: aws.String("ASGName"),
ParameterValue: aws.String(cur.Name),
},
{
ParameterKey: aws.String("ASGLaunchTemplateName"),
ParameterValue: aws.String(cur.Name + "-launch-template"),
},
{
ParameterKey: aws.String("RoleName"),
ParameterValue: aws.String(ts.cfg.RoleName),
},
{
ParameterKey: aws.String("SecurityGroupID"),
ParameterValue: aws.String(ts.cfg.SecurityGroupID),
},
{
ParameterKey: aws.String("RemoteAccessKeyName"),
ParameterValue: aws.String(ts.cfg.RemoteAccessKeyName),
},
{
ParameterKey: aws.String("PublicSubnetIDs"),
ParameterValue: aws.String(strings.Join(ts.cfg.PublicSubnetIDs, ",")),
},
},
}
ts.lg.Info("added image ID", zap.String("image-id", cur.ImageID))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("ImageID"),
ParameterValue: aws.String(cur.ImageID),
})
if cur.ImageIDSSMParameter != "" {
ts.lg.Info("added image SSM parameter", zap.String("image-id-ssm-parameter", cur.ImageIDSSMParameter))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("ImageIDSSMParameter"),
ParameterValue: aws.String(cur.ImageIDSSMParameter),
})
}
if len(cur.InstanceTypes) > 0 {
ts.lg.Info("added instance type", zap.Strings("instance-types", cur.InstanceTypes))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("InstanceTypes"),
ParameterValue: aws.String(strings.Join(cur.InstanceTypes, ",")),
})
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("InstanceTypesCount"),
ParameterValue: aws.String(fmt.Sprintf("%d", len(cur.InstanceTypes))),
})
}
if cur.VolumeSize > 0 {
ts.lg.Info("added volume size", zap.Int64("volume-size", cur.VolumeSize))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("VolumeSize"),
ParameterValue: aws.String(fmt.Sprintf("%d", cur.VolumeSize)),
})
}
if cur.ASGMinSize > 0 {
ts.lg.Info("added min size", zap.Int64("min-size", cur.ASGMinSize))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("ASGMinSize"),
ParameterValue: aws.String(fmt.Sprintf("%d", cur.ASGMinSize)),
})
}
if cur.ASGMaxSize > 0 {
ts.lg.Info("added max size", zap.Int64("max-size", cur.ASGMaxSize))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("ASGMaxSize"),
ParameterValue: aws.String(fmt.Sprintf("%d", cur.ASGMaxSize)),
})
}
if cur.ASGDesiredCapacity > 0 {
ts.lg.Info("added desired size", zap.Int64("desired-capacity", cur.ASGDesiredCapacity))
stackInput.Parameters = append(stackInput.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String("ASGDesiredCapacity"),
ParameterValue: aws.String(fmt.Sprintf("%d", cur.ASGDesiredCapacity)),
})
}
stackOutput, err := ts.cfnAPI.CreateStack(stackInput)
if err != nil {
cur.CreateTook += time.Since(timeStart)
cur.CreateTookString = cur.CreateTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
return err
}
cur.ASGCFNStackID = aws.StringValue(stackOutput.StackId)
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
}
ts.lg.Info("waiting for ASGs create using CFN", zap.String("name", ts.cfg.Name))
for asgName, cur := range ts.cfg.ASGs {
timeStart := time.Now()
ts.lg.Info("waiting for ASG", zap.String("name", asgName))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
ch := awscfn.Poll(
ctx,
ts.stopCreationCh,
ts.lg,
ts.cfnAPI,
cur.ASGCFNStackID,
cloudformation.ResourceStatusCreateComplete,
2*time.Minute,
30*time.Second,
)
var st awscfn.StackStatus
for st = range ch {
if st.Error != nil {
ts.cfg.RecordStatus(fmt.Sprintf("failed to create ASG (%v)", st.Error))
ts.lg.Warn("polling errror", zap.Error(st.Error))
}
}
cancel()
if st.Error != nil {
cur.CreateTook += time.Since(timeStart)
cur.CreateTookString = cur.CreateTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
return st.Error
}
// update status after creating a new ASG
for _, o := range st.Stack.Outputs {
switch k := aws.StringValue(o.OutputKey); k {
case "ASGName":
ts.lg.Info("found ASGName value from CFN", zap.String("value", aws.StringValue(o.OutputValue)))
case "InstanceProfileARN":
ts.lg.Info("found InstanceProfileARN value from CFN", zap.String("value", aws.StringValue(o.OutputValue)))
default:
cur.CreateTook += time.Since(timeStart)
cur.CreateTookString = cur.CreateTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
return fmt.Errorf("unexpected OutputKey %q from %q", k, cur.ASGCFNStackID)
}
}
ts.lg.Info("created ASG",
zap.String("name", cur.Name),
zap.String("cfn-stack-id", cur.ASGCFNStackID),
zap.String("started", humanize.RelTime(createStart, time.Now(), "ago", "from now")),
)
cur.CreateTook += time.Since(timeStart)
cur.CreateTookString = cur.CreateTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Up = true
ts.cfg.Sync()
var aout *autoscaling.DescribeAutoScalingGroupsOutput
aout, err = ts.asgAPI.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: aws.StringSlice([]string{asgName}),
})
if err != nil {
return fmt.Errorf("ASG %q not found (%v)", asgName, err)
}
if len(aout.AutoScalingGroups) != 1 {
return fmt.Errorf("%q expected only 1 ASG, got %+v", asgName, aout.AutoScalingGroups)
}
av := aout.AutoScalingGroups[0]
instanceIDs := make([]string, 0, len(av.Instances))
for _, iv := range av.Instances {
instanceIDs = append(instanceIDs, aws.StringValue(iv.InstanceId))
}
ts.lg.Info(
"describing EC2 instances in ASG",
zap.String("asg-name", asgName),
zap.Strings("instance-ids", instanceIDs),
)
ec2Instances, err := awsapiec2.PollUntilRunning(
10*time.Minute,
ts.lg,
ts.ec2API,
instanceIDs...,
)
if err != nil {
cur.CreateTook += time.Since(timeStart)
cur.CreateTookString = cur.CreateTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
return err
}
cur.Instances = make(map[string]ec2config.Instance)
for id, vv := range ec2Instances {
ivv := ec2config.ConvertInstance(vv)
ivv.RemoteAccessUserName = cur.RemoteAccessUserName
cur.Instances[id] = ivv
}
cur.CreateTook += time.Since(timeStart)
cur.CreateTookString = cur.CreateTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.RecordStatus(fmt.Sprintf("%q/%s", asgName, cloudformation.ResourceStatusCreateComplete))
ts.cfg.Sync()
}
return ts.cfg.Sync()
}
func (ts *Tester) deleteASGs() (err error) {
deleteStart := time.Now()
defer func() {
ts.cfg.DeleteTook += time.Since(deleteStart)
ts.cfg.DeleteTookString = ts.cfg.DeleteTook.String()
ts.cfg.Sync()
}()
var errs []string
ts.lg.Info("deleting ASGs using CFN", zap.String("name", ts.cfg.Name))
for asgName, cur := range ts.cfg.ASGs {
if cur.ASGCFNStackID == "" {
return fmt.Errorf("%q ASG stack ID is empty", cur.Name)
}
timeStart := time.Now()
ts.lg.Info("deleting ASG", zap.String("name", asgName), zap.String("cfn-stack-id", cur.ASGCFNStackID))
_, err = ts.cfnAPI.DeleteStack(&cloudformation.DeleteStackInput{
StackName: aws.String(cur.ASGCFNStackID),
})
if err != nil {
ts.cfg.RecordStatus(fmt.Sprintf("failed to delete ASG (%v)", err))
cur.DeleteTook += time.Since(timeStart)
cur.DeleteTookString = ts.cfg.DeleteTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
errs = append(errs, fmt.Sprintf("failed to delete ASG (%v)", err))
continue
}
ts.cfg.Up = false
ts.cfg.Sync()
}
ts.lg.Info("waiting for ASGs delete using CFN", zap.String("name", ts.cfg.Name))
for asgName, cur := range ts.cfg.ASGs {
timeStart := time.Now()
ts.lg.Info("waiting for ASG", zap.String("name", asgName))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
ch := awscfn.Poll(
ctx,
make(chan struct{}), // do not exit on stop
ts.lg,
ts.cfnAPI,
cur.ASGCFNStackID,
cloudformation.ResourceStatusDeleteComplete,
2*time.Minute,
20*time.Second,
)
var st awscfn.StackStatus
for st = range ch {
if st.Error != nil {
cancel()
ts.cfg.RecordStatus(fmt.Sprintf("failed to delete ASG (%v)", st.Error))
ts.lg.Warn("polling errror", zap.Error(st.Error))
}
}
cancel()
if st.Error != nil {
cur.DeleteTook += time.Since(timeStart)
cur.DeleteTookString = ts.cfg.DeleteTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
errs = append(errs, fmt.Sprintf("failed to delete ASG (%v)", st.Error))
continue
}
ts.cfg.RecordStatus(fmt.Sprintf("%q/%s", asgName, ec2config.StatusDELETEDORNOTEXIST))
cur.DeleteTook += time.Since(timeStart)
cur.DeleteTookString = ts.cfg.DeleteTook.String()
ts.cfg.ASGs[asgName] = cur
ts.cfg.Sync()
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "; "))
}
return ts.cfg.Sync()
}