/
ec2.go
314 lines (281 loc) · 8.77 KB
/
ec2.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
package aws
import (
"fmt"
"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"
log "github.com/sirupsen/logrus"
)
const (
defaultClusterID = "unknown-cluster"
kubernetesClusterLegacyTag = "KubernetesCluster"
clusterIDTag = "ClusterID" // TODO(sszuecs): deprecated fallback cleanup
clusterIDTagPrefix = "kubernetes.io/cluster/"
resourceLifecycleOwned = "owned"
kubernetesCreatorTag = "kubernetes:application"
autoScalingGroupNameTag = "aws:autoscaling:groupName"
runningState = 16 // See https://github.com/aws/aws-sdk-go/blob/master/service/ec2/api.go, type InstanceState
stoppedState = 80 // See https://github.com/aws/aws-sdk-go/blob/master/service/ec2/api.go, type InstanceState
elbRoleTagName = "kubernetes.io/role/elb"
internalELBRoleTagName = "kubernetes.io/role/internal-elb"
kubernetesNodeRoleTag = "k8s.io/role/node"
)
type securityGroupDetails struct {
name string
id string
}
type instanceDetails struct {
id string
ip string
vpcID string
tags map[string]string
running bool
}
func (id *instanceDetails) clusterID() string {
for name, value := range id.tags {
if strings.HasPrefix(name, clusterIDTagPrefix) && value == resourceLifecycleOwned {
return strings.TrimPrefix(name, clusterIDTagPrefix)
}
}
for name, value := range id.tags {
if name == kubernetesClusterLegacyTag {
return value
}
}
return defaultClusterID
}
type subnetDetails struct {
id string
availabilityZone string
tags map[string]string
public bool
}
func (sd *subnetDetails) String() string {
return fmt.Sprintf("%s (%s) @ %s (public: %t)", sd.Name(), sd.id, sd.availabilityZone, sd.public)
}
func (sd *subnetDetails) Name() string {
if n, err := getNameTag(sd.tags); err == nil {
return n
}
return "unknown-subnet"
}
func getAutoScalingGroupName(instanceTags map[string]string) (string, error) {
if len(instanceTags) < 1 {
return "", ErrMissingAutoScalingGroupTag
}
asg, has := instanceTags[autoScalingGroupNameTag]
if !has || asg == "" {
return "", ErrMissingAutoScalingGroupTag
}
return asg, nil
}
func getInstanceDetails(ec2Service ec2iface.EC2API, instanceID string) (*instanceDetails, error) {
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-id"),
Values: []*string{
aws.String(instanceID),
},
},
},
}
resp, err := ec2Service.DescribeInstances(params)
if err != nil || resp == nil {
return nil, fmt.Errorf("unable to get details for instance %q: %w", instanceID, err)
}
i, err := findFirstRunningInstance(resp)
if err != nil {
return nil, fmt.Errorf("unable to find instance %q: %w", instanceID, err)
}
return &instanceDetails{
id: aws.StringValue(i.InstanceId),
ip: aws.StringValue(i.PrivateIpAddress),
vpcID: aws.StringValue(i.VpcId),
tags: convertEc2Tags(i.Tags),
running: aws.Int64Value(i.State.Code)&0xff == runningState,
}, nil
}
func getInstancesDetailsWithFilters(ec2Service ec2iface.EC2API, filters []*ec2.Filter) (map[string]*instanceDetails, error) {
params := &ec2.DescribeInstancesInput{
Filters: filters,
}
result := make(map[string]*instanceDetails)
err := ec2Service.DescribeInstancesPages(params, func(resp *ec2.DescribeInstancesOutput, lastPage bool) bool {
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
result[aws.StringValue(instance.InstanceId)] = &instanceDetails{
id: aws.StringValue(instance.InstanceId),
ip: aws.StringValue(instance.PrivateIpAddress),
vpcID: aws.StringValue(instance.VpcId),
tags: convertEc2Tags(instance.Tags),
running: aws.Int64Value(instance.State.Code)&0xff == runningState,
}
}
}
return true
})
if err != nil {
return nil, fmt.Errorf("failed getting instance list from EC2: %w", err)
}
return result, nil
}
func findFirstRunningInstance(resp *ec2.DescribeInstancesOutput) (*ec2.Instance, error) {
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
// The low byte represents the state. The high byte is an opaque internal value
// and should be ignored.
if aws.Int64Value(instance.State.Code)&0xff == runningState {
return instance, nil
}
}
}
return nil, ErrNoRunningInstances
}
func getSubnets(svc ec2iface.EC2API, vpcID, clusterID string) ([]*subnetDetails, error) {
params := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcID),
},
},
},
}
resp, err := svc.DescribeSubnets(params)
if err != nil {
return nil, err
}
log.Debug("aws.getRouteTables")
rt, err := getRouteTables(svc, vpcID)
if err != nil {
return nil, err
}
retAll := make([]*subnetDetails, len(resp.Subnets))
retFiltered := make([]*subnetDetails, 0)
for i, sn := range resp.Subnets {
az := aws.StringValue(sn.AvailabilityZone)
subnetID := aws.StringValue(sn.SubnetId)
isPublic, err := isSubnetPublic(rt, subnetID)
if err != nil {
return nil, err
}
tags := convertEc2Tags(sn.Tags)
retAll[i] = &subnetDetails{
id: subnetID,
availabilityZone: az,
public: isPublic,
tags: tags,
}
if _, ok := tags[clusterIDTagPrefix+clusterID]; ok {
retFiltered = append(retFiltered, &subnetDetails{
id: subnetID,
availabilityZone: az,
public: isPublic,
tags: tags,
})
}
}
// Fall back to full list of subnets if none matching expected tagging are found, with a stern warning
// https://github.com/kubernetes/kubernetes/blob/v1.10.3/pkg/cloudprovider/providers/aws/aws.go#L3009
if len(retFiltered) == 0 {
log.Warn("No tagged subnets found; considering all subnets. This is likely to be an error in future versions.")
return retAll, nil
}
return retFiltered, nil
}
func convertEc2Tags(instanceTags []*ec2.Tag) map[string]string {
tags := make(map[string]string, len(instanceTags))
for _, tagDescription := range instanceTags {
tags[aws.StringValue(tagDescription.Key)] = aws.StringValue(tagDescription.Value)
}
return tags
}
func getRouteTables(svc ec2iface.EC2API, vpcID string) ([]*ec2.RouteTable, error) {
params := &ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcID),
},
},
},
}
resp, err := svc.DescribeRouteTables(params)
if err != nil {
return nil, err
}
return resp.RouteTables, nil
}
// Copied from Kubernete's https://github.com/kubernetes/kubernetes/blob/master/pkg/cloudprovider/providers/aws/aws.go
func isSubnetPublic(rt []*ec2.RouteTable, subnetID string) (bool, error) {
var subnetTable *ec2.RouteTable
for _, table := range rt {
for _, assoc := range table.Associations {
if aws.StringValue(assoc.SubnetId) == subnetID {
subnetTable = table
break
}
}
}
if subnetTable == nil {
// If there is no explicit association, the subnet will be implicitly
// associated with the VPC's main routing table.
for _, table := range rt {
for _, assoc := range table.Associations {
if aws.BoolValue(assoc.Main) {
subnetTable = table
break
}
}
}
}
if subnetTable == nil {
return false, fmt.Errorf("could not locate routing table for subnet %q", subnetID)
}
for _, route := range subnetTable.Routes {
// There is no direct way in the AWS API to determine if a subnet is public or private.
// A public subnet is one which has an internet gateway route
// we look for the gatewayId and make sure it has the prefix of igw to differentiate
// from the default in-subnet route which is called "local"
// or other virtual gateway (starting with vgv)
// or vpc peering connections (starting with pcx).
if strings.HasPrefix(aws.StringValue(route.GatewayId), "igw") {
return true, nil
}
}
return false, nil
}
func findSecurityGroupWithClusterID(svc ec2iface.EC2API, clusterID string, controllerID string) (*securityGroupDetails, error) {
params := &ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:" + clusterIDTagPrefix + clusterID),
Values: []*string{
aws.String(resourceLifecycleOwned),
},
},
{
Name: aws.String("tag:" + kubernetesCreatorTag),
Values: []*string{
aws.String(controllerID),
},
},
},
}
resp, err := svc.DescribeSecurityGroups(params)
if err != nil {
return nil, err
}
if len(resp.SecurityGroups) < 1 {
return nil, fmt.Errorf("could not find security group that matches: %s", params.Filters)
}
sg := resp.SecurityGroups[0]
return &securityGroupDetails{
name: aws.StringValue(sg.GroupName),
id: aws.StringValue(sg.GroupId),
}, nil
}