forked from zalando-incubator/kube-ingress-aws-controller
/
ec2.go
259 lines (227 loc) · 6.04 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
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"strings"
)
type instanceDetails struct {
id string
vpcID string
tags map[string]string
}
func (id *instanceDetails) name() string {
if n, err := getNameTag(id.tags); err == nil {
return n
}
return "unknowninstance"
}
func (id *instanceDetails) clusterID() string {
if clusterID, err := getTag(id.tags, clusterIDTag); err == nil {
return clusterID
}
return "unknown-cluster"
}
type subnetDetails struct {
id string
availabilityZone string
tags map[string]string
public bool
}
type securityGroupDetails struct {
name string
id string
}
func (sd *subnetDetails) String() string {
return fmt.Sprintf("%s (%s) @ %s", sd.Name(), sd.id, sd.availabilityZone)
}
func (sd *subnetDetails) Name() string {
if n, err := getNameTag(sd.tags); err == nil {
return n
}
return "unknown-subnet"
}
const (
autoScalingGroupNameTag = "aws:autoscaling:groupName"
runningState = 16 // See github.com/aws/aws-sdk-go/service/ec2/api.go, type InstanceState
)
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 {
return nil, err
}
if len(resp.Reservations) < 1 || len(resp.Reservations[0].Instances) < 1 {
return nil, fmt.Errorf("unable to get details for instance %q", instanceID)
}
i := resp.Reservations[0].Instances[0]
if aws.Int64Value(i.State.Code) != runningState {
return nil, fmt.Errorf("instance is in an invalid state: %s", aws.StringValue(i.State.Name))
}
return &instanceDetails{
id: aws.StringValue(i.InstanceId),
vpcID: aws.StringValue(i.VpcId),
tags: convertEc2Tags(i.Tags),
}, nil
}
func getSubnets(svc ec2iface.EC2API, vpcID 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
}
rt, err := getRouteTables(svc, vpcID)
if err != nil {
return nil, err
}
ret := make([]*subnetDetails, len(resp.Subnets))
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
}
ret[i] = &subnetDetails{
id: subnetID,
availabilityZone: az,
public: isPublic,
tags: convertEc2Tags(sn.Tags),
}
}
return ret, nil
}
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) == true {
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 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 findSecurityGroupWithNameTag(svc ec2iface.EC2API, nameTag string) (*securityGroupDetails, error) {
params := &ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag-key"),
Values: []*string{
aws.String("Name"),
},
},
{
Name: aws.String("tag-value"),
Values: []*string{
aws.String(nameTag),
},
},
{
Name: aws.String("tag-key"),
Values: []*string{
aws.String("aws:cloudformation:logical-id"),
},
},
{
Name: aws.String("tag-value"),
Values: []*string{
aws.String("IngressLoadBalancerSecurityGroup"),
},
},
},
}
resp, err := svc.DescribeSecurityGroups(params)
if err != nil {
return nil, err
}
if len(resp.SecurityGroups) < 1 {
return nil, ErrMissingSecurityGroup
}
sg := resp.SecurityGroups[0]
return &securityGroupDetails{
name: aws.StringValue(sg.GroupName),
id: aws.StringValue(sg.GroupId),
}, nil
}