This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
/
aws.go
424 lines (360 loc) · 10.6 KB
/
aws.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
package aws
import (
"context"
"encoding/base64"
"errors"
"log"
"strconv"
"strings"
"time"
awspkg "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/talos-systems/cluster-api-provider-talos/pkg/cloud/talos/utils"
"gopkg.in/yaml.v2"
"k8s.io/client-go/kubernetes"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)
// AWS represents a provider for AWS.
type AWS struct {
}
// ClusterInfo holds data about desired config in cluster object
type ClusterInfo struct {
Region string
}
// MachineInfo holds data about desired config in cluster object
type MachineInfo struct {
Region string
Instances InstanceInfo
}
// InstanceInfo holds data about the instances we'll create
type InstanceInfo struct {
Type string
AMI string
Keypair string
SecurityGroups []string
Disks DiskInfo
}
// DiskInfo holds disk info data
type DiskInfo struct {
Size int
}
//NewAWS returns an instance of the AWS provisioner
func NewAWS() (*AWS, error) {
return &AWS{}, nil
}
// Create creates an instance in AWS.
func (aws *AWS) Create(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, clientset *kubernetes.Clientset) error {
machineSpec, err := utils.MachineProviderFromSpec(machine.Spec.ProviderSpec)
if err != nil {
return err
}
awsConfig := &MachineInfo{}
yaml.Unmarshal([]byte(machineSpec.Platform.Config), awsConfig)
ec2client, err := client(awsConfig.Region)
if err != nil {
return err
}
//fetch floating IP if master and userdata based on machine name
natIP := ""
if strings.Contains(machine.ObjectMeta.Name, "master") {
// Find public ip
address, err := getPublicIPByName(ec2client, machine.ObjectMeta.Name+"-ip")
if err != nil {
return err
}
if address == nil {
return errors.New("IP not ready")
}
natIP = *address.PublicIp
}
udConfigMap, err := utils.FetchConfigMap(cluster, machine, clientset)
if err != nil {
return err
}
ud := udConfigMap.Data["userdata"]
udb64 := base64.StdEncoding.EncodeToString([]byte(ud))
// Create our ec2 instance and wait for it to be running
instanceInput := &ec2.RunInstancesInput{
ImageId: awspkg.String(awsConfig.Instances.AMI),
InstanceType: awspkg.String(awsConfig.Instances.Type),
MinCount: awspkg.Int64(1),
MaxCount: awspkg.Int64(1),
KeyName: awspkg.String(awsConfig.Instances.Keypair),
UserData: awspkg.String(udb64),
}
res, err := ec2client.RunInstances(instanceInput)
if err != nil {
return err
}
instanceID := *res.Instances[0].InstanceId
//Wait for instance to be running, then fetch and associate pre-existing Elastic IP if needed
if natIP != "" {
//TODO: Should probably attempt to call delete if running status fails
err = waitForStatus(instanceID, "running", ec2client)
if err != nil {
return err
}
descAddrInput := &ec2.DescribeAddressesInput{PublicIps: []*string{awspkg.String(natIP)}}
addresses, err := ec2client.DescribeAddresses(descAddrInput)
if err != nil {
return err
}
assocAddrInput := &ec2.AssociateAddressInput{
AllocationId: addresses.Addresses[0].AllocationId,
NetworkInterfaceId: res.Instances[0].NetworkInterfaces[0].NetworkInterfaceId,
}
_, err = ec2client.AssociateAddress(assocAddrInput)
if err != nil {
return err
}
}
// Add tags to the created instance
_, err = ec2client.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{res.Instances[0].InstanceId},
Tags: []*ec2.Tag{
{
Key: awspkg.String("Name"),
Value: awspkg.String(machine.ObjectMeta.Name),
},
{
Key: awspkg.String("TalosClusterName"),
Value: awspkg.String(cluster.ObjectMeta.Name),
},
},
})
if err != nil {
return err
}
log.Println("[AWS] Instance created with ID:" + instanceID)
return nil
}
//Update updates a given AWS instance.
func (aws *AWS) Update(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, clientset *kubernetes.Clientset) error {
return nil
}
// Delete deletes a AWS instance.
func (aws *AWS) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, clientset *kubernetes.Clientset) error {
// Fish out configs and create an ec2 client
machineSpec, err := utils.MachineProviderFromSpec(machine.Spec.ProviderSpec)
if err != nil {
return err
}
awsConfig := &MachineInfo{}
yaml.Unmarshal([]byte(machineSpec.Platform.Config), awsConfig)
ec2client, err := client(awsConfig.Region)
if err != nil {
return err
}
//Make sure instance exists before attempting delete
exists, err := aws.Exists(ctx, cluster, machine, clientset)
if err != nil {
return err
}
if !exists {
return nil
}
//Find instance id
id, err := fetchInstanceID(cluster.ObjectMeta.Name, machine.ObjectMeta.Name, ec2client)
if err != nil {
return err
}
//Stop Instance
input := &ec2.TerminateInstancesInput{
InstanceIds: []*string{id},
}
_, err = ec2client.TerminateInstances(input)
if err != nil {
return err
}
return nil
}
// Exists returns whether or not an instance is present in AWS.
func (aws *AWS) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine, clientset *kubernetes.Clientset) (bool, error) {
// Fish out configs and create an ec2 client
machineSpec, err := utils.MachineProviderFromSpec(machine.Spec.ProviderSpec)
if err != nil {
return false, err
}
awsConfig := &MachineInfo{}
yaml.Unmarshal([]byte(machineSpec.Platform.Config), awsConfig)
ec2client, err := client(awsConfig.Region)
if err != nil {
return false, err
}
id, err := fetchInstanceID(cluster.ObjectMeta.Name, machine.ObjectMeta.Name, ec2client)
if err != nil {
return false, err
}
if id == nil {
return false, nil
}
return true, nil
}
// AllocateExternalIPs creates IPs for the control plane nodes
func (aws *AWS) AllocateExternalIPs(cluster *clusterv1.Cluster, clientset *kubernetes.Clientset) ([]string, error) {
// Fish out configs and create an ec2 client
clusterSpec, err := utils.ClusterProviderFromSpec(cluster.Spec.ProviderSpec)
if err != nil {
return nil, err
}
awsConfig := &ClusterInfo{}
yaml.Unmarshal([]byte(clusterSpec.Platform.Config), awsConfig)
ec2client, err := client(awsConfig.Region)
if err != nil {
return nil, err
}
floatingIPs := []string{}
for i := 0; i < clusterSpec.ControlPlane.Count; i++ {
// Check if ips already exist and add to list early if so
flip, err := getPublicIPByName(ec2client, cluster.ObjectMeta.Name+"-master-"+strconv.Itoa(i)+"-ip")
if err != nil {
return nil, err
}
if flip != nil {
floatingIPs = append(floatingIPs, *flip.PublicIp)
continue
}
// Allocate elastic ip
allocRes, err := ec2client.AllocateAddress(&ec2.AllocateAddressInput{
Domain: awspkg.String("vpc"),
})
if err != nil {
return nil, err
}
// Add tags to the created instance
_, err = ec2client.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{allocRes.AllocationId},
Tags: []*ec2.Tag{
{
Key: awspkg.String("Name"),
Value: awspkg.String(cluster.ObjectMeta.Name + "-master-" + strconv.Itoa(i) + "-ip"),
},
},
})
if err != nil {
return nil, err
}
floatingIPs = append(floatingIPs, *allocRes.PublicIp)
}
return floatingIPs, nil
}
// DeAllocateExternalIPs cleans IPs for the control plane nodes
func (aws *AWS) DeAllocateExternalIPs(cluster *clusterv1.Cluster, clientset *kubernetes.Clientset) error {
// Fish out configs and create an ec2 client
clusterSpec, err := utils.ClusterProviderFromSpec(cluster.Spec.ProviderSpec)
if err != nil {
return err
}
awsConfig := &ClusterInfo{}
yaml.Unmarshal([]byte(clusterSpec.Platform.Config), awsConfig)
ec2client, err := client(awsConfig.Region)
if err != nil {
return err
}
for i := 0; i < clusterSpec.ControlPlane.Count; i++ {
// Check if ips already exist and add to list early if so
flip, err := getPublicIPByName(ec2client, cluster.ObjectMeta.Name+"-master-"+strconv.Itoa(i)+"-ip")
if err != nil {
return err
}
// Ignore if not found
if flip == nil {
return nil
}
_, err = ec2client.ReleaseAddress(&ec2.ReleaseAddressInput{
AllocationId: flip.AllocationId,
})
if err != nil {
return err
}
}
return nil
}
// getPublicIPbyName finds the public IP object from a list of all IP objects
func getPublicIPByName(ec2client *ec2.EC2, name string) (*ec2.Address, error) {
result, err := ec2client.DescribeAddresses(&ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
{
Name: awspkg.String("tag:Name"),
Values: awspkg.StringSlice([]string{name}),
},
},
})
if err != nil {
return nil, err
}
if len(result.Addresses) > 0 {
return result.Addresses[0], nil
}
// Not found
return nil, nil
}
// client generates an ec2 client to use
func client(region string) (*ec2.EC2, error) {
sess, err := session.NewSession(&awspkg.Config{
Region: awspkg.String(region)},
)
if err != nil {
return nil, err
}
ec2client := ec2.New(sess)
return ec2client, nil
}
//fetchInstance ID searches AWS for instance name and the cluster name tags that we add during instance creation. Returns the instance ID.
func fetchInstanceID(clusterName string, instanceName string, client *ec2.EC2) (*string, error) {
instanceFilters := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: awspkg.String("tag:Name"),
Values: []*string{
awspkg.String(instanceName),
},
},
&ec2.Filter{
Name: awspkg.String("tag:TalosClusterName"),
Values: []*string{
awspkg.String(clusterName),
},
},
&ec2.Filter{
Name: awspkg.String("instance-state-name"),
Values: []*string{awspkg.String("running"), awspkg.String("pending"), awspkg.String("stopped")},
},
},
}
res, err := client.DescribeInstances(instanceFilters)
if err != nil {
return nil, err
}
if len(res.Reservations) == 0 {
return nil, nil
}
if len(res.Reservations[0].Instances) > 1 {
return nil, errors.New("[AWS] Multiple instances with same filter info")
}
return res.Reservations[0].Instances[0].InstanceId, nil
}
//waitForStatus polls the AWS api for a certain instance status
//needed for attaching elastic IP after boot
func waitForStatus(instanceID string, desiredState string, client *ec2.EC2) error {
timeout := time.After(120 * time.Second)
tick := time.Tick(3 * time.Second)
for {
select {
case <-timeout:
return errors.New("[AWS] Timed out waiting for running instance: " + instanceID)
case <-tick:
instanceInput := &ec2.DescribeInstancesInput{
InstanceIds: []*string{awspkg.String(instanceID)},
}
res, err := client.DescribeInstances(instanceInput)
if err != nil {
return err
}
if *res.Reservations[0].Instances[0].State.Name == desiredState {
return nil
}
}
}
}