/
routetable.go
271 lines (236 loc) · 9.87 KB
/
routetable.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
package ec2
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/smithy-go"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/crossplane-contrib/provider-aws/apis/ec2/v1beta1"
"github.com/crossplane-contrib/provider-aws/pkg/utils/jsonpatch"
"github.com/crossplane-contrib/provider-aws/pkg/utils/pointer"
)
const (
// DefaultLocalGatewayID is the id for local gateway
DefaultLocalGatewayID = "local"
// RouteTableIDNotFound is the code that is returned by ec2 when the given SubnetID is invalid
RouteTableIDNotFound = "InvalidRouteTableID.NotFound"
// RouteNotFound is the code that is returned when the given route is not found
RouteNotFound = "InvalidRoute.NotFound"
// AssociationIDNotFound is the code that is returned when then given AssociationID is invalid
AssociationIDNotFound = "InvalidAssociationID.NotFound"
)
// RouteTableClient is the external client used for RouteTable Custom Resource
type RouteTableClient interface {
CreateRouteTable(ctx context.Context, input *ec2.CreateRouteTableInput, opts ...func(*ec2.Options)) (*ec2.CreateRouteTableOutput, error)
DeleteRouteTable(ctx context.Context, input *ec2.DeleteRouteTableInput, opts ...func(*ec2.Options)) (*ec2.DeleteRouteTableOutput, error)
DescribeRouteTables(ctx context.Context, input *ec2.DescribeRouteTablesInput, opts ...func(*ec2.Options)) (*ec2.DescribeRouteTablesOutput, error)
CreateRoute(ctx context.Context, input *ec2.CreateRouteInput, opts ...func(*ec2.Options)) (*ec2.CreateRouteOutput, error)
DeleteRoute(ctx context.Context, input *ec2.DeleteRouteInput, opts ...func(*ec2.Options)) (*ec2.DeleteRouteOutput, error)
AssociateRouteTable(ctx context.Context, input *ec2.AssociateRouteTableInput, opts ...func(*ec2.Options)) (*ec2.AssociateRouteTableOutput, error)
DisassociateRouteTable(ctx context.Context, input *ec2.DisassociateRouteTableInput, opts ...func(*ec2.Options)) (*ec2.DisassociateRouteTableOutput, error)
CreateTags(ctx context.Context, input *ec2.CreateTagsInput, opts ...func(*ec2.Options)) (*ec2.CreateTagsOutput, error)
DeleteTags(ctx context.Context, input *ec2.DeleteTagsInput, opts ...func(*ec2.Options)) (*ec2.DeleteTagsOutput, error)
}
// NewRouteTableClient returns a new client using AWS credentials as JSON encoded data.
func NewRouteTableClient(cfg aws.Config) RouteTableClient {
return ec2.NewFromConfig(cfg)
}
// IsRouteTableNotFoundErr returns true if the error is because the route table doesn't exist
func IsRouteTableNotFoundErr(err error) bool {
var awsErr smithy.APIError
return errors.As(err, &awsErr) && awsErr.ErrorCode() == RouteTableIDNotFound
}
// IsRouteNotFoundErr returns true if the error is because the route doesn't exist
func IsRouteNotFoundErr(err error) bool {
var awsErr smithy.APIError
return errors.As(err, &awsErr) && awsErr.ErrorCode() == RouteNotFound
}
// IsAssociationIDNotFoundErr returns true if the error is because the association doesn't exist
func IsAssociationIDNotFoundErr(err error) bool {
var awsErr smithy.APIError
return errors.As(err, &awsErr) && awsErr.ErrorCode() == AssociationIDNotFound
}
// GenerateRTObservation is used to produce v1beta1.RouteTableExternalStatus from
// ec2.RouteTable.
func GenerateRTObservation(rt ec2types.RouteTable) v1beta1.RouteTableObservation {
o := v1beta1.RouteTableObservation{
OwnerID: aws.ToString(rt.OwnerId),
RouteTableID: aws.ToString(rt.RouteTableId),
}
if len(rt.Routes) > 0 {
o.Routes = make([]v1beta1.RouteState, len(rt.Routes))
for i, rt := range rt.Routes {
o.Routes[i] = v1beta1.RouteState{
State: string(rt.State),
DestinationCIDRBlock: aws.ToString(rt.DestinationCidrBlock),
DestinationIPV6CIDRBlock: aws.ToString(rt.DestinationIpv6CidrBlock),
GatewayID: aws.ToString(rt.GatewayId),
InstanceID: aws.ToString(rt.InstanceId),
LocalGatewayID: aws.ToString(rt.LocalGatewayId),
NatGatewayID: aws.ToString(rt.NatGatewayId),
NetworkInterfaceID: aws.ToString(rt.NetworkInterfaceId),
TransitGatewayID: aws.ToString(rt.TransitGatewayId),
VpcPeeringConnectionID: aws.ToString(rt.VpcPeeringConnectionId),
}
}
}
if len(rt.Associations) > 0 {
o.Associations = make([]v1beta1.AssociationState, len(rt.Associations))
for i, asc := range rt.Associations {
o.Associations[i] = v1beta1.AssociationState{
Main: asc.Main,
AssociationID: aws.ToString(asc.RouteTableAssociationId),
State: string(asc.AssociationState.State),
SubnetID: aws.ToString(asc.SubnetId),
}
}
}
return o
}
// LateInitializeRT fills the empty fields in *v1beta1.RouteTableParameters with
// the values seen in ec2.RouteTable.
func LateInitializeRT(in *v1beta1.RouteTableParameters, rt *ec2types.RouteTable) { //nolint:gocyclo
if rt == nil {
return
}
in.VPCID = pointer.LateInitialize(in.VPCID, rt.VpcId)
if !pointer.BoolValue(in.IgnoreRoutes) {
if len(in.Routes) == 0 && len(rt.Routes) != 0 {
in.Routes = make([]v1beta1.RouteBeta, len(rt.Routes))
for i, val := range rt.Routes {
in.Routes[i] = v1beta1.RouteBeta{
DestinationCIDRBlock: val.DestinationCidrBlock,
GatewayID: val.GatewayId,
InstanceID: val.InstanceId,
LocalGatewayID: val.LocalGatewayId,
NatGatewayID: val.NatGatewayId,
NetworkInterfaceID: val.NetworkInterfaceId,
TransitGatewayID: val.TransitGatewayId,
VpcPeeringConnectionID: val.VpcPeeringConnectionId,
}
}
}
}
if len(in.Associations) == 0 && len(rt.Associations) != 0 {
in.Associations = make([]v1beta1.Association, len(rt.Associations))
for i, val := range rt.Associations {
in.Associations[i] = v1beta1.Association{
SubnetID: val.SubnetId,
}
}
}
if len(in.Tags) == 0 && len(rt.Tags) != 0 {
in.Tags = BuildFromEC2TagsV1Beta1(rt.Tags)
}
}
// CreateRTPatch creates a *v1beta1.RouteTableParameters that has only the changed
// values between the target *v1beta1.RouteTableParameters and the current
// *ec2.RouteTable
func CreateRTPatch(in ec2types.RouteTable, target v1beta1.RouteTableParameters) (*v1beta1.RouteTableParameters, error) {
targetCopy := target.DeepCopy()
currentParams := &v1beta1.RouteTableParameters{}
SortTagsV1Beta1(target.Tags, in.Tags)
if !pointer.BoolValue(target.IgnoreRoutes) {
// Add the default route for fair comparison.
for _, val := range in.Routes {
if val.GatewayId != nil && *val.GatewayId == DefaultLocalGatewayID {
targetCopy.Routes = append([]v1beta1.RouteBeta{{
GatewayID: val.GatewayId,
DestinationCIDRBlock: val.DestinationCidrBlock,
}}, target.Routes...)
}
}
SortRoutes(targetCopy.Routes, in.Routes)
}
LateInitializeRT(currentParams, &in)
if !pointer.BoolValue(target.IgnoreRoutes) {
for i := range targetCopy.Routes {
targetCopy.Routes[i].ClearRefSelectors()
}
}
for i := range target.Associations {
targetCopy.Associations[i].ClearRefSelectors()
}
jsonPatch, err := jsonpatch.CreateJSONPatch(*currentParams, targetCopy)
if err != nil {
return nil, err
}
patch := &v1beta1.RouteTableParameters{}
if err := json.Unmarshal(jsonPatch, patch); err != nil {
return nil, err
}
return patch, nil
}
// IsRtUpToDate checks whether there is a change in any of the modifiable fields.
func IsRtUpToDate(p v1beta1.RouteTableParameters, rt ec2types.RouteTable) (bool, error) {
patch, err := CreateRTPatch(rt, p)
if err != nil {
return false, err
}
return cmp.Equal(&v1beta1.RouteTableParameters{}, patch,
cmpopts.EquateEmpty(),
cmpopts.IgnoreTypes(&xpv1.Reference{}, &xpv1.Selector{}),
cmpopts.IgnoreFields(v1beta1.RouteTableParameters{}, "IgnoreRoutes"),
cmpopts.IgnoreFields(v1beta1.RouteTableParameters{}, "Region"),
), nil
}
// SortRoutes sorts array of Routes on DestinationCIDR
func SortRoutes(route []v1beta1.RouteBeta, ec2Route []ec2types.Route) {
sort.Slice(route, func(i, j int) bool {
return compareRoutes(i, j, route)
})
sort.Slice(ec2Route, func(i, j int) bool {
return compareEC2Routes(i, j, ec2Route)
})
}
func compareRoutes(i, j int, route []v1beta1.RouteBeta) bool {
if route[i].DestinationCIDRBlock != nil && route[j].DestinationCIDRBlock != nil {
return *route[i].DestinationCIDRBlock < *route[j].DestinationCIDRBlock
}
if route[i].DestinationIPV6CIDRBlock != nil && route[j].DestinationIPV6CIDRBlock != nil {
return *route[i].DestinationIPV6CIDRBlock < *route[j].DestinationIPV6CIDRBlock
}
if route[i].DestinationCIDRBlock != nil && route[j].DestinationIPV6CIDRBlock != nil {
return true
}
if route[i].DestinationIPV6CIDRBlock != nil && route[j].DestinationCIDRBlock != nil {
return false
}
return false
}
func compareEC2Routes(i, j int, ec2Route []ec2types.Route) bool {
if ec2Route[i].DestinationCidrBlock != nil && ec2Route[j].DestinationCidrBlock != nil {
return *ec2Route[i].DestinationCidrBlock < *ec2Route[j].DestinationCidrBlock
}
if ec2Route[i].DestinationIpv6CidrBlock != nil && ec2Route[j].DestinationIpv6CidrBlock != nil {
return *ec2Route[i].DestinationIpv6CidrBlock < *ec2Route[j].DestinationIpv6CidrBlock
}
if ec2Route[i].DestinationCidrBlock != nil && ec2Route[j].DestinationIpv6CidrBlock != nil {
return true
}
if ec2Route[i].DestinationIpv6CidrBlock != nil && ec2Route[j].DestinationCidrBlock != nil {
return false
}
return false
}
// ValidateRoutes on empty cidrs
func ValidateRoutes(route []v1beta1.RouteBeta) error {
errs := make([]string, 0, len(route))
for i, r := range route {
if r.DestinationCIDRBlock == nil && r.DestinationIPV6CIDRBlock == nil {
errs = append(errs, fmt.Sprintf("route[%d]: both v4 and v6 cidrs are empty", i))
}
}
if len(errs) > 0 {
return fmt.Errorf("invalid routes: %s", strings.Join(errs, ";"))
}
return nil
}