-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathelastic_ip.go
343 lines (286 loc) · 9.69 KB
/
elastic_ip.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
/*
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/cloudup/terraformWriter"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
// ElasticIP manages an AWS Address (ElasticIP)
// +kops:fitask
type ElasticIP struct {
Name *string
Lifecycle fi.Lifecycle
ID *string
PublicIP *string
// Shared is set if this is a shared IP
Shared *bool
// ElasticIPs don't support tags. We instead find it via a related resource.
// TagOnSubnet tags a subnet with the ElasticIP. Deprecated: doesn't round-trip with terraform.
TagOnSubnet *Subnet
Tags map[string]string
// AssociatedNatGatewayRouteTable follows the RouteTable -> NatGateway -> ElasticIP
AssociatedNatGatewayRouteTable *RouteTable
}
var _ fi.CompareWithID = &ElasticIP{}
func (e *ElasticIP) CompareWithID() *string {
return e.ID
}
// Find returns the actual ElasticIP state, or nil if not found
func (e *ElasticIP) Find(context *fi.Context) (*ElasticIP, error) {
return e.find(context.Cloud.(awsup.AWSCloud))
}
// find will attempt to look up the elastic IP from AWS
func (e *ElasticIP) find(cloud awsup.AWSCloud) (*ElasticIP, error) {
publicIP := e.PublicIP
allocationID := e.ID
// Find via RouteTable -> NatGateway -> ElasticIP
if allocationID == nil && publicIP == nil && e.AssociatedNatGatewayRouteTable != nil {
ngw, err := findNatGatewayFromRouteTable(cloud, e.AssociatedNatGatewayRouteTable)
if err != nil {
return nil, fmt.Errorf("error finding AssociatedNatGatewayRouteTable: %v", err)
}
if ngw == nil {
klog.V(2).Infof("AssociatedNatGatewayRouteTable not found")
} else {
if len(ngw.NatGatewayAddresses) == 0 {
return nil, fmt.Errorf("NatGateway %q has no addresses", *ngw.NatGatewayId)
}
if len(ngw.NatGatewayAddresses) > 1 {
return nil, fmt.Errorf("NatGateway %q has multiple addresses", *ngw.NatGatewayId)
}
allocationID = ngw.NatGatewayAddresses[0].AllocationId
if allocationID == nil {
return nil, fmt.Errorf("NatGateway %q has nil addresses", *ngw.NatGatewayId)
} else {
klog.V(2).Infof("Found ElasticIP AllocationID %q via NatGateway", *allocationID)
}
}
}
// Find via tag on subnet
// TODO: Deprecated, because doesn't round-trip with terraform
if allocationID == nil && publicIP == nil && e.TagOnSubnet != nil && e.TagOnSubnet.ID != nil {
var filters []*ec2.Filter
filters = append(filters, awsup.NewEC2Filter("key", "AssociatedElasticIp"))
filters = append(filters, awsup.NewEC2Filter("resource-id", *e.TagOnSubnet.ID))
request := &ec2.DescribeTagsInput{
Filters: filters,
}
response, err := cloud.EC2().DescribeTags(request)
if err != nil {
return nil, fmt.Errorf("error listing tags: %v", err)
}
if response == nil || len(response.Tags) == 0 {
return nil, nil
}
if len(response.Tags) != 1 {
return nil, fmt.Errorf("found multiple tags for: %v", e)
}
t := response.Tags[0]
publicIP = t.Value
klog.V(2).Infof("Found public IP via tag: %v", *publicIP)
}
if publicIP != nil || allocationID != nil {
request := &ec2.DescribeAddressesInput{}
if allocationID != nil {
request.AllocationIds = []*string{allocationID}
} else if publicIP != nil {
request.Filters = []*ec2.Filter{awsup.NewEC2Filter("public-ip", *publicIP)}
}
response, err := cloud.EC2().DescribeAddresses(request)
if err != nil {
return nil, fmt.Errorf("error listing ElasticIPs: %v", err)
}
if response == nil || len(response.Addresses) == 0 {
return nil, fmt.Errorf("found no ElasticIPs for: %v", e)
}
if len(response.Addresses) != 1 {
return nil, fmt.Errorf("found multiple ElasticIPs for: %v", e)
}
a := response.Addresses[0]
actual := &ElasticIP{
ID: a.AllocationId,
PublicIP: a.PublicIp,
}
actual.TagOnSubnet = e.TagOnSubnet
actual.AssociatedNatGatewayRouteTable = e.AssociatedNatGatewayRouteTable
{
tags, err := cloud.EC2().DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: aws.StringSlice([]string{*a.AllocationId}),
},
},
})
if err != nil {
return nil, fmt.Errorf("error querying tags for ElasticIP: %v", err)
}
var ec2Tags []*ec2.Tag
for _, t := range tags.Tags {
ec2Tags = append(ec2Tags, &ec2.Tag{
Key: t.Key,
Value: t.Value,
})
}
actual.Tags = intersectTags(ec2Tags, e.Tags)
}
// ElasticIP don't have a Name (no tags), so we set the name to avoid spurious changes
actual.Name = e.Name
e.ID = actual.ID
// Avoid spurious changes
actual.Lifecycle = e.Lifecycle
actual.Shared = e.Shared
return actual, nil
}
return nil, nil
}
// Run is called to execute this task.
// This is the main entry point of the task, and will actually
// connect our internal resource representation to an actual
// resource in AWS
func (e *ElasticIP) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
// CheckChanges validates the resource. EIPs are simple, so virtually no
// validation
func (_ *ElasticIP) CheckChanges(a, e, changes *ElasticIP) error {
// This is a new EIP
if a == nil {
// No logic for EIPs - they are just created
return nil
}
// This is an existing EIP
// We should never be changing this
if a != nil {
if changes.PublicIP != nil {
return fi.CannotChangeField("PublicIP")
}
if changes.TagOnSubnet != nil {
return fi.CannotChangeField("TagOnSubnet")
}
if changes.ID != nil {
return fi.CannotChangeField("ID")
}
}
return nil
}
// RenderAWS is where we actually apply changes to AWS
func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) error {
var publicIp *string
var eipId *string
// If this is a new ElasticIP
if a == nil {
klog.V(2).Infof("Creating ElasticIP for VPC")
request := &ec2.AllocateAddressInput{
TagSpecifications: awsup.EC2TagSpecification(ec2.ResourceTypeElasticIp, e.Tags),
}
request.Domain = aws.String(ec2.DomainTypeVpc)
response, err := t.Cloud.EC2().AllocateAddress(request)
if err != nil {
return fmt.Errorf("error creating ElasticIP: %v", err)
}
e.ID = response.AllocationId
e.PublicIP = response.PublicIp
publicIp = e.PublicIP
eipId = response.AllocationId
} else {
publicIp = a.PublicIP
eipId = a.ID
if err := t.AddAWSTags(*e.ID, e.Tags); err != nil {
return err
}
}
// Tag the associated subnet
if e.TagOnSubnet != nil {
if e.TagOnSubnet.ID == nil {
return fmt.Errorf("Subnet ID not set")
}
tags := make(map[string]string)
tags["AssociatedElasticIp"] = *publicIp
tags["AssociatedElasticIpAllocationId"] = *eipId // Leaving this in for reference, even though we don't use it
err := t.AddAWSTags(*e.TagOnSubnet.ID, tags)
if err != nil {
return fmt.Errorf("Unable to tag subnet %v", err)
}
} else {
// TODO: Figure out what we can do. We're sort of stuck between wanting to have one code-path with
// terraform, and having a bigger "window of loss" here before we create the NATGateway
klog.V(2).Infof("ElasticIP %q not tagged on subnet; risk of leaking", fi.StringValue(publicIp))
}
return nil
}
type terraformElasticIP struct {
VPC *bool `json:"vpc" cty:"vpc"`
Tags map[string]string `json:"tags,omitempty" cty:"tags"`
}
func (_ *ElasticIP) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *ElasticIP) error {
if fi.BoolValue(e.Shared) {
if e.ID == nil {
return fmt.Errorf("ID must be set, if ElasticIP is shared: %v", e)
}
klog.V(4).Infof("reusing existing ElasticIP with id %q", aws.StringValue(e.ID))
return nil
}
tf := &terraformElasticIP{
VPC: aws.Bool(true),
Tags: e.Tags,
}
return t.RenderResource("aws_eip", *e.Name, tf)
}
func (e *ElasticIP) TerraformLink() *terraformWriter.Literal {
if fi.BoolValue(e.Shared) {
if e.ID == nil {
klog.Fatalf("ID must be set, if ElasticIP is shared: %v", e)
}
return terraformWriter.LiteralFromStringValue(*e.ID)
}
return terraformWriter.LiteralProperty("aws_eip", *e.Name, "id")
}
type cloudformationElasticIP struct {
Domain *string `json:"Domain"`
Tags []cloudformationTag `json:"Tags,omitempty"`
}
func (_ *ElasticIP) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *ElasticIP) error {
if fi.BoolValue(e.Shared) {
if e.ID == nil {
return fmt.Errorf("ID must be set, if ElasticIP is shared: %v", e)
}
klog.V(4).Infof("reusing existing ElasticIP with id %q", aws.StringValue(e.ID))
return nil
}
tf := &cloudformationElasticIP{
Domain: aws.String("vpc"),
Tags: buildCloudformationTags(e.Tags),
}
return t.RenderResource("AWS::EC2::EIP", *e.Name, tf)
}
// Removed because you normally want CloudformationAllocationID
//func (e *ElasticIP) CloudformationLink() *cloudformation.Literal {
// return cloudformation.Ref("AWS::EC2::EIP", *e.Name)
//}
func (e *ElasticIP) CloudformationAllocationID() *cloudformation.Literal {
if fi.BoolValue(e.Shared) {
if e.ID == nil {
klog.Fatalf("ID must be set, if ElasticIP is shared: %v", e)
}
return cloudformation.LiteralString(*e.ID)
}
return cloudformation.GetAtt("AWS::EC2::EIP", *e.Name, "AllocationId")
}