/
cluster.go
323 lines (287 loc) · 8.67 KB
/
cluster.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
package aws
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"sync"
"text/template"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/guseggert/clustertest/agent"
clusteriface "github.com/guseggert/clustertest/cluster"
"go.uber.org/zap"
)
const userDataTemplate = `#!/bin/bash
mkdir /node
cd /node
curl --retry 3 '{{.NodeAgentURL}}' > nodeagent
chmod +x nodeagent
nohup ./nodeagent \
--heartbeat-timeout 1m \
--on-heartbeat-failure shutdown \
--ca-cert-pem {{.CACertPEMEncoded}} \
--cert-pem {{.CertPEMEncoded}} \
--key-pem {{.KeyPEMEncoded}} \
&>/var/log/nodeagent &
`
type Cluster struct {
Nodes []*Node
InstanceType string
CleanupWait bool
RunInstancesConfig func(*ec2.RunInstancesInput) error
ctx context.Context
config *config
}
func collectPages[IN any, OUT any](input IN, fn func(IN, func(OUT, bool) bool) error) ([]OUT, error) {
var out []OUT
err := fn(input, func(output OUT, more bool) bool {
out = append(out, output)
return true
})
return out, err
}
func collectPagesWithContext[IN any, OUT any](ctx context.Context, input IN, fn func(context.Context, IN, func(OUT, bool) bool, ...request.Option) error, opts ...request.Option) ([]OUT, error) {
var out []OUT
err := fn(ctx, input, func(output OUT, more bool) bool {
out = append(out, output)
return true
}, opts...)
return out, err
}
type Option func(c *Cluster)
// WithRunInstancesInput registers a callback for customizing RunInstances calls when new nodes are created.
func (c *Cluster) WithRunInstancesInput(f func(input *ec2.RunInstancesInput) error) *Cluster {
c.RunInstancesConfig = f
return c
}
func (c *Cluster) WithLogger(l *zap.SugaredLogger) *Cluster {
c.config.log = l.Named("ec2_cluster")
return c
}
func (c *Cluster) WithInstanceType(s string) *Cluster {
c.InstanceType = s
return c
}
func (c *Cluster) WithSession(sess *session.Session) *Cluster {
c.config.session = sess
return c
}
func (c *Cluster) WithNodeAgentBin(binPath string) *Cluster {
c.config.nodeAgentBin = binPath
return c
}
// WithCleanupWait causes the Cleanup methods to wait for instance termination to succeed before returning.
func (c *Cluster) WithCleanupWait() *Cluster {
c.CleanupWait = true
return c
}
func (c *Cluster) WithAMIID(amiID string) *Cluster {
c.config.amiID = amiID
return c
}
func (c *Cluster) Context(ctx context.Context) *Cluster {
newC := *c
newC.ctx = ctx
return &newC
}
// NewCluster creates a new AWS cluster.
// This uses standard AWS profile env vars.
// With no configuration, this uses the default profile.
// The user/role used must have the appropriate permissions for the test runner,
// in order to find the resources in the account and launch/destroy EC2 instances.
//
// By default, this looks for the node agent binary by searching up from PWD for a "nodeagent" file.
func NewCluster() *Cluster {
return &Cluster{
InstanceType: "t3.micro",
ctx: context.Background(),
config: &config{},
}
}
func (c *Cluster) waitForInstances(ctx context.Context, instances []*ec2.Instance) ([]*ec2.Instance, error) {
var instanceIDs []*string
for _, inst := range instances {
instanceIDs = append(instanceIDs, inst.InstanceId)
}
var newInstances []*ec2.Instance
for i := 0; ; i++ {
if i != 0 {
time.Sleep(1 * time.Second)
}
out, err := c.config.ec2Client.DescribeInstancesWithContext(ctx, &ec2.DescribeInstancesInput{
InstanceIds: instanceIDs,
})
if err != nil {
// this can happen due to EC2 eventual consistency, ignore it and keep polling
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "InvalidInstanceID.NotFound" {
continue
}
}
return nil, fmt.Errorf("waiting for EC2 instance: %w", err)
}
newInstances = nil
for _, res := range out.Reservations {
for _, inst := range res.Instances {
stateName := *inst.State.Name
switch stateName {
case ec2.InstanceStateNamePending:
continue
case ec2.InstanceStateNameRunning:
newInstances = append(newInstances, inst)
default:
return nil, fmt.Errorf("unexpected instance state %q", stateName)
}
}
}
if len(newInstances) == len(instances) {
return newInstances, nil
}
}
}
func (c *Cluster) NewNodes(ctx context.Context, n int) (clusteriface.Nodes, error) {
if err := c.ensureLoaded(); err != nil {
return nil, err
}
req, _ := c.config.s3Client.GetObjectRequest(&s3.GetObjectInput{
Bucket: &c.config.nodeAgentS3Bucket,
Key: &c.config.nodeAgentS3Key,
})
nodeagentURL, err := req.Presign(5 * time.Minute)
if err != nil {
return nil, fmt.Errorf("presigning node agent URL: %w", err)
}
tmpl, err := template.New("").Parse(userDataTemplate)
if err != nil {
return nil, fmt.Errorf("parsing user data template: %w", err)
}
caCertPEMEncoded := base64.StdEncoding.EncodeToString(c.config.cert.CA.CertPEMBytes)
certPEMEncoded := base64.StdEncoding.EncodeToString(c.config.cert.Server.CertPEMBytes)
keyPEMEncoded := base64.StdEncoding.EncodeToString(c.config.cert.Server.KeyPEMBytes)
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, map[string]string{
"NodeAgentURL": nodeagentURL,
"CACertPEMEncoded": caCertPEMEncoded,
"CertPEMEncoded": certPEMEncoded,
"KeyPEMEncoded": keyPEMEncoded,
})
if err != nil {
return nil, fmt.Errorf("executing user data template: %w", err)
}
userData := base64.StdEncoding.EncodeToString(buf.Bytes())
n64 := int64(n)
input := &ec2.RunInstancesInput{
ImageId: &c.config.amiID,
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Arn: &c.config.instanceProfileARN},
InstanceType: &c.InstanceType,
MaxCount: &n64,
MinCount: &n64,
InstanceInitiatedShutdownBehavior: aws.String(ec2.ShutdownBehaviorTerminate),
UserData: &userData,
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{{
AssociatePublicIpAddress: aws.Bool(true),
DeleteOnTermination: aws.Bool(true),
Groups: []*string{&c.config.instanceSecurityGroupID},
SubnetId: &c.config.subnetID,
DeviceIndex: aws.Int64(0),
}},
}
if c.RunInstancesConfig != nil {
c.RunInstancesConfig(input)
}
reservations, err := c.config.ec2Client.RunInstancesWithContext(ctx, input)
if err != nil {
return nil, fmt.Errorf("launching instance: %w", err)
}
if len(reservations.Instances) != n {
return nil, fmt.Errorf("expected %d instances instance but got %d", n, len(reservations.Instances))
}
instances, err := c.waitForInstances(ctx, reservations.Instances)
if err != nil {
return nil, fmt.Errorf("waiting for instancs: %w", err)
}
var ifaceNodes clusteriface.Nodes
var nodes []*Node
for _, inst := range instances {
nodeAgentClient, err := agent.NewClient(c.config.log, c.config.cert, *inst.PublicIpAddress, 8080)
if err != nil {
return nil, fmt.Errorf("constructing node agent client: %w", err)
}
node := &Node{
agentClient: nodeAgentClient,
sess: c.config.session,
ec2Client: c.config.ec2Client,
instanceID: *inst.InstanceId,
accountID: c.config.accountID,
cleanupWait: c.CleanupWait,
}
nodes = append(nodes, node)
ifaceNodes = append(ifaceNodes, node)
c.Nodes = append(c.Nodes, node)
node.agentClient.StartHeartbeat()
}
err = c.waitForNodesHeartbeats(ctx, nodes)
if err != nil {
return nil, err
}
return ifaceNodes, nil
}
func (c *Cluster) waitForNodeHeartbeat(ctx context.Context, node *Node) {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := node.Heartbeat(ctx)
if err == nil {
return
}
}
}
}
func (c *Cluster) waitForNodesHeartbeats(ctx context.Context, nodes []*Node) error {
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
wg := sync.WaitGroup{}
wg.Add(len(nodes))
// wait on a heartbeat
for _, node := range nodes {
node := node
go func() {
defer wg.Done()
c.waitForNodeHeartbeat(ctx, node)
}()
}
wg.Wait()
return ctx.Err()
}
func (c *Cluster) NewNode(ctx context.Context) (clusteriface.Node, error) {
if err := c.ensureLoaded(); err != nil {
return nil, err
}
nodes, err := c.NewNodes(ctx, 1)
if err != nil {
return nil, err
}
return nodes[0], nil
}
func (c *Cluster) Cleanup(ctx context.Context) error {
if err := c.ensureLoaded(); err != nil {
return err
}
for _, n := range c.Nodes {
err := n.Stop(ctx)
if err != nil {
return fmt.Errorf("stopping node %s: %w", n, err)
}
}
return nil
}