-
Notifications
You must be signed in to change notification settings - Fork 13
/
aws.go
1291 lines (1131 loc) · 35.4 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
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
package aws
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/opsworks"
sh "github.com/codeskyblue/go-sh"
"github.com/libopenstorage/cloudops"
"github.com/libopenstorage/cloudops/backoff"
"github.com/libopenstorage/cloudops/pkg/exec"
"github.com/libopenstorage/cloudops/unsupported"
awscredentials "github.com/libopenstorage/secrets/aws/credentials"
"github.com/portworx/sched-ops/k8s/core"
"github.com/portworx/sched-ops/task"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
)
const (
awsDevicePrefix = "/dev/sd"
awsDevicePrefixWithX = "/dev/xvd"
awsDevicePrefixWithH = "/dev/hd"
awsDevicePrefixNvme = "/dev/nvme"
contextTimeout = 30 * time.Second
awsErrorModificationNotFound = "InvalidVolumeModification.NotFound"
// Standard aws credential constants
awsAccessKeyName = "AWS_ACCESS_KEY_ID"
awsSecretAccessKeyName = "AWS_SECRET_ACCESS_KEY"
)
// For unit testing purpose
type ec2Wrapper struct {
Client ec2iface.EC2API
}
type awsOps struct {
cloudops.Compute
instanceType string
instance string
zone string
region string
outpostARN string
ec2 *ec2Wrapper
autoscaling *autoscaling.AutoScaling
mutex sync.Mutex
}
var (
// ErrAWSEnvNotAvailable is the error type when aws credentials are not set
ErrAWSEnvNotAvailable = fmt.Errorf("aws credentials are not set in environment")
nvmeCmd = exec.Which("nvme")
)
// NewClient creates a new cloud operations client for AWS
func NewClient(k8sSecretName, k8sSecretNamespace string) (cloudops.Ops, error) {
runningOnEc2 := true
zone, instanceID, instanceType, outpostARN, err := getInfoFromMetadata()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
logrus.Infof("Code %v", awsErr.Code())
}
}
if err != nil {
runningOnEc2 = false
// try to get it from env
zone, instanceID, instanceType, err = getInfoFromEnv()
if err != nil {
return nil, err
}
}
var (
staticAwsSecretKey string
staticAwsAccessKey string
)
if len(k8sSecretName) != 0 && len(k8sSecretNamespace) != 0 {
k8sSecret, err := core.Instance().GetSecret(k8sSecretName, k8sSecretNamespace)
if err == nil {
awsAccessKey, found := k8sSecret.Data[awsAccessKeyName]
if !found {
return nil, fmt.Errorf("%v not found in k8s secret %v", awsAccessKeyName, k8sSecretName)
}
staticAwsAccessKey = string(awsAccessKey)
awsSecretKey, found := k8sSecret.Data[awsSecretAccessKeyName]
if !found {
return nil, fmt.Errorf("%v not found in k8s secret %v", awsSecretAccessKeyName, k8sSecretName)
}
staticAwsSecretKey = string(awsSecretKey)
} // else try other secret providers
}
region := zone[:len(zone)-1]
awsCreds, err := awscredentials.NewAWSCredentials(staticAwsAccessKey, staticAwsSecretKey, "", runningOnEc2)
if err != nil {
return nil, err
}
creds, err := awsCreds.Get()
if err != nil {
return nil, err
}
ec2 := &ec2Wrapper{
ec2.New(
session.New(
&aws.Config{
Region: ®ion,
Credentials: creds,
},
),
),
}
autoscaling := autoscaling.New(
session.New(
&aws.Config{
Region: ®ion,
Credentials: creds,
},
),
)
return backoff.NewExponentialBackoffOps(
&awsOps{
Compute: unsupported.NewUnsupportedCompute(),
instance: instanceID,
instanceType: instanceType,
ec2: ec2,
zone: zone,
region: region,
autoscaling: autoscaling,
outpostARN: outpostARN,
},
isExponentialError,
backoff.DefaultExponentialBackoff,
), nil
}
func (s *awsOps) filters(
labels map[string]string,
keys []string,
) []*ec2.Filter {
if len(labels) == 0 {
return nil
}
f := make([]*ec2.Filter, len(labels)+len(keys))
i := 0
for k, v := range labels {
s := string("tag:") + k
value := v
f[i] = &ec2.Filter{Name: &s, Values: []*string{&value}}
i++
}
for _, k := range keys {
s := string("tag-key:") + k
f[i] = &ec2.Filter{Name: &s}
i++
}
return f
}
func (s *awsOps) tags(labels map[string]string) []*ec2.Tag {
if len(labels) == 0 {
return nil
}
t := make([]*ec2.Tag, len(labels))
i := 0
for k, v := range labels {
key := k
value := v
t[i] = &ec2.Tag{Key: &key, Value: &value}
i++
}
return t
}
func (s *awsOps) waitStatus(id string, desired string) error {
request := &ec2.DescribeVolumesInput{VolumeIds: []*string{&id}}
actual := ""
_, err := task.DoRetryWithTimeout(
func() (interface{}, bool, error) {
awsVols, err := s.ec2.Client.DescribeVolumes(request)
if err != nil {
return nil, true, err
}
if len(awsVols.Volumes) != 1 {
return nil, true, fmt.Errorf("expected one volume %v got %v",
id, len(awsVols.Volumes))
}
if awsVols.Volumes[0].State == nil {
return nil, true, fmt.Errorf("Nil volume state for %v", id)
}
actual = *awsVols.Volumes[0].State
if actual == desired {
return nil, false, nil
}
return nil, true, fmt.Errorf(
"Volume %v did not transition to %v current state %v",
id, desired, actual)
},
cloudops.ProviderOpsTimeout,
cloudops.ProviderOpsRetryInterval)
return err
}
func (s *awsOps) waitAttachmentStatus(
volumeID string,
desired string,
timeout time.Duration,
) (*ec2.Volume, error) {
id := volumeID
request := &ec2.DescribeVolumesInput{VolumeIds: []*string{&id}}
interval := 2 * time.Second
logrus.Infof("Waiting for state transition to %q", desired)
f := func() (interface{}, bool, error) {
awsVols, err := s.ec2.Client.DescribeVolumes(request)
if err != nil {
return nil, false, err
}
if len(awsVols.Volumes) != 1 {
return nil, false, fmt.Errorf("expected one volume %v got %v",
volumeID, len(awsVols.Volumes))
}
var actual string
vol := awsVols.Volumes[0]
awsAttachment := vol.Attachments
if awsAttachment == nil || len(awsAttachment) == 0 {
// We have encountered scenarios where AWS returns a nil attachment state
// for a volume transitioning from detaching -> attaching.
actual = ec2.VolumeAttachmentStateDetached
} else {
actual = *awsAttachment[0].State
}
if actual == desired {
return vol, false, nil
}
return nil, true, fmt.Errorf("Volume %v failed to transition to %v current state %v",
volumeID, desired, actual)
}
outVol, err := task.DoRetryWithTimeout(f, timeout, interval)
if err != nil {
return nil, err
}
if vol, ok := outVol.(*ec2.Volume); ok {
return vol, nil
}
return nil, cloudops.NewStorageError(cloudops.ErrVolInval,
fmt.Sprintf("Invalid volume object for volume %s", volumeID), "")
}
func (s *awsOps) Name() string { return string(cloudops.AWS) }
func (s *awsOps) InstanceID() string { return s.instance }
func (s *awsOps) InspectInstance(instanceID string) (*cloudops.InstanceInfo, error) {
inst, err := DescribeInstanceByID(s.ec2, instanceID)
if err != nil {
return nil, err
}
name := instanceID
labels := labelsFromTags(inst.Tags)
if nameFromTags, present := labels["Name"]; present && len(nameFromTags) > 0 {
name = nameFromTags
}
instInfo := &cloudops.InstanceInfo{
CloudResourceInfo: cloudops.CloudResourceInfo{
Name: name,
ID: *inst.InstanceId,
Zone: s.zone,
Region: s.region,
Labels: labels,
},
}
return instInfo, nil
}
func (s *awsOps) InspectInstanceGroupForInstance(instanceID string) (*cloudops.InstanceGroupInfo, error) {
selfInfo, err := s.InspectInstance(instanceID)
if err != nil {
return nil, err
}
for tag, value := range selfInfo.Labels {
// https://docs.aws.amazon.com/autoscaling/ec2/userguide/autoscaling-tagging.html#tag-lifecycle
if tag == "aws:autoscaling:groupName" {
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(value),
},
}
result, err := s.autoscaling.DescribeAutoScalingGroups(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
return nil, aerr
}
return nil, err
}
if len(result.AutoScalingGroups) != 1 {
return nil, fmt.Errorf("DescribeAutoScalingGroups (%v) returned %v groups, expect 1",
value, len(result.AutoScalingGroups))
}
group := result.AutoScalingGroups[0]
zones := make([]string, 0)
for _, z := range group.AvailabilityZones {
zones = append(zones, *z)
}
retval := &cloudops.InstanceGroupInfo{
CloudResourceInfo: cloudops.CloudResourceInfo{
Name: *group.AutoScalingGroupName,
Zone: s.zone,
Region: s.region,
Labels: labelsFromTags(group.Tags),
},
Zones: zones,
AutoscalingEnabled: true,
Min: group.MinSize,
Max: group.MaxSize,
}
return retval, nil
}
}
return nil, &cloudops.ErrNoInstanceGroup{}
}
func (s *awsOps) ApplyTags(volumeID string, labels map[string]string, options map[string]string) error {
req := &ec2.CreateTagsInput{
Resources: []*string{&volumeID},
Tags: s.tags(labels),
DryRun: dryRun(options),
}
_, err := s.ec2.Client.CreateTags(req)
return err
}
func (s *awsOps) RemoveTags(volumeID string, labels map[string]string, options map[string]string) error {
req := &ec2.DeleteTagsInput{
Resources: []*string{&volumeID},
Tags: s.tags(labels),
DryRun: dryRun(options),
}
_, err := s.ec2.Client.DeleteTags(req)
return err
}
func (s *awsOps) matchTag(tag *ec2.Tag, match string) bool {
return tag.Key != nil &&
tag.Value != nil &&
len(*tag.Key) != 0 &&
len(*tag.Value) != 0 &&
*tag.Key == match
}
func (s *awsOps) DeviceMappings() (map[string]string, error) {
instance, err := s.describe()
if err != nil {
return nil, err
}
m := make(map[string]string)
for _, d := range instance.BlockDeviceMappings {
if d.DeviceName != nil && d.Ebs != nil && d.Ebs.VolumeId != nil {
devName := *d.DeviceName
// Skip the root device
if devName == *instance.RootDeviceName {
continue
}
devicePath, err := s.getActualDevicePath(devName, *d.Ebs.VolumeId)
if err != nil {
return nil, cloudops.NewStorageError(
cloudops.ErrInvalidDevicePath,
fmt.Sprintf("unable to get actual device path for %s. %v", devName, err),
s.instance)
}
m[devicePath] = *d.Ebs.VolumeId
}
}
return m, nil
}
// Describe current instance.
func (s *awsOps) Describe() (interface{}, error) {
return s.describe()
}
func (s *awsOps) describe() (*ec2.Instance, error) {
request := &ec2.DescribeInstancesInput{
InstanceIds: []*string{&s.instance},
}
out, err := s.ec2.Client.DescribeInstances(request)
if err != nil {
return nil, err
}
if len(out.Reservations) != 1 {
return nil, fmt.Errorf("DescribeInstances(%v) returned %v reservations, expect 1",
s.instance, len(out.Reservations))
}
if len(out.Reservations[0].Instances) != 1 {
return nil, fmt.Errorf("DescribeInstances(%v) returned %v Reservations, expect 1",
s.instance, len(out.Reservations[0].Instances))
}
return out.Reservations[0].Instances[0], nil
}
func (s *awsOps) getPrefixFromRootDeviceName(rootDeviceName string) (string, error) {
devPrefix := awsDevicePrefix
if !strings.HasPrefix(rootDeviceName, devPrefix) {
devPrefix = awsDevicePrefixWithX
if !strings.HasPrefix(rootDeviceName, devPrefix) {
devPrefix = awsDevicePrefixWithH
if !strings.HasPrefix(rootDeviceName, devPrefix) {
return "", fmt.Errorf("unknown prefix type on root device: %s",
rootDeviceName)
}
}
}
return devPrefix, nil
}
// getParentDevice returns the parent device of the given device path
// by following the symbolic link. It is expected that the input device
// path exists
func (s *awsOps) getParentDevice(ipDevPath string) (string, error) {
// Check if the path is a symbolic link
var parentDevPath string
fi, err := os.Lstat(ipDevPath)
if err != nil {
return "", err
}
if fi.Mode()&os.ModeSymlink != 0 {
// input device path is a symbolic link
// get the parent device
output, err := filepath.EvalSymlinks(ipDevPath)
if err != nil {
return "", fmt.Errorf("failed to read symlink due to: %v", err)
}
parentDevPath = strings.TrimSpace(string(output))
} else {
parentDevPath = ipDevPath
}
return parentDevPath, nil
}
// getActualDevicePath does an os.Stat on the provided devicePath.
// If not found it will try all the different devicePrefixes provided by AWS
// such as /dev/sd and /dev/xvd and return the devicePath which is found
// or return an error
func (s *awsOps) getActualDevicePath(ipDevicePath, volumeID string) (string, error) {
var err error
letter := ipDevicePath[len(ipDevicePath)-1:]
devicePath := awsDevicePrefix + letter
if _, err = os.Stat(devicePath); err == nil {
return s.getParentDevice(devicePath)
}
devicePath = awsDevicePrefixWithX + letter
if _, err = os.Stat(devicePath); err == nil {
return s.getParentDevice(devicePath)
}
devicePath = awsDevicePrefixWithH + letter
if _, err = os.Stat(devicePath); err == nil {
return s.getParentDevice(devicePath)
}
if devicePath, err = s.getNvmeDeviceFromVolumeID(volumeID); err == nil {
if _, err = os.Stat(devicePath); err == nil {
return devicePath, nil
}
}
return "", fmt.Errorf("unable to map volume %v with block device mapping %v to an"+
" actual device path on the host", volumeID, ipDevicePath)
}
func (s *awsOps) getNvmeDeviceFromVolumeID(volumeID string) (string, error) {
// We will use nvme list command to find nvme device mappings
// A typical output of nvme list looks like this
// # nvme list
// Node SN Model Namespace Usage Format FW Rev
// ---------------- -------------------- ---------------------------------------- --------- -------------------------- ---------------- --------
// /dev/nvme0n1 vol00fd6f8c30dc619f4 Amazon Elastic Block Store 1 0.00 B / 137.44 GB 512 B + 0 B 1.0
// /dev/nvme1n1 vol044e12c8c0af45b3d Amazon Elastic Block Store 1 0.00 B / 107.37 GB 512 B + 0 B 1.0
trimmedVolumeID := strings.Replace(volumeID, "-", "", 1)
out, err := sh.Command(nvmeCmd, "list").Command("grep", trimmedVolumeID).Command("awk", "{print $1}").Output()
if err != nil {
return "", fmt.Errorf("unable to map %v volume to an nvme device: %v", volumeID, err)
}
return strings.TrimSpace(string(out)), nil
}
// GetMetadataInstance is the function to be called when trying to get metadata/user data on eks.
func GetMetadataInstance() (*ec2metadata.EC2Metadata, error) {
sess, err := session.NewSession()
if err != nil {
return nil, err
}
metadata := ec2metadata.New(sess)
if err != nil {
return nil, fmt.Errorf("failed to init aws provider. "+
"Failed to init from metadata due to: %v", err)
}
return metadata, nil
}
// GetMetadataWithTimeoutAndBackoff will get metadata with exponential backoff.
func GetMetadataWithTimeoutAndBackoff(metadata *ec2metadata.EC2Metadata, path string) (string, error) {
out, err := helperWithTimeoutAndBackoff(metadata, path)
return out, err
}
// GetUserDataWithTimeoutAndBackoff will get user data with exponential backoff.
func GetUserDataWithTimeoutAndBackoff(metadata *ec2metadata.EC2Metadata) (string, error) {
return helperWithTimeoutAndBackoff(metadata, "")
}
// RatelimitingExponentialBackoff will lead to a backoff of a max of around 2 minutes. TestScale unit tests was used to come up with this empherical number.
var RatelimitingExponentialBackoff = wait.Backoff{
Duration: 2 * time.Second, // the base duration
Factor: 2.0, // Duration is multiplied by factor each iteration
Jitter: 1.0, // The amount of jitter applied each iteration
Steps: 5, // Exit with error after this many steps
}
func helperWithTimeoutAndBackoff(metadata *ec2metadata.EC2Metadata, p string) (string, error) {
var (
origErr error
result string
)
conditionFn := func() (bool, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), contextTimeout)
defer cancelFn()
if p != "" {
result, origErr = metadata.GetMetadataWithContext(ctx, p)
} else {
result, origErr = metadata.GetUserDataWithContext(ctx)
}
if origErr != nil && !isErrorCode404(origErr) && (strings.Contains(origErr.Error(), "Client.Timeout exceeded while awaiting headers") || isExponentialError(origErr)) {
logrus.Errorf("Retrying aws metadata ops after backoff")
return false, nil
}
return true, origErr
}
expErr := wait.ExponentialBackoff(RatelimitingExponentialBackoff, conditionFn)
if expErr == wait.ErrWaitTimeout {
return "", cloudops.NewStorageError(cloudops.ErrExponentialTimeout, origErr.Error(), "")
}
return result, origErr
}
func (s *awsOps) FreeDevices() ([]string, error) {
self, err := s.describe()
if err != nil {
return nil, err
}
rootDeviceName := *self.RootDeviceName
var blockDeviceMappings = make([]interface{}, len(self.BlockDeviceMappings))
for i, b := range self.BlockDeviceMappings {
blockDeviceMappings[i] = b
}
freeLetterTracker := []byte("fghijklmnop")
devNamesInUse := make(map[string]string) // used as a set, values not used
// We also need to fetch ephemeral device mappings as they are not populated
// in blockDeviceMappings
// See bottom of this page:
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html?icmpid=docs_ec2_console#instance-block-device-mapping
c, err := GetMetadataInstance()
if err != nil {
return nil, err
}
mappingsFromMetadata, err := GetMetadataWithTimeoutAndBackoff(c, "block-device-mapping")
if err != nil {
return nil, err
}
devices := strings.Split(mappingsFromMetadata, "\n")
for _, device := range devices {
if device == "root" || device == "ami" {
continue
}
devName, err := GetMetadataWithTimeoutAndBackoff(c, "block-device-mapping/"+device)
if err != nil {
return nil, err
}
if !strings.HasPrefix(devName, "/dev/") {
devName = "/dev/" + devName
}
devNamesInUse[devName] = ""
}
devPrefix := awsDevicePrefix
for _, d := range blockDeviceMappings {
dev := d.(*ec2.InstanceBlockDeviceMapping)
if dev.DeviceName == nil {
return nil, fmt.Errorf("Nil device name")
}
devName := *dev.DeviceName
if devName == rootDeviceName {
continue
}
devNamesInUse[devName] = ""
}
for devName := range devNamesInUse {
// Extract the letter from the devName (e.g extract 'f' from '/dev/sdf')
if !strings.HasPrefix(devName, devPrefix) {
devPrefix = awsDevicePrefixWithX
if !strings.HasPrefix(devName, devPrefix) {
devPrefix = awsDevicePrefixWithH
if !strings.HasPrefix(devName, devPrefix) {
return nil, fmt.Errorf("bad device name %q", devName)
}
}
}
letter := devName[len(devPrefix):]
// Reset devPrefix for next devices
devPrefix = awsDevicePrefix
// AWS instances can have the following device names
// /dev/xvd[b-c][a-z]
if len(letter) == 1 {
index := letter[0] - 'f'
if index > ('p' - 'f') {
continue
}
freeLetterTracker[index] = '0' // mark as used
} else if len(letter) == 2 {
// We do not attach EBS volumes with "/dev/xvdc[a-z]" formats
continue
} else {
return nil, fmt.Errorf("cannot parse device name %q", devName)
}
}
// Set the prefix to the same one used as the root drive
// The reason we do this is based on the virtualization type AWS might attach
// the device "sda" at /dev/sda OR /dev/xvda. So we look at how the root device
// is attached and use that prefix
devPrefix, err = s.getPrefixFromRootDeviceName(rootDeviceName)
if err != nil {
return nil, err
}
free := make([]string, len(freeLetterTracker))
count := 0
for _, b := range freeLetterTracker {
if b != '0' {
free[count] = devPrefix + string(b)
count++
}
}
if count == 0 {
return nil, fmt.Errorf("No more free devices")
}
return reverse(free[:count]), nil
}
func (s *awsOps) rollbackCreate(id string, createErr error) error {
logrus.Warnf("Rollback create volume %v, Error %v", id, createErr)
err := s.Delete(id, nil)
if err != nil {
logrus.Warnf("Rollback failed volume %v, Error %v", id, err)
}
return createErr
}
func (s *awsOps) refreshVol(id *string) (*ec2.Volume, error) {
vols, err := s.Inspect([]*string{id}, nil)
if err != nil {
return nil, err
}
if len(vols) != 1 {
return nil, fmt.Errorf("failed to get vol: %s."+
"Found: %d volumes on inspecting", *id, len(vols))
}
resp, ok := vols[0].(*ec2.Volume)
if !ok {
return nil, cloudops.NewStorageError(cloudops.ErrVolInval,
fmt.Sprintf("Invalid volume returned by inspect API for vol: %s", *id),
"")
}
return resp, nil
}
func (s *awsOps) deleted(v *ec2.Volume) bool {
return *v.State == ec2.VolumeStateDeleting ||
*v.State == ec2.VolumeStateDeleted
}
func (s *awsOps) available(v *ec2.Volume) bool {
return *v.State == ec2.VolumeStateAvailable
}
func (s *awsOps) GetDeviceID(vol interface{}) (string, error) {
if d, ok := vol.(*ec2.Volume); ok {
return *d.VolumeId, nil
} else if d, ok := vol.(*ec2.Snapshot); ok {
return *d.SnapshotId, nil
} else {
return "", fmt.Errorf("invalid type: %v given to GetDeviceID", vol)
}
}
func (s *awsOps) Inspect(volumeIds []*string, options map[string]string) ([]interface{}, error) {
req := &ec2.DescribeVolumesInput{
VolumeIds: volumeIds,
DryRun: dryRun(options),
}
resp, err := s.ec2.Client.DescribeVolumes(req)
if err != nil {
return nil, err
}
var awsVols = make([]interface{}, len(resp.Volumes))
for i, v := range resp.Volumes {
awsVols[i] = v
}
return awsVols, nil
}
func (s *awsOps) Tags(volumeID string) (map[string]string, error) {
vol, err := s.refreshVol(&volumeID)
if err != nil {
return nil, err
}
labels := make(map[string]string)
for _, tag := range vol.Tags {
labels[*tag.Key] = *tag.Value
}
return labels, nil
}
func (s *awsOps) Enumerate(
volumeIds []*string,
labels map[string]string,
setIdentifier string,
) (map[string][]interface{}, error) {
sets := make(map[string][]interface{})
// Enumerate all volumes that have same labels.
f := s.filters(labels, nil)
req := &ec2.DescribeVolumesInput{Filters: f, VolumeIds: volumeIds}
awsVols, err := s.ec2.Client.DescribeVolumes(req)
if err != nil {
return nil, err
}
// Volume sets are identified by volumes with the same setIdentifer.
for _, vol := range awsVols.Volumes {
if s.deleted(vol) {
continue
}
if len(setIdentifier) == 0 {
cloudops.AddElementToMap(sets, vol, cloudops.SetIdentifierNone)
} else {
found := false
for _, tag := range vol.Tags {
if s.matchTag(tag, setIdentifier) {
cloudops.AddElementToMap(sets, vol, *tag.Value)
found = true
break
}
}
if !found {
cloudops.AddElementToMap(sets, vol, cloudops.SetIdentifierNone)
}
}
}
return sets, nil
}
func (s *awsOps) Create(
v interface{},
labels map[string]string,
options map[string]string,
) (interface{}, error) {
vol, ok := v.(*ec2.Volume)
if !ok {
return nil, cloudops.NewStorageError(cloudops.ErrVolInval,
"Invalid volume template given", "")
}
if vol.VolumeType == nil {
return nil, cloudops.NewStorageError(cloudops.ErrVolInval,
"Drive type not specified in the storage spec", "")
}
req := &ec2.CreateVolumeInput{
AvailabilityZone: vol.AvailabilityZone,
Encrypted: vol.Encrypted,
KmsKeyId: vol.KmsKeyId,
Size: vol.Size,
VolumeType: vol.VolumeType,
SnapshotId: vol.SnapshotId,
Throughput: vol.Throughput,
DryRun: dryRun(options),
}
if len(s.outpostARN) > 0 {
outpostARN := s.outpostARN
req.OutpostArn = &outpostARN
}
if len(vol.Tags) > 0 || len(labels) > 0 {
// Need to tag volumes on creation
tagSpec := &ec2.TagSpecification{}
tagSpec.SetResourceType(ec2.ResourceTypeVolume)
volTags := []*ec2.Tag{}
for _, tag := range vol.Tags {
// Make a copy of the keys and values
key := *tag.Key
value := *tag.Value
volTags = append(volTags, &ec2.Tag{Key: &key, Value: &value})
}
for k, v := range labels {
// Make a copy of the keys and values
key := k
value := v
volTags = append(volTags, &ec2.Tag{Key: &key, Value: &value})
}
tagSpec.Tags = volTags
req.TagSpecifications = []*ec2.TagSpecification{tagSpec}
}
// note, as of 2021-05-04, `opsworks` does not have `const VolumeTypeGp3 = gp3` (using RAW format)
if *vol.VolumeType == opsworks.VolumeTypeIo1 || *vol.VolumeType == "gp3" {
req.Iops = vol.Iops
}
resp, err := s.ec2.Client.CreateVolume(req)
if err != nil {
return nil, err
}
if err = s.waitStatus(
*resp.VolumeId,
ec2.VolumeStateAvailable,
); err != nil {
return nil, s.rollbackCreate(*resp.VolumeId, err)
}
return s.refreshVol(resp.VolumeId)
}
func (s *awsOps) DeleteFrom(id, _ string) error {
return s.Delete(id, nil)
}
func (s *awsOps) Delete(id string, options map[string]string) error {
req := &ec2.DeleteVolumeInput{
VolumeId: &id,
DryRun: dryRun(options),
}
_, err := s.ec2.Client.DeleteVolume(req)
return err
}
func (s *awsOps) Attach(volumeID string, options map[string]string) (string, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
devices, err := s.FreeDevices()
if err != nil {
return "", err
}
for _, device := range devices {
req := &ec2.AttachVolumeInput{
Device: &device,
InstanceId: &s.instance,
VolumeId: &volumeID,
DryRun: dryRun(options),
}
if _, err := s.ec2.Client.AttachVolume(req); err != nil {
if strings.Contains(err.Error(), "is already in use") {
logrus.Infof("Skipping device: %s as it's in use. Will try next free device", device)
continue
}
return "", err
}
vol, err := s.waitAttachmentStatus(
volumeID,
ec2.VolumeAttachmentStateAttached,
time.Minute,
)
if err != nil {
return "", err
}
return s.DevicePath(*vol.VolumeId)
}
return "", fmt.Errorf("failed to attach any of the free devices. Attempted: %v", devices)
}
func (s *awsOps) Detach(volumeID string, options map[string]string) error {
return s.detachInternal(volumeID, s.instance, options)
}
func (s *awsOps) DetachFrom(volumeID, instanceName string) error {
return s.detachInternal(volumeID, instanceName, nil)
}
func (s *awsOps) detachInternal(volumeID, instanceName string, options map[string]string) error {
force := false
req := &ec2.DetachVolumeInput{
InstanceId: &instanceName,
VolumeId: &volumeID,
Force: &force,
DryRun: dryRun(options),
}
if _, err := s.ec2.Client.DetachVolume(req); err != nil {
return err
}
_, err := s.waitAttachmentStatus(volumeID,
ec2.VolumeAttachmentStateDetached,
time.Minute,
)
return err
}
func isErrorModificationNotFound(err error) bool {
return strings.HasPrefix(err.Error(), awsErrorModificationNotFound)
}
func (s *awsOps) AreVolumesReadyToExpand(volumeIDs []*string) (bool, error) {
modificationStateRequest := &ec2.DescribeVolumesModificationsInput{
VolumeIds: volumeIDs,
}
describeOutput, err := s.ec2.Client.DescribeVolumesModifications(modificationStateRequest)
if err != nil {
// modification state not found, this indicates no change has occurred before.
if isErrorModificationNotFound(err) {
return true, nil
}
// in the case of getting unclassified request failure, result of this checker may be bypassed
// to not block volume resize operation.
return false, &cloudops.ErrCloudProviderRequestFailure{
Request: "DescribeVolumesModifications",
Message: err.Error(),
}
}
states := describeOutput.VolumesModifications
var state string
for i := 0; i < len(states); i++ {
if states[i] == nil || states[i].ModificationState == nil {
logrus.Debugf("volume modification state is nil for volume id: %s", *volumeIDs[i])
continue