-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
instance_volume_attachment.go
115 lines (92 loc) · 2.6 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2019 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 awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/klog/v2"
"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},
}
klog.V(2).Infof("found matching InstanceVolumeAttachment")
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
}