This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_volume_attachment.go
99 lines (80 loc) · 2.05 KB
/
instance_volume_attachment.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
package awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
)
type InstanceVolumeAttachment struct {
Instance *Instance
Volume *EBSVolume
Device *string
}
func (e *InstanceVolumeAttachment) String() string {
return fi.TaskAsString(e)
}
func (e *InstanceVolumeAttachment) Find(c *fi.Context) (*InstanceVolumeAttachment, error) {
cloud := c.Cloud.(*awsup.AWSCloud)
instanceID := e.Instance.ID
volumeID := e.Volume.ID
if instanceID == nil || volumeID == nil {
return nil, nil
}
instance, err := cloud.DescribeInstance(*instanceID)
if err != nil {
return nil, err
}
for _, bdm := range instance.BlockDeviceMappings {
if bdm.Ebs == nil {
continue
}
if aws.StringValue(bdm.Ebs.VolumeId) != *volumeID {
continue
}
actual := &InstanceVolumeAttachment{
Device: bdm.DeviceName,
Instance: &Instance{ID: instance.InstanceId},
Volume: &EBSVolume{ID: bdm.Ebs.VolumeId},
}
glog.V(2).Infof("found matching InstanceVolumeAttachmen")
return actual, nil
}
return nil, nil
}
func (e *InstanceVolumeAttachment) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *InstanceVolumeAttachment) CheckChanges(a, e, changes *InstanceVolumeAttachment) error {
if a != nil {
if changes.Device != nil {
// TODO: Support this?
return fi.CannotChangeField("Device")
}
}
if a == nil {
if e.Device == nil {
return fi.RequiredField("Device")
}
}
return nil
}
func (_ *InstanceVolumeAttachment) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *InstanceVolumeAttachment) error {
if a == nil {
err := t.WaitForInstanceRunning(*e.Instance.ID)
if err != nil {
return err
}
request := &ec2.AttachVolumeInput{
InstanceId: e.Instance.ID,
VolumeId: e.Volume.ID,
Device: e.Device,
}
_, err = t.Cloud.EC2.AttachVolume(request)
if err != nil {
return fmt.Errorf("error creating InstanceVolumeAttachment: %v", err)
}
}
return nil // no tags
}