-
Notifications
You must be signed in to change notification settings - Fork 79
/
ec2.go
162 lines (147 loc) · 4.7 KB
/
ec2.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
// Package ec2 implements various AWS EC2 operations.
package ec2
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-k8s-tester/pkg/ctxutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"go.uber.org/zap"
)
// WaitUntilRunning describes all EC2 instances for the specified ASG.
// It waits until all instances are 'running'.
// TODO: make method (e.g. asgClient."WaitUntilRunning")
func WaitUntilRunning(
ctx context.Context,
stopc chan struct{},
asgAPI autoscalingiface.AutoScalingAPI,
ec2API ec2iface.EC2API,
asgName string) (ec2Instances map[string]*ec2.Instance, err error) {
zap.L().Info("polling ASG until all EC2 instances are running",
zap.String("asg-name", asgName),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
for ctx.Err() == nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-stopc:
return nil, errors.New("poll aborted")
case <-time.After(10 * time.Second):
}
// When ASG has >500 nodes, some instances may shut down at any moments,
// making previous instance ID list stale
// thus, fetch latest instance IDs for every iteration
var aout *autoscaling.DescribeAutoScalingGroupsOutput
aout, err = asgAPI.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: aws.StringSlice([]string{asgName}),
})
if err != nil {
return nil, fmt.Errorf("ASG[%q] not found (%v)", asgName, err)
}
if len(aout.AutoScalingGroups) != 1 {
return nil, fmt.Errorf("expected 1 ASG[%q], got %+v", asgName, aout.AutoScalingGroups)
}
av := aout.AutoScalingGroups[0]
instanceIDs := make([]string, 0, len(av.Instances))
for _, iv := range av.Instances {
lv := aws.StringValue(iv.LifecycleState)
switch lv {
case autoscaling.LifecycleStatePending,
autoscaling.LifecycleStatePendingWait,
autoscaling.LifecycleStatePendingProceed,
autoscaling.LifecycleStateInService:
instanceIDs = append(instanceIDs, aws.StringValue(iv.InstanceId))
default:
zap.L().Warn("skipping instance due to lifecycle state",
zap.String("instance-id", aws.StringValue(iv.InstanceId)),
zap.String("lifecycle-state", lv),
)
}
}
// minimum waits for small number of node + extra waits for large number of nodes
// e.g. 5-minute + 25-minute for 500 nodes
waitDur := 5*time.Minute + 3*time.Second*time.Duration(len(instanceIDs))
ctx2, cancel := context.WithTimeout(ctx, waitDur)
ec2Instances, err = pollUntilRunning(ctx2, stopc, ec2API, instanceIDs...)
cancel()
if err == nil {
break
}
zap.L().Warn("failed to poll instance status; retrying", zap.Error(err))
}
return ec2Instances, err
}
// pollUntilRunning describes EC2 instances by batch,
// and waits until all instances are 'running'.
func pollUntilRunning(
ctx context.Context,
stopc chan struct{},
ec2API ec2iface.EC2API,
instanceIDs ...string) (ec2Instances map[string]*ec2.Instance, err error) {
targetN := len(instanceIDs)
ec2Instances = make(map[string]*ec2.Instance, targetN)
left := make(map[string]struct{}, targetN)
for _, id := range instanceIDs {
left[id] = struct{}{}
}
zap.L().Info("polling instance status",
zap.Int("target-total", targetN),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
for ctx.Err() == nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-stopc:
return nil, errors.New("poll aborted")
case <-time.After(10 * time.Second):
}
// batch by 30
batch := make([]string, 0, 30)
for id := range left {
batch = append(batch, id)
if len(batch) >= 30 {
break
}
}
zap.L().Info("describing batch", zap.Int("batch-total", len(batch)))
var dout *ec2.DescribeInstancesOutput
dout, err = ec2API.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice(batch),
})
if err != nil {
zap.L().Warn("failed to describe instances", zap.Error(err))
time.Sleep(5 * time.Second)
continue
}
for _, rsrv := range dout.Reservations {
for _, iv := range rsrv.Instances {
state := aws.StringValue(iv.State.Name)
if state != ec2.InstanceStateNameRunning {
continue
}
instanceID := aws.StringValue(iv.InstanceId)
ec2Instances[instanceID] = iv
}
}
zap.L().Info("checking ec2 instances",
zap.Int("reservations", len(dout.Reservations)),
zap.Int("running-instances-so-far", len(ec2Instances)),
zap.Int("target-total", targetN),
)
// remove completed instances from next batch
for id := range ec2Instances {
delete(left, id)
}
if len(left) == 0 {
break
}
}
return ec2Instances, err
}