forked from kubernetes/kops
/
routetableassociation.go
180 lines (152 loc) · 4.79 KB
/
routetableassociation.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
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"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
//go:generate fitask -type=RouteTableAssociation
type RouteTableAssociation struct {
Name *string
ID *string
RouteTable *RouteTable
Subnet *Subnet
}
func (s *RouteTableAssociation) CompareWithID() *string {
return s.ID
}
func (e *RouteTableAssociation) Find(c *fi.Context) (*RouteTableAssociation, error) {
cloud := c.Cloud.(awsup.AWSCloud)
routeTableID := e.RouteTable.ID
subnetID := e.Subnet.ID
if routeTableID == nil || subnetID == nil {
return nil, nil
}
request := &ec2.DescribeRouteTablesInput{
RouteTableIds: []*string{routeTableID},
}
response, err := cloud.EC2().DescribeRouteTables(request)
if err != nil {
return nil, fmt.Errorf("error listing RouteTables: %v", err)
}
if response == nil || len(response.RouteTables) == 0 {
return nil, nil
}
if len(response.RouteTables) != 1 {
return nil, fmt.Errorf("found multiple RouteTables matching tags")
}
rt := response.RouteTables[0]
for _, rta := range rt.Associations {
if aws.StringValue(rta.SubnetId) != *subnetID {
continue
}
actual := &RouteTableAssociation{
Name: e.Name,
ID: rta.RouteTableAssociationId,
RouteTable: &RouteTable{ID: rta.RouteTableId},
Subnet: &Subnet{ID: rta.SubnetId},
}
glog.V(2).Infof("found matching RouteTableAssociation %q", *actual.ID)
e.ID = actual.ID
return actual, nil
}
return nil, nil
}
func (e *RouteTableAssociation) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *RouteTableAssociation) CheckChanges(a, e, changes *RouteTableAssociation) error {
if a != nil {
if e.RouteTable == nil {
return fi.RequiredField("RouteTable")
}
if e.Subnet == nil {
return fi.RequiredField("Subnet")
}
}
if a != nil {
if changes.RouteTable != nil {
return fi.CannotChangeField("RouteTable")
}
if changes.Subnet != nil {
return fi.CannotChangeField("Subnet")
}
}
return nil
}
func findExistingRouteTableForSubnet(cloud awsup.AWSCloud, subnet *Subnet) (*ec2.RouteTable, error) {
if subnet == nil {
return nil, fmt.Errorf("subnet not set")
}
if subnet.ID == nil {
return nil, fmt.Errorf("subnet ID not set")
}
request := &ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{awsup.NewEC2Filter("association.subnet-id", fi.StringValue(subnet.ID))},
}
response, err := cloud.EC2().DescribeRouteTables(request)
if err != nil {
return nil, fmt.Errorf("error listing RouteTables: %v", err)
}
if response == nil || len(response.RouteTables) == 0 {
return nil, nil
}
if len(response.RouteTables) != 1 {
return nil, fmt.Errorf("found multiple RouteTables attached to subnet")
}
rt := response.RouteTables[0]
return rt, nil
}
func (_ *RouteTableAssociation) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *RouteTableAssociation) error {
if a == nil {
// TODO: We might do better just to make the subnet the primary key here
glog.V(2).Infof("Checking for existing RouteTableAssociation to subnet")
existing, err := findExistingRouteTableForSubnet(t.Cloud, e.Subnet)
if err != nil {
return fmt.Errorf("error checking for existing RouteTableAssociation: %v", err)
}
if existing != nil {
for _, a := range existing.Associations {
if aws.StringValue(a.SubnetId) != aws.StringValue(e.Subnet.ID) {
continue
}
glog.V(2).Infof("Creating RouteTableAssociation")
request := &ec2.DisassociateRouteTableInput{
AssociationId: a.RouteTableAssociationId,
}
_, err := t.Cloud.EC2().DisassociateRouteTable(request)
if err != nil {
return fmt.Errorf("error disassociating existing RouteTable from subnet: %v", err)
}
}
}
glog.V(2).Infof("Creating RouteTableAssociation")
request := &ec2.AssociateRouteTableInput{
SubnetId: e.Subnet.ID,
RouteTableId: e.RouteTable.ID,
}
response, err := t.Cloud.EC2().AssociateRouteTable(request)
if err != nil {
return fmt.Errorf("error creating RouteTableAssociation: %v", err)
}
e.ID = response.AssociationId
}
return nil // no tags
}
type terraformRouteTableAssociation struct {
SubnetID *terraform.Literal `json:"subnet_id"`
RouteTableID *terraform.Literal `json:"route_table_id"`
}
func (_ *RouteTableAssociation) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *RouteTableAssociation) error {
tf := &terraformRouteTableAssociation{
SubnetID: e.Subnet.TerraformLink(),
RouteTableID: e.RouteTable.TerraformLink(),
}
return t.RenderResource("aws_route_table_association", *e.Name, tf)
}
func (e *RouteTableAssociation) TerraformLink() *terraform.Literal {
return terraform.LiteralSelfLink("aws_route_table_association", *e.Name)
}