This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
198 lines (166 loc) · 4.74 KB
/
route.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
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=Route
type Route struct {
Name *string
RouteTable *RouteTable
InternetGateway *InternetGateway
Instance *Instance
CIDR *string
}
func (e *Route) Find(c *fi.Context) (*Route, error) {
cloud := c.Cloud.(*awsup.AWSCloud)
if e.RouteTable == nil || e.CIDR == nil {
// TODO: Move to validate?
return nil, nil
}
if e.RouteTable.ID == nil {
return nil, nil
}
request := &ec2.DescribeRouteTablesInput{
RouteTableIds: []*string{e.RouteTable.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
} else {
if len(response.RouteTables) != 1 {
glog.Fatalf("found multiple RouteTables matching tags")
}
rt := response.RouteTables[0]
for _, r := range rt.Routes {
if aws.StringValue(r.DestinationCidrBlock) != *e.CIDR {
continue
}
actual := &Route{
Name: e.Name,
RouteTable: &RouteTable{ID: rt.RouteTableId},
CIDR: r.DestinationCidrBlock,
}
if r.GatewayId != nil {
actual.InternetGateway = &InternetGateway{ID: r.GatewayId}
}
if r.InstanceId != nil {
actual.Instance = &Instance{ID: r.InstanceId}
}
if aws.StringValue(r.State) == "blackhole" {
glog.V(2).Infof("found route is a blackhole route")
// These should be nil anyway, but just in case...
actual.Instance = nil
actual.InternetGateway = nil
}
glog.V(2).Infof("found route matching cidr %s", *e.CIDR)
return actual, nil
}
}
return nil, nil
}
func (e *Route) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *Route) CheckChanges(a, e, changes *Route) error {
if a == nil {
// TODO: Create validate method?
if e.RouteTable == nil {
return fi.RequiredField("RouteTable")
}
if e.CIDR == nil {
return fi.RequiredField("CIDR")
}
targetCount := 0
if e.InternetGateway != nil {
targetCount++
}
if e.Instance != nil {
targetCount++
}
if targetCount == 0 {
return fmt.Errorf("InternetGateway or Instance is required")
}
if targetCount != 1 {
return fmt.Errorf("Cannot set both InternetGateway and Instance")
}
}
if a != nil {
if changes.RouteTable != nil {
return fi.CannotChangeField("RouteTable")
}
if changes.CIDR != nil {
return fi.CannotChangeField("CIDR")
}
}
return nil
}
func (_ *Route) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Route) error {
if a == nil {
request := &ec2.CreateRouteInput{}
request.RouteTableId = checkNotNil(e.RouteTable.ID)
request.DestinationCidrBlock = checkNotNil(e.CIDR)
if e.InternetGateway != nil {
request.GatewayId = checkNotNil(e.InternetGateway.ID)
}
if e.Instance != nil {
request.InstanceId = checkNotNil(e.Instance.ID)
}
glog.V(2).Infof("Creating Route with RouteTable:%q CIDR:%q", *e.RouteTable.ID, *e.CIDR)
response, err := t.Cloud.EC2.CreateRoute(request)
if err != nil {
return fmt.Errorf("error creating Route: %v", err)
}
if !aws.BoolValue(response.Return) {
return fmt.Errorf("create Route request failed: %v", response)
}
} else {
request := &ec2.ReplaceRouteInput{}
request.RouteTableId = checkNotNil(e.RouteTable.ID)
request.DestinationCidrBlock = checkNotNil(e.CIDR)
if e.InternetGateway != nil {
request.GatewayId = checkNotNil(e.InternetGateway.ID)
}
if e.Instance != nil {
request.InstanceId = checkNotNil(e.Instance.ID)
}
glog.V(2).Infof("Updating Route with RouteTable:%q CIDR:%q", *e.RouteTable.ID, *e.CIDR)
_, err := t.Cloud.EC2.ReplaceRoute(request)
if err != nil {
return fmt.Errorf("error updating Route: %v", err)
}
}
return nil
}
func checkNotNil(s *string) *string {
if s == nil {
glog.Fatal("string pointer was unexpectedly nil")
}
return s
}
type terraformRoute struct {
RouteTableID *terraform.Literal `json:"route_table_id"`
CIDR *string `json:"destination_cidr_block,omitempty"`
InternetGatewayID *terraform.Literal `json:"gateway_id,omitempty"`
InstanceID *terraform.Literal `json:"instance_id,omitempty"`
}
func (_ *Route) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Route) error {
tf := &terraformRoute{
CIDR: e.CIDR,
RouteTableID: e.RouteTable.TerraformLink(),
}
if e.InternetGateway != nil {
tf.InternetGatewayID = e.InternetGateway.TerraformLink()
}
if e.Instance != nil {
tf.InstanceID = e.Instance.TerraformLink()
}
return t.RenderResource("aws_route", *e.Name, tf)
}