-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathinstances.go
1203 lines (1006 loc) · 42.2 KB
/
instances.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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ec2
import (
"context"
"encoding/base64"
"fmt"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/pkg/errors"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/converters"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/filter"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/userdata"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capierrors "sigs.k8s.io/cluster-api/errors"
)
// GetRunningInstanceByTags returns the existing instance or nothing if it doesn't exist.
func (s *Service) GetRunningInstanceByTags(scope *scope.MachineScope) (*infrav1.Instance, error) {
s.scope.Debug("Looking for existing machine instance by tags")
input := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
filter.EC2.ClusterOwned(s.scope.Name()),
filter.EC2.Name(scope.Name()),
filter.EC2.InstanceStates(ec2.InstanceStateNamePending, ec2.InstanceStateNameRunning),
},
}
out, err := s.EC2Client.DescribeInstancesWithContext(context.TODO(), input)
switch {
case awserrors.IsNotFound(err):
return nil, nil
case err != nil:
record.Eventf(s.scope.InfraCluster(), "FailedDescribeInstances", "Failed to describe instances by tags: %v", err)
return nil, errors.Wrap(err, "failed to describe instances by tags")
}
// TODO: currently just returns the first matched instance, need to
// better rationalize how to find the right instance to return if multiple
// match
for _, res := range out.Reservations {
for _, inst := range res.Instances {
return s.SDKToInstance(inst)
}
}
return nil, nil
}
// InstanceIfExists returns the existing instance by id and errors if it cannot find the instance(ErrInstanceNotFoundByID) or API call fails (ErrDescribeInstance).
// Returns empty instance with nil error, only when providerID is nil.
func (s *Service) InstanceIfExists(id *string) (*infrav1.Instance, error) {
if id == nil {
s.scope.Info("Instance does not have an instance id")
return nil, nil
}
s.scope.Debug("Looking for instance by id", "instance-id", *id)
input := &ec2.DescribeInstancesInput{
InstanceIds: []*string{id},
}
out, err := s.EC2Client.DescribeInstancesWithContext(context.TODO(), input)
switch {
case awserrors.IsNotFound(err):
record.Eventf(s.scope.InfraCluster(), "FailedFindInstances", "failed to find instance by providerId %q: %v", *id, err)
return nil, ErrInstanceNotFoundByID
case err != nil:
record.Eventf(s.scope.InfraCluster(), "FailedDescribeInstances", "failed to describe instance %q: %v", *id, err)
return nil, ErrDescribeInstance
}
if len(out.Reservations) > 0 && len(out.Reservations[0].Instances) > 0 {
return s.SDKToInstance(out.Reservations[0].Instances[0])
}
// Failed to find instance with provider id.
record.Eventf(s.scope.InfraCluster(), "FailedFindInstances", "failed to find instance by providerId %q: %v", *id, err)
return nil, ErrInstanceNotFoundByID
}
// CreateInstance runs an ec2 instance.
//
//nolint:gocyclo // this function has multiple processes to perform
func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, userDataFormat string) (*infrav1.Instance, error) {
s.scope.Debug("Creating an instance for a machine")
input := &infrav1.Instance{
Type: scope.AWSMachine.Spec.InstanceType,
IAMProfile: scope.AWSMachine.Spec.IAMInstanceProfile,
RootVolume: scope.AWSMachine.Spec.RootVolume.DeepCopy(),
NonRootVolumes: scope.AWSMachine.Spec.NonRootVolumes,
NetworkInterfaces: scope.AWSMachine.Spec.NetworkInterfaces,
}
// Make sure to use the MachineScope here to get the merger of AWSCluster and AWSMachine tags
additionalTags := scope.AdditionalTags()
input.Tags = infrav1.Build(infrav1.BuildParams{
ClusterName: s.scope.KubernetesClusterName(),
Lifecycle: infrav1.ResourceLifecycleOwned,
Name: aws.String(scope.Name()),
Role: aws.String(scope.Role()),
Additional: additionalTags,
}.WithCloudProvider(s.scope.KubernetesClusterName()).WithMachineName(scope.Machine))
var err error
imageArchitecture, err := s.pickArchitectureForInstanceType(input.Type)
if err != nil {
return nil, err
}
// Pick image from the machine configuration, or use a default one.
if scope.AWSMachine.Spec.AMI.ID != nil { //nolint:nestif
input.ImageID = *scope.AWSMachine.Spec.AMI.ID
} else {
if scope.Machine.Spec.Version == nil {
err := errors.New("Either AWSMachine's spec.ami.id or Machine's spec.version must be defined")
scope.SetFailureReason(capierrors.CreateMachineError)
scope.SetFailureMessage(err)
return nil, err
}
imageLookupFormat := scope.AWSMachine.Spec.ImageLookupFormat
if imageLookupFormat == "" {
imageLookupFormat = scope.InfraCluster.ImageLookupFormat()
}
imageLookupOrg := scope.AWSMachine.Spec.ImageLookupOrg
if imageLookupOrg == "" {
imageLookupOrg = scope.InfraCluster.ImageLookupOrg()
}
imageLookupBaseOS := scope.AWSMachine.Spec.ImageLookupBaseOS
if imageLookupBaseOS == "" {
imageLookupBaseOS = scope.InfraCluster.ImageLookupBaseOS()
}
if scope.IsEKSManaged() && imageLookupFormat == "" && imageLookupOrg == "" && imageLookupBaseOS == "" {
input.ImageID, err = s.eksAMILookup(*scope.Machine.Spec.Version, imageArchitecture, scope.AWSMachine.Spec.AMI.EKSOptimizedLookupType)
if err != nil {
return nil, err
}
} else {
input.ImageID, err = s.defaultAMIIDLookup(imageLookupFormat, imageLookupOrg, imageLookupBaseOS, imageArchitecture, *scope.Machine.Spec.Version)
if err != nil {
return nil, err
}
}
}
subnetID, err := s.findSubnet(scope)
if err != nil {
return nil, err
}
input.SubnetID = subnetID
// Preserve user-defined PublicIp option.
input.PublicIPOnLaunch = scope.AWSMachine.Spec.PublicIP
// Public address from BYO Public IPv4 Pools need to be associated after launch (main machine
// reconciliate loop) preventing duplicated public IP. The map on launch is explicitly
// disabled in instances with PublicIP defined to true.
if scope.AWSMachine.Spec.ElasticIPPool != nil && scope.AWSMachine.Spec.ElasticIPPool.PublicIpv4Pool != nil {
input.PublicIPOnLaunch = ptr.To(false)
}
if !scope.IsControlPlaneExternallyManaged() && !scope.IsExternallyManaged() && !scope.IsEKSManaged() && s.scope.Network().APIServerELB.DNSName == "" {
record.Eventf(s.scope.InfraCluster(), "FailedCreateInstance", "Failed to run controlplane, APIServer ELB not available")
return nil, awserrors.NewFailedDependency("failed to run controlplane, APIServer ELB not available")
}
if scope.CompressUserData(userDataFormat) {
userData, err = userdata.GzipBytes(userData)
if err != nil {
return nil, errors.New("failed to gzip userdata")
}
}
input.UserData = ptr.To[string](base64.StdEncoding.EncodeToString(userData))
// Set security groups.
ids, err := s.GetCoreSecurityGroups(scope)
if err != nil {
return nil, err
}
input.SecurityGroupIDs = append(input.SecurityGroupIDs, ids...)
// If SSHKeyName WAS NOT provided in the AWSMachine Spec, fallback to the value provided in the AWSCluster Spec.
// If a value was not provided in the AWSCluster Spec, then use the defaultSSHKeyName
// Note that:
// - a nil AWSMachine.Spec.SSHKeyName value means use the AWSCluster.Spec.SSHKeyName SSH key name value
// - nil values for both AWSCluster.Spec.SSHKeyName and AWSMachine.Spec.SSHKeyName means use the default SSH key name value
// - an empty string means do not set an SSH key name at all
// - otherwise use the value specified in either AWSMachine or AWSCluster
var prioritizedSSHKeyName string
switch {
case scope.AWSMachine.Spec.SSHKeyName != nil:
// prefer AWSMachine.Spec.SSHKeyName if it is defined
prioritizedSSHKeyName = *scope.AWSMachine.Spec.SSHKeyName
case scope.InfraCluster.SSHKeyName() != nil:
// fallback to AWSCluster.Spec.SSHKeyName if it is defined
prioritizedSSHKeyName = *scope.InfraCluster.SSHKeyName()
default:
if !scope.IsExternallyManaged() {
prioritizedSSHKeyName = defaultSSHKeyName
}
}
// Only set input.SSHKeyName if the user did not explicitly request no ssh key be set (explicitly setting "" on either the Machine or related Cluster)
if prioritizedSSHKeyName != "" {
input.SSHKeyName = aws.String(prioritizedSSHKeyName)
}
input.SpotMarketOptions = scope.AWSMachine.Spec.SpotMarketOptions
input.InstanceMetadataOptions = scope.AWSMachine.Spec.InstanceMetadataOptions
input.Tenancy = scope.AWSMachine.Spec.Tenancy
input.PlacementGroupName = scope.AWSMachine.Spec.PlacementGroupName
input.PlacementGroupPartition = scope.AWSMachine.Spec.PlacementGroupPartition
input.PrivateDNSName = scope.AWSMachine.Spec.PrivateDNSName
input.CapacityReservationID = scope.AWSMachine.Spec.CapacityReservationID
s.scope.Debug("Running instance", "machine-role", scope.Role())
s.scope.Debug("Running instance with instance metadata options", "metadata options", input.InstanceMetadataOptions)
out, err := s.runInstance(scope.Role(), input)
if err != nil {
// Only record the failure event if the error is not related to failed dependencies.
// This is to avoid spamming failure events since the machine will be requeued by the actuator.
if !awserrors.IsFailedDependency(errors.Cause(err)) {
record.Warnf(scope.AWSMachine, "FailedCreate", "Failed to create instance: %v", err)
}
return nil, err
}
// Set the providerID and instanceID as soon as we create an instance so that we keep it in case of errors afterward
scope.SetProviderID(out.ID, out.AvailabilityZone)
scope.SetInstanceID(out.ID)
if len(input.NetworkInterfaces) > 0 {
for _, id := range input.NetworkInterfaces {
s.scope.Debug("Attaching security groups to provided network interface", "groups", input.SecurityGroupIDs, "interface", id)
if err := s.attachSecurityGroupsToNetworkInterface(input.SecurityGroupIDs, id); err != nil {
return nil, err
}
}
}
s.scope.Debug("Adding tags on each network interface from resource", "resource-id", out.ID)
// Fetching the network interfaces attached to the specific instance
networkInterfaces, err := s.getInstanceENIs(out.ID)
if err != nil {
return nil, err
}
s.scope.Debug("Fetched the network interfaces")
// Once all the network interfaces attached to the specific instance are found, the similar tags of instance are created for network interfaces too
if len(networkInterfaces) > 0 {
s.scope.Debug("Attempting to create tags from resource", "resource-id", out.ID)
for _, networkInterface := range networkInterfaces {
// Create/Update tags in AWS.
if err := s.UpdateResourceTags(networkInterface.NetworkInterfaceId, out.Tags, nil); err != nil {
return nil, errors.Wrapf(err, "failed to create tags for resource %q: ", *networkInterface.NetworkInterfaceId)
}
}
}
record.Eventf(scope.AWSMachine, "SuccessfulCreate", "Created new %s instance with id %q", scope.Role(), out.ID)
return out, nil
}
// findSubnet attempts to retrieve a subnet ID in the following order:
// - subnetID specified in machine configuration,
// - subnet based on filters in machine configuration
// - subnet based on the availability zone specified,
// - default to the first private subnet available.
func (s *Service) findSubnet(scope *scope.MachineScope) (string, error) {
// Check Machine.Spec.FailureDomain first as it's used by KubeadmControlPlane to spread machines across failure domains.
failureDomain := scope.Machine.Spec.FailureDomain
// We basically have 2 sources for subnets:
// 1. If subnet.id or subnet.filters are specified, we directly query AWS
// 2. All other cases use the subnets provided in the cluster network spec without ever calling AWS
switch {
case scope.AWSMachine.Spec.Subnet != nil && (scope.AWSMachine.Spec.Subnet.ID != nil || scope.AWSMachine.Spec.Subnet.Filters != nil):
criteria := []*ec2.Filter{
filter.EC2.SubnetStates(ec2.SubnetStatePending, ec2.SubnetStateAvailable),
}
if scope.AWSMachine.Spec.Subnet.ID != nil {
criteria = append(criteria, &ec2.Filter{Name: aws.String("subnet-id"), Values: aws.StringSlice([]string{*scope.AWSMachine.Spec.Subnet.ID})})
}
for _, f := range scope.AWSMachine.Spec.Subnet.Filters {
criteria = append(criteria, &ec2.Filter{Name: aws.String(f.Name), Values: aws.StringSlice(f.Values)})
}
subnets, err := s.getFilteredSubnets(criteria...)
if err != nil {
return "", errors.Wrapf(err, "failed to filter subnets for criteria %q", criteria)
}
if len(subnets) == 0 {
errMessage := fmt.Sprintf("failed to run machine %q, no subnets available matching criteria %q",
scope.Name(), criteria)
record.Warnf(scope.AWSMachine, "FailedCreate", errMessage)
return "", awserrors.NewFailedDependency(errMessage)
}
var filtered []*ec2.Subnet
var errMessage string
for _, subnet := range subnets {
if failureDomain != nil && *subnet.AvailabilityZone != *failureDomain {
// we could have included the failure domain in the query criteria, but then we end up with EC2 error
// messages that don't give a good hint about what is really wrong
errMessage += fmt.Sprintf(" subnet %q availability zone %q does not match failure domain %q.",
*subnet.SubnetId, *subnet.AvailabilityZone, *failureDomain)
continue
}
if ptr.Deref(scope.AWSMachine.Spec.PublicIP, false) {
matchingSubnet := s.scope.Subnets().FindByID(*subnet.SubnetId)
if matchingSubnet == nil {
errMessage += fmt.Sprintf(" unable to find subnet %q among the AWSCluster subnets.", *subnet.SubnetId)
continue
}
if !matchingSubnet.IsPublic {
errMessage += fmt.Sprintf(" subnet %q is a private subnet.", *subnet.SubnetId)
continue
}
}
tags := converters.TagsToMap(subnet.Tags)
if tags[infrav1.NameAWSSubnetAssociation] == infrav1.SecondarySubnetTagValue {
errMessage += fmt.Sprintf(" subnet %q belongs to a secondary CIDR block which won't be used to create instances.", *subnet.SubnetId)
continue
}
filtered = append(filtered, subnet)
}
// keep AWS returned orderz stable, but prefer a subnet in the cluster VPC
clusterVPC := s.scope.VPC().ID
sort.SliceStable(filtered, func(i, j int) bool {
return *filtered[i].VpcId == clusterVPC
})
if len(filtered) == 0 {
errMessage = fmt.Sprintf("failed to run machine %q, found %d subnets matching criteria but post-filtering failed.",
scope.Name(), len(subnets)) + errMessage
record.Warnf(scope.AWSMachine, "FailedCreate", errMessage)
return "", awserrors.NewFailedDependency(errMessage)
}
return *filtered[0].SubnetId, nil
case failureDomain != nil:
if scope.AWSMachine.Spec.PublicIP != nil && *scope.AWSMachine.Spec.PublicIP {
subnets := s.scope.Subnets().FilterPublic().FilterNonCni().FilterByZone(*failureDomain)
if len(subnets) == 0 {
errMessage := fmt.Sprintf("failed to run machine %q with public IP, no public subnets available in availability zone %q",
scope.Name(), *failureDomain)
record.Warnf(scope.AWSMachine, "FailedCreate", errMessage)
return "", awserrors.NewFailedDependency(errMessage)
}
return subnets[0].GetResourceID(), nil
}
subnets := s.scope.Subnets().FilterPrivate().FilterNonCni().FilterByZone(*failureDomain)
if len(subnets) == 0 {
errMessage := fmt.Sprintf("failed to run machine %q, no subnets available in availability zone %q",
scope.Name(), *failureDomain)
record.Warnf(scope.AWSMachine, "FailedCreate", errMessage)
return "", awserrors.NewFailedDependency(errMessage)
}
return subnets[0].GetResourceID(), nil
case scope.AWSMachine.Spec.PublicIP != nil && *scope.AWSMachine.Spec.PublicIP:
subnets := s.scope.Subnets().FilterPublic().FilterNonCni()
if len(subnets) == 0 {
errMessage := fmt.Sprintf("failed to run machine %q with public IP, no public subnets available", scope.Name())
record.Eventf(scope.AWSMachine, "FailedCreate", errMessage)
return "", awserrors.NewFailedDependency(errMessage)
}
return subnets[0].GetResourceID(), nil
// TODO(vincepri): Define a tag that would allow to pick a preferred subnet in an AZ when working
// with control plane machines.
default:
sns := s.scope.Subnets().FilterPrivate().FilterNonCni()
if len(sns) == 0 {
errMessage := fmt.Sprintf("failed to run machine %q, no subnets available", scope.Name())
record.Eventf(s.scope.InfraCluster(), "FailedCreateInstance", errMessage)
return "", awserrors.NewFailedDependency(errMessage)
}
return sns[0].GetResourceID(), nil
}
}
// getFilteredSubnets fetches subnets filtered based on the criteria passed.
func (s *Service) getFilteredSubnets(criteria ...*ec2.Filter) ([]*ec2.Subnet, error) {
out, err := s.EC2Client.DescribeSubnetsWithContext(context.TODO(), &ec2.DescribeSubnetsInput{Filters: criteria})
if err != nil {
return nil, err
}
return out.Subnets, nil
}
// GetCoreSecurityGroups looks up the security group IDs managed by this actuator
// They are considered "core" to its proper functioning.
func (s *Service) GetCoreSecurityGroups(scope *scope.MachineScope) ([]string, error) {
if scope.IsExternallyManaged() {
ids := make([]string, 0)
for _, sg := range scope.AWSMachine.Spec.AdditionalSecurityGroups {
if sg.ID == nil {
continue
}
ids = append(ids, *sg.ID)
}
return ids, nil
}
// These are common across both controlplane and node machines
sgRoles := []infrav1.SecurityGroupRole{
infrav1.SecurityGroupNode,
}
if !scope.IsEKSManaged() {
sgRoles = append(sgRoles, infrav1.SecurityGroupLB)
}
switch scope.Role() {
case "node":
// Just the common security groups above
if scope.IsEKSManaged() {
sgRoles = append(sgRoles, infrav1.SecurityGroupEKSNodeAdditional)
}
case "control-plane":
sgRoles = append(sgRoles, infrav1.SecurityGroupControlPlane)
default:
return nil, errors.Errorf("Unknown node role %q", scope.Role())
}
ids := make([]string, 0, len(sgRoles))
for _, sg := range sgRoles {
if _, ok := scope.AWSMachine.Spec.SecurityGroupOverrides[sg]; ok {
ids = append(ids, scope.AWSMachine.Spec.SecurityGroupOverrides[sg])
continue
}
if _, ok := s.scope.SecurityGroups()[sg]; ok {
ids = append(ids, s.scope.SecurityGroups()[sg].ID)
continue
}
return nil, awserrors.NewFailedDependency(fmt.Sprintf("%s security group not available", sg))
}
return ids, nil
}
// GetCoreNodeSecurityGroups looks up the security group IDs managed by this actuator
// They are considered "core" to its proper functioning.
func (s *Service) GetCoreNodeSecurityGroups(scope scope.LaunchTemplateScope) ([]string, error) {
// These are common across both controlplane and node machines
sgRoles := []infrav1.SecurityGroupRole{
infrav1.SecurityGroupNode,
}
if !scope.IsEKSManaged() {
sgRoles = append(sgRoles, infrav1.SecurityGroupLB)
} else {
sgRoles = append(sgRoles, infrav1.SecurityGroupEKSNodeAdditional)
}
ids := make([]string, 0, len(sgRoles))
for _, sg := range sgRoles {
if _, ok := s.scope.SecurityGroups()[sg]; !ok {
return nil, awserrors.NewFailedDependency(
fmt.Sprintf("%s security group not available", sg),
)
}
ids = append(ids, s.scope.SecurityGroups()[sg].ID)
}
return ids, nil
}
// TerminateInstance terminates an EC2 instance.
// Returns nil on success, error in all other cases.
func (s *Service) TerminateInstance(instanceID string) error {
s.scope.Debug("Attempting to terminate instance", "instance-id", instanceID)
input := &ec2.TerminateInstancesInput{
InstanceIds: aws.StringSlice([]string{instanceID}),
}
if _, err := s.EC2Client.TerminateInstancesWithContext(context.TODO(), input); err != nil {
return errors.Wrapf(err, "failed to terminate instance with id %q", instanceID)
}
s.scope.Debug("Terminated instance", "instance-id", instanceID)
return nil
}
// TerminateInstanceAndWait terminates and waits
// for an EC2 instance to terminate.
func (s *Service) TerminateInstanceAndWait(instanceID string) error {
if err := s.TerminateInstance(instanceID); err != nil {
return err
}
s.scope.Debug("Waiting for EC2 instance to terminate", "instance-id", instanceID)
input := &ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice([]string{instanceID}),
}
if err := s.EC2Client.WaitUntilInstanceTerminatedWithContext(context.TODO(), input); err != nil {
return errors.Wrapf(err, "failed to wait for instance %q termination", instanceID)
}
return nil
}
func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instance, error) {
input := &ec2.RunInstancesInput{
InstanceType: aws.String(i.Type),
ImageId: aws.String(i.ImageID),
KeyName: i.SSHKeyName,
EbsOptimized: i.EBSOptimized,
MaxCount: aws.Int64(1),
MinCount: aws.Int64(1),
UserData: i.UserData,
}
s.scope.Debug("userData size", "bytes", len(*i.UserData), "role", role)
if len(i.NetworkInterfaces) > 0 {
netInterfaces := make([]*ec2.InstanceNetworkInterfaceSpecification, 0, len(i.NetworkInterfaces))
for index, id := range i.NetworkInterfaces {
netInterfaces = append(netInterfaces, &ec2.InstanceNetworkInterfaceSpecification{
NetworkInterfaceId: aws.String(id),
DeviceIndex: aws.Int64(int64(index)),
})
}
netInterfaces[0].AssociatePublicIpAddress = i.PublicIPOnLaunch
input.NetworkInterfaces = netInterfaces
} else {
input.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(0),
SubnetId: aws.String(i.SubnetID),
Groups: aws.StringSlice(i.SecurityGroupIDs),
AssociatePublicIpAddress: i.PublicIPOnLaunch,
},
}
}
if i.IAMProfile != "" {
input.IamInstanceProfile = &ec2.IamInstanceProfileSpecification{
Name: aws.String(i.IAMProfile),
}
}
blockdeviceMappings := []*ec2.BlockDeviceMapping{}
if i.RootVolume != nil {
rootDeviceName, err := s.checkRootVolume(i.RootVolume, i.ImageID)
if err != nil {
return nil, err
}
i.RootVolume.DeviceName = aws.StringValue(rootDeviceName)
blockDeviceMapping := volumeToBlockDeviceMapping(i.RootVolume)
blockdeviceMappings = append(blockdeviceMappings, blockDeviceMapping)
}
for vi := range i.NonRootVolumes {
nonRootVolume := i.NonRootVolumes[vi]
if nonRootVolume.DeviceName == "" {
return nil, errors.Errorf("non root volume should have device name specified")
}
blockDeviceMapping := volumeToBlockDeviceMapping(&nonRootVolume)
blockdeviceMappings = append(blockdeviceMappings, blockDeviceMapping)
}
if len(blockdeviceMappings) != 0 {
input.BlockDeviceMappings = blockdeviceMappings
}
if len(i.Tags) > 0 {
resources := []string{ec2.ResourceTypeInstance, ec2.ResourceTypeVolume, ec2.ResourceTypeNetworkInterface}
for _, r := range resources {
spec := &ec2.TagSpecification{ResourceType: aws.String(r)}
// We need to sort keys for tests to work
keys := make([]string, 0, len(i.Tags))
for k := range i.Tags {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
spec.Tags = append(spec.Tags, &ec2.Tag{
Key: aws.String(key),
Value: aws.String(i.Tags[key]),
})
}
input.TagSpecifications = append(input.TagSpecifications, spec)
}
}
input.InstanceMarketOptions = getInstanceMarketOptionsRequest(i.SpotMarketOptions)
input.MetadataOptions = getInstanceMetadataOptionsRequest(i.InstanceMetadataOptions)
input.PrivateDnsNameOptions = getPrivateDNSNameOptionsRequest(i.PrivateDNSName)
input.CapacityReservationSpecification = getCapacityReservationSpecification(i.CapacityReservationID)
if i.Tenancy != "" {
input.Placement = &ec2.Placement{
Tenancy: &i.Tenancy,
}
}
if i.PlacementGroupName == "" && i.PlacementGroupPartition != 0 {
return nil, errors.Errorf("placementGroupPartition is set but placementGroupName is empty")
}
if i.PlacementGroupName != "" {
if input.Placement == nil {
input.Placement = &ec2.Placement{}
}
input.Placement.GroupName = &i.PlacementGroupName
if i.PlacementGroupPartition != 0 {
input.Placement.PartitionNumber = &i.PlacementGroupPartition
}
}
out, err := s.EC2Client.RunInstancesWithContext(context.TODO(), input)
if err != nil {
return nil, errors.Wrap(err, "failed to run instance")
}
if len(out.Instances) == 0 {
return nil, errors.Errorf("no instance returned for reservation %v", out.GoString())
}
return s.SDKToInstance(out.Instances[0])
}
func volumeToBlockDeviceMapping(v *infrav1.Volume) *ec2.BlockDeviceMapping {
ebsDevice := &ec2.EbsBlockDevice{
DeleteOnTermination: aws.Bool(true),
VolumeSize: aws.Int64(v.Size),
Encrypted: v.Encrypted,
}
if v.Throughput != nil {
ebsDevice.Throughput = v.Throughput
}
if v.IOPS != 0 {
ebsDevice.Iops = aws.Int64(v.IOPS)
}
if v.EncryptionKey != "" {
ebsDevice.Encrypted = aws.Bool(true)
ebsDevice.KmsKeyId = aws.String(v.EncryptionKey)
}
if v.Type != "" {
ebsDevice.VolumeType = aws.String(string(v.Type))
}
return &ec2.BlockDeviceMapping{
DeviceName: &v.DeviceName,
Ebs: ebsDevice,
}
}
// GetInstanceSecurityGroups returns a map from ENI id to the security groups applied to that ENI
// While some security group operations take place at the "instance" level, these are in fact an API convenience for manipulating the first ("primary") ENI's properties.
func (s *Service) GetInstanceSecurityGroups(instanceID string) (map[string][]string, error) {
enis, err := s.getInstanceENIs(instanceID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get ENIs for instance %q", instanceID)
}
out := make(map[string][]string)
for _, eni := range enis {
var groups []string
for _, group := range eni.Groups {
groups = append(groups, aws.StringValue(group.GroupId))
}
out[aws.StringValue(eni.NetworkInterfaceId)] = groups
}
return out, nil
}
// UpdateInstanceSecurityGroups modifies the security groups of the given
// EC2 instance.
func (s *Service) UpdateInstanceSecurityGroups(instanceID string, ids []string) error {
s.scope.Debug("Attempting to update security groups on instance", "instance-id", instanceID)
enis, err := s.getInstanceENIs(instanceID)
if err != nil {
return errors.Wrapf(err, "failed to get ENIs for instance %q", instanceID)
}
s.scope.Debug("Found ENIs on instance", "number-of-enis", len(enis), "instance-id", instanceID)
for _, eni := range enis {
if err := s.attachSecurityGroupsToNetworkInterface(ids, aws.StringValue(eni.NetworkInterfaceId)); err != nil {
return errors.Wrapf(err, "failed to modify network interfaces on instance %q", instanceID)
}
}
return nil
}
// UpdateResourceTags updates the tags for an instance.
// This will be called if there is anything to create (update) or delete.
// We may not always have to perform each action, so we check what we're
// receiving to avoid calling AWS if we don't need to.
func (s *Service) UpdateResourceTags(resourceID *string, create, remove map[string]string) error {
s.scope.Debug("Attempting to update tags on resource", "resource-id", *resourceID)
// If we have anything to create or update
if len(create) > 0 {
s.scope.Debug("Attempting to create tags on resource", "resource-id", *resourceID)
// Convert our create map into an array of *ec2.Tag
createTagsInput := converters.MapToTags(create)
// Create the CreateTags input.
input := &ec2.CreateTagsInput{
Resources: []*string{resourceID},
Tags: createTagsInput,
}
// Create/Update tags in AWS.
if _, err := s.EC2Client.CreateTagsWithContext(context.TODO(), input); err != nil {
return errors.Wrapf(err, "failed to create tags for resource %q: %+v", *resourceID, create)
}
}
// If we have anything to remove
if len(remove) > 0 {
s.scope.Debug("Attempting to delete tags on resource", "resource-id", *resourceID)
// Convert our remove map into an array of *ec2.Tag
removeTagsInput := converters.MapToTags(remove)
// Create the DeleteTags input
input := &ec2.DeleteTagsInput{
Resources: []*string{resourceID},
Tags: removeTagsInput,
}
// Delete tags in AWS.
if _, err := s.EC2Client.DeleteTagsWithContext(context.TODO(), input); err != nil {
return errors.Wrapf(err, "failed to delete tags for resource %q: %v", *resourceID, remove)
}
}
return nil
}
func (s *Service) getInstanceENIs(instanceID string) ([]*ec2.NetworkInterface, error) {
input := &ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("attachment.instance-id"),
Values: []*string{aws.String(instanceID)},
},
},
}
output, err := s.EC2Client.DescribeNetworkInterfacesWithContext(context.TODO(), input)
if err != nil {
return nil, err
}
return output.NetworkInterfaces, nil
}
func (s *Service) getImageRootDevice(imageID string) (*string, error) {
input := &ec2.DescribeImagesInput{
ImageIds: []*string{aws.String(imageID)},
}
output, err := s.EC2Client.DescribeImagesWithContext(context.TODO(), input)
if err != nil {
return nil, err
}
if len(output.Images) == 0 {
return nil, errors.Errorf("no images returned when looking up ID %q", imageID)
}
return output.Images[0].RootDeviceName, nil
}
func (s *Service) getImageSnapshotSize(imageID string) (*int64, error) {
input := &ec2.DescribeImagesInput{
ImageIds: []*string{aws.String(imageID)},
}
output, err := s.EC2Client.DescribeImagesWithContext(context.TODO(), input)
if err != nil {
return nil, err
}
if len(output.Images) == 0 {
return nil, errors.Errorf("no images returned when looking up ID %q", imageID)
}
if len(output.Images[0].BlockDeviceMappings) == 0 {
return nil, errors.Errorf("no block device mappings returned when looking up ID %q", imageID)
}
if output.Images[0].BlockDeviceMappings[0].Ebs == nil {
return nil, errors.Errorf("no EBS returned when looking up ID %q", imageID)
}
if output.Images[0].BlockDeviceMappings[0].Ebs.VolumeSize == nil {
return nil, errors.Errorf("no EBS volume size returned when looking up ID %q", imageID)
}
return output.Images[0].BlockDeviceMappings[0].Ebs.VolumeSize, nil
}
// SDKToInstance converts an AWS EC2 SDK instance to the CAPA instance type.
// SDKToInstance populates all instance fields except for rootVolumeSize,
// because EC2.DescribeInstances does not return the size of storage devices. An
// additional call to EC2 is required to get this value.
func (s *Service) SDKToInstance(v *ec2.Instance) (*infrav1.Instance, error) {
i := &infrav1.Instance{
ID: aws.StringValue(v.InstanceId),
State: infrav1.InstanceState(*v.State.Name),
Type: aws.StringValue(v.InstanceType),
SubnetID: aws.StringValue(v.SubnetId),
ImageID: aws.StringValue(v.ImageId),
SSHKeyName: v.KeyName,
PrivateIP: v.PrivateIpAddress,
PublicIP: v.PublicIpAddress,
ENASupport: v.EnaSupport,
EBSOptimized: v.EbsOptimized,
}
// Extract IAM Instance Profile name from ARN
// TODO: Handle this comparison more safely, perhaps by querying IAM for the
// instance profile ARN and comparing to the ARN returned by EC2
if v.IamInstanceProfile != nil && v.IamInstanceProfile.Arn != nil {
split := strings.Split(aws.StringValue(v.IamInstanceProfile.Arn), "instance-profile/")
if len(split) > 1 && split[1] != "" {
i.IAMProfile = split[1]
}
}
for _, sg := range v.SecurityGroups {
i.SecurityGroupIDs = append(i.SecurityGroupIDs, *sg.GroupId)
}
if len(v.Tags) > 0 {
i.Tags = converters.TagsToMap(v.Tags)
}
i.Addresses = s.getInstanceAddresses(v)
i.AvailabilityZone = aws.StringValue(v.Placement.AvailabilityZone)
for _, volume := range v.BlockDeviceMappings {
i.VolumeIDs = append(i.VolumeIDs, *volume.Ebs.VolumeId)
}
if v.MetadataOptions != nil {
metadataOptions := &infrav1.InstanceMetadataOptions{}
if v.MetadataOptions.HttpEndpoint != nil {
metadataOptions.HTTPEndpoint = infrav1.InstanceMetadataState(*v.MetadataOptions.HttpEndpoint)
}
if v.MetadataOptions.HttpPutResponseHopLimit != nil {
metadataOptions.HTTPPutResponseHopLimit = *v.MetadataOptions.HttpPutResponseHopLimit
}
if v.MetadataOptions.HttpTokens != nil {
metadataOptions.HTTPTokens = infrav1.HTTPTokensState(*v.MetadataOptions.HttpTokens)
}
if v.MetadataOptions.InstanceMetadataTags != nil {
metadataOptions.InstanceMetadataTags = infrav1.InstanceMetadataState(*v.MetadataOptions.InstanceMetadataTags)
}
i.InstanceMetadataOptions = metadataOptions
}
if v.PrivateDnsNameOptions != nil {
i.PrivateDNSName = &infrav1.PrivateDNSName{
EnableResourceNameDNSAAAARecord: v.PrivateDnsNameOptions.EnableResourceNameDnsAAAARecord,
EnableResourceNameDNSARecord: v.PrivateDnsNameOptions.EnableResourceNameDnsARecord,
HostnameType: v.PrivateDnsNameOptions.HostnameType,
}
}
return i, nil
}
func (s *Service) getInstanceAddresses(instance *ec2.Instance) []clusterv1.MachineAddress {
addresses := []clusterv1.MachineAddress{}
// Check if the DHCP Option Set has domain name set
domainName := s.GetDHCPOptionSetDomainName(s.EC2Client, instance.VpcId)
for _, eni := range instance.NetworkInterfaces {
privateDNSAddress := clusterv1.MachineAddress{
Type: clusterv1.MachineInternalDNS,
Address: aws.StringValue(eni.PrivateDnsName),
}
privateIPAddress := clusterv1.MachineAddress{
Type: clusterv1.MachineInternalIP,
Address: aws.StringValue(eni.PrivateIpAddress),
}
addresses = append(addresses, privateDNSAddress, privateIPAddress)
if domainName != nil {
// Add secondary private DNS Name with domain name set in DHCP Option Set
additionalPrivateDNSAddress := clusterv1.MachineAddress{
Type: clusterv1.MachineInternalDNS,
Address: fmt.Sprintf("%s.%s", strings.Split(privateDNSAddress.Address, ".")[0], *domainName),
}
addresses = append(addresses, additionalPrivateDNSAddress)
}
// An elastic IP is attached if association is non nil pointer
if eni.Association != nil {
publicDNSAddress := clusterv1.MachineAddress{
Type: clusterv1.MachineExternalDNS,
Address: aws.StringValue(eni.Association.PublicDnsName),
}
publicIPAddress := clusterv1.MachineAddress{
Type: clusterv1.MachineExternalIP,
Address: aws.StringValue(eni.Association.PublicIp),
}
addresses = append(addresses, publicDNSAddress, publicIPAddress)
}
}
return addresses
}
func (s *Service) getNetworkInterfaceSecurityGroups(interfaceID string) ([]string, error) {
input := &ec2.DescribeNetworkInterfaceAttributeInput{
Attribute: aws.String("groupSet"),
NetworkInterfaceId: aws.String(interfaceID),
}
output, err := s.EC2Client.DescribeNetworkInterfaceAttributeWithContext(context.TODO(), input)
if err != nil {
return nil, err
}
groups := make([]string, len(output.Groups))
for i := range output.Groups {
groups[i] = aws.StringValue(output.Groups[i].GroupId)
}
return groups, nil
}
func (s *Service) attachSecurityGroupsToNetworkInterface(groups []string, interfaceID string) error {
s.scope.Info("Updating security groups", "groups", groups)
input := &ec2.ModifyNetworkInterfaceAttributeInput{
NetworkInterfaceId: aws.String(interfaceID),
Groups: aws.StringSlice(groups),
}