-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
vpc_ipv4.go
448 lines (379 loc) · 14.5 KB
/
vpc_ipv4.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package builder
import (
"context"
"fmt"
"strings"
"github.com/weaveworks/eksctl/pkg/awsapi"
gfncfn "github.com/weaveworks/goformation/v4/cloudformation/cloudformation"
gfnec2 "github.com/weaveworks/goformation/v4/cloudformation/ec2"
gfnt "github.com/weaveworks/goformation/v4/cloudformation/types"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
"github.com/weaveworks/eksctl/pkg/vpc"
)
const (
cfnControlPlaneSGResource = "ControlPlaneSecurityGroup"
cfnSharedNodeSGResource = "ClusterSharedNodeSecurityGroup"
cfnIngressClusterToNodeSGResource = "IngressDefaultClusterToNodeSG"
cfnVPCResource = "VPC"
)
// A IPv4VPCResourceSet builds the resources required for the specified VPC
type IPv4VPCResourceSet struct {
rs *resourceSet
clusterConfig *api.ClusterConfig
ec2API awsapi.EC2
vpcID *gfnt.Value
subnetDetails *SubnetDetails
extendForOutposts bool
}
type SubnetResource struct {
Subnet *gfnt.Value
RouteTable *gfnt.Value
AvailabilityZone string
onOutpost bool
}
type SubnetDetails struct {
Private []SubnetResource
Public []SubnetResource
PrivateLocalZone []SubnetResource
PublicLocalZone []SubnetResource
controlPlaneOnOutposts bool
}
// NewIPv4VPCResourceSet creates and returns a new VPCResourceSet
func NewIPv4VPCResourceSet(rs *resourceSet, clusterConfig *api.ClusterConfig, ec2API awsapi.EC2, extendForOutposts bool) *IPv4VPCResourceSet {
return &IPv4VPCResourceSet{
rs: rs,
clusterConfig: clusterConfig,
ec2API: ec2API,
subnetDetails: &SubnetDetails{
controlPlaneOnOutposts: clusterConfig.IsControlPlaneOnOutposts(),
},
extendForOutposts: extendForOutposts,
}
}
func (v *IPv4VPCResourceSet) CreateTemplate(ctx context.Context) (*gfnt.Value, *SubnetDetails, error) {
if err := v.addResources(); err != nil {
return nil, nil, err
}
v.addOutputs(ctx)
return v.vpcID, v.subnetDetails, nil
}
// AddResources adds all required resources
func (v *IPv4VPCResourceSet) addResources() error {
vpc := v.clusterConfig.VPC
v.vpcID = v.rs.newResource(cfnVPCResource, &gfnec2.VPC{
CidrBlock: gfnt.NewString(vpc.CIDR.String()),
EnableDnsSupport: gfnt.True(),
EnableDnsHostnames: gfnt.True(),
})
if v.clusterConfig.IsFullyPrivate() {
v.noNAT()
v.subnetDetails.Private = v.addSubnets(nil, api.SubnetTopologyPrivate, vpc.Subnets.Private)
return nil
}
refIG := v.rs.newResource("InternetGateway", &gfnec2.InternetGateway{})
vpcGA := "VPCGatewayAttachment"
v.rs.newResource(vpcGA, &gfnec2.VPCGatewayAttachment{
InternetGatewayId: refIG,
VpcId: v.vpcID,
})
refPublicRT := v.rs.newResource("PublicRouteTable", &gfnec2.RouteTable{
VpcId: v.vpcID,
})
if api.IsEnabled(vpc.AutoAllocateIPv6) {
v.rs.newResource("AutoAllocatedCIDRv6", &gfnec2.VPCCidrBlock{
VpcId: v.vpcID,
AmazonProvidedIpv6CidrBlock: gfnt.True(),
})
v.rs.newResource(PubSubIPv6RouteKey, &gfnec2.Route{
RouteTableId: refPublicRT,
DestinationIpv6CidrBlock: gfnt.NewString(InternetIPv6CIDR),
GatewayId: refIG,
AWSCloudFormationDependsOn: []string{vpcGA},
})
}
v.rs.newResource("PublicSubnetRoute", &gfnec2.Route{
RouteTableId: refPublicRT,
DestinationCidrBlock: gfnt.NewString(InternetCIDR),
GatewayId: refIG,
AWSCloudFormationDependsOn: []string{vpcGA},
})
v.subnetDetails.Public = v.addSubnets(refPublicRT, api.SubnetTopologyPublic, vpc.Subnets.Public)
if err := v.addNATGateways(); err != nil {
return err
}
v.subnetDetails.Private = v.addSubnets(nil, api.SubnetTopologyPrivate, vpc.Subnets.Private)
if vpc.LocalZoneSubnets != nil {
if len(vpc.LocalZoneSubnets.Public) > 0 {
v.subnetDetails.PublicLocalZone = v.addSubnets(refPublicRT, api.SubnetTopologyPublic, vpc.LocalZoneSubnets.Public)
}
if len(vpc.LocalZoneSubnets.Public) > 0 {
v.subnetDetails.PrivateLocalZone = v.addSubnets(nil, api.SubnetTopologyPrivate, vpc.LocalZoneSubnets.Private)
}
}
return nil
}
func (s *SubnetDetails) ControlPlaneSubnetRefs() []*gfnt.Value {
privateSubnetRefs := s.PrivateSubnetRefs()
if s.controlPlaneOnOutposts && len(privateSubnetRefs) > 0 {
return privateSubnetRefs
}
return append(s.PublicSubnetRefs(), privateSubnetRefs...)
}
func (s *SubnetDetails) PublicSubnetRefs() []*gfnt.Value {
return collectSubnetRefs(s.Public)
}
func (s *SubnetDetails) PrivateSubnetRefs() []*gfnt.Value {
return collectSubnetRefs(s.Private)
}
func (s *SubnetDetails) PublicLocalZoneSubnetRefs() []*gfnt.Value {
return collectSubnetRefs(s.PublicLocalZone)
}
func (s *SubnetDetails) PrivateLocalZoneSubnetRefs() []*gfnt.Value {
return collectSubnetRefs(s.PrivateLocalZone)
}
func (s *SubnetDetails) PublicOutpostSubnetRefs() []*gfnt.Value {
return collectSubnetRefsPredicate(s.Public, func(sr SubnetResource) bool {
return sr.onOutpost
})
}
func (s *SubnetDetails) PrivateOutpostSubnetRefs() []*gfnt.Value {
return collectSubnetRefsPredicate(s.Private, func(sr SubnetResource) bool {
return sr.onOutpost
})
}
func collectSubnetRefsPredicate(subnetResources []SubnetResource, predicateFn func(SubnetResource) bool) []*gfnt.Value {
var subnetRefs []*gfnt.Value
for _, subnetAZ := range subnetResources {
if predicateFn(subnetAZ) {
subnetRefs = append(subnetRefs, subnetAZ.Subnet)
}
}
return subnetRefs
}
func collectSubnetRefs(subnetResources []SubnetResource) []*gfnt.Value {
return collectSubnetRefsPredicate(subnetResources, func(_ SubnetResource) bool {
return true
})
}
// addOutputs adds VPC resource outputs
func (v *IPv4VPCResourceSet) addOutputs(ctx context.Context) {
v.rs.defineOutput(outputs.ClusterVPC, v.vpcID, true, func(val string) error {
v.clusterConfig.VPC.ID = val
return nil
})
if v.clusterConfig.VPC.NAT != nil {
v.rs.defineOutputWithoutCollector(outputs.ClusterFeatureNATMode, v.clusterConfig.VPC.NAT.Gateway, false)
}
addSubnetOutputFunc := func(subnetRefs []*gfnt.Value, outputName string, handler func(subnetIDs []string) error) {
v.rs.defineJoinedOutput(outputName, subnetRefs, true, func(value string) error {
return handler(strings.Split(value, ","))
})
}
addSubnetOutput := func(subnetRefs []*gfnt.Value, subnetMapping api.AZSubnetMapping, outputName string) {
addSubnetOutputFunc(subnetRefs, outputName, func(subnetIDs []string) error {
return vpc.ImportSubnetsFromIDList(ctx, v.ec2API, v.clusterConfig, subnetMapping, subnetIDs)
})
}
addSubnetOutputWithAlias := func(subnetRefs []*gfnt.Value, subnetMapping api.AZSubnetMapping, outputName string, makeSubnetAlias vpc.MakeSubnetAlias) {
addSubnetOutputFunc(subnetRefs, outputName, func(subnetIDs []string) error {
return vpc.ImportSubnetsByIDsWithAlias(ctx, v.ec2API, v.clusterConfig, subnetMapping, subnetIDs, makeSubnetAlias)
})
}
clusterVPC := v.clusterConfig.VPC
if subnetAZs := v.subnetDetails.PrivateSubnetRefs(); len(subnetAZs) > 0 {
addSubnetOutput(subnetAZs, clusterVPC.Subnets.Private, outputs.ClusterSubnetsPrivate)
}
if subnetAZs := v.subnetDetails.PublicSubnetRefs(); len(subnetAZs) > 0 {
addSubnetOutput(subnetAZs, clusterVPC.Subnets.Public, outputs.ClusterSubnetsPublic)
}
if subnetAZs := v.subnetDetails.PrivateLocalZoneSubnetRefs(); len(subnetAZs) > 0 {
addSubnetOutput(subnetAZs, clusterVPC.LocalZoneSubnets.Private, outputs.ClusterSubnetsPrivateLocal)
}
if subnetAZs := v.subnetDetails.PublicLocalZoneSubnetRefs(); len(subnetAZs) > 0 {
addSubnetOutput(subnetAZs, clusterVPC.LocalZoneSubnets.Public, outputs.ClusterSubnetsPublicLocal)
}
if v.extendForOutposts {
if subnetAZs := v.subnetDetails.PrivateOutpostSubnetRefs(); len(subnetAZs) > 0 {
addSubnetOutputWithAlias(subnetAZs, clusterVPC.Subnets.Private, outputs.ClusterSubnetsPrivateExtended, vpc.MakeExtendedSubnetAliasFunc())
}
if subnetAZs := v.subnetDetails.PublicOutpostSubnetRefs(); len(subnetAZs) > 0 {
addSubnetOutputWithAlias(subnetAZs, clusterVPC.Subnets.Public, outputs.ClusterSubnetsPublicExtended, vpc.MakeExtendedSubnetAliasFunc())
}
}
if v.clusterConfig.IsFullyPrivate() {
v.rs.defineOutputWithoutCollector(outputs.ClusterFullyPrivate, true, true)
}
}
// RenderJSON returns the rendered JSON
func (v *IPv4VPCResourceSet) RenderJSON() ([]byte, error) {
return v.rs.renderJSON()
}
func (v *IPv4VPCResourceSet) addSubnets(refRT *gfnt.Value, topology api.SubnetTopology, subnets map[string]api.AZSubnetSpec) []SubnetResource {
autoAllocateIPV6 := api.IsEnabled(v.clusterConfig.VPC.AutoAllocateIPv6)
var subnetResources []SubnetResource
for name, s := range subnets {
az := s.AZ
nameAlias := makeAZResourceName(name)
subnet := &gfnec2.Subnet{
AvailabilityZone: gfnt.NewString(az),
CidrBlock: gfnt.NewString(s.CIDR.String()),
VpcId: v.vpcID,
}
maybeSetHostnameType(v.clusterConfig.VPC, subnet)
if v.clusterConfig.IsControlPlaneOnOutposts() {
subnet.OutpostArn = gfnt.NewString(v.clusterConfig.Outpost.ControlPlaneOutpostARN)
} else if s.OutpostARN != "" {
subnet.OutpostArn = gfnt.NewString(s.OutpostARN)
}
switch topology {
case api.SubnetTopologyPrivate:
// Choose the appropriate route table for private subnets.
refRT = gfnt.MakeRef("PrivateRouteTable" + nameAlias)
subnet.Tags = []gfncfn.Tag{{
Key: gfnt.NewString("kubernetes.io/role/internal-elb"),
Value: gfnt.NewString("1"),
}}
case api.SubnetTopologyPublic:
subnet.Tags = []gfncfn.Tag{{
Key: gfnt.NewString("kubernetes.io/role/elb"),
Value: gfnt.NewString("1"),
}}
subnet.MapPublicIpOnLaunch = gfnt.True()
}
subnetAlias := string(topology) + nameAlias
refSubnet := v.rs.newResource("Subnet"+subnetAlias, subnet)
v.rs.newResource("RouteTableAssociation"+subnetAlias, &gfnec2.SubnetRouteTableAssociation{
SubnetId: refSubnet,
RouteTableId: refRT,
})
if autoAllocateIPV6 {
refSubnetSlices := getSubnetIPv6CIDRBlock((len(v.clusterConfig.AvailabilityZones) * 2) + 2)
v.rs.newResource(subnetAlias+"CIDRv6", &gfnec2.SubnetCidrBlock{
SubnetId: refSubnet,
Ipv6CidrBlock: gfnt.MakeFnSelect(gfnt.NewInteger(s.CIDRIndex), refSubnetSlices),
})
}
subnetResources = append(subnetResources, SubnetResource{
AvailabilityZone: az,
RouteTable: refRT,
Subnet: refSubnet,
onOutpost: s.OutpostARN != "",
})
}
return subnetResources
}
func (v *IPv4VPCResourceSet) addNATGateways() error {
switch *v.clusterConfig.VPC.NAT.Gateway {
case api.ClusterHighlyAvailableNAT:
v.haNAT()
case api.ClusterSingleNAT:
v.singleNAT()
case api.ClusterDisableNAT:
v.noNAT()
default:
// TODO validate this before starting to add resources
return fmt.Errorf("%s is not a valid NAT gateway mode", *v.clusterConfig.VPC.NAT.Gateway)
}
return nil
}
var (
sgProtoTCP = gfnt.NewString("tcp")
sgSourceAnywhereIPv4 = gfnt.NewString("0.0.0.0/0")
sgSourceAnywhereIPv6 = gfnt.NewString("::/0")
sgPortZero = gfnt.NewInteger(0)
sgMaxNodePort = gfnt.NewInteger(65535)
sgPortHTTPS = gfnt.NewInteger(443)
sgPortSSH = gfnt.NewInteger(22)
)
type clusterSecurityGroup struct {
ControlPlane *gfnt.Value
ClusterSharedNode *gfnt.Value
}
func (v *IPv4VPCResourceSet) haNAT() {
for subnetAlias := range v.clusterConfig.VPC.Subnets.Public {
subnetAZResourceName := makeAZResourceName(subnetAlias)
// Allocate an EIP
v.rs.newResource("NATIP"+subnetAZResourceName, &gfnec2.EIP{
Domain: gfnt.NewString("vpc"),
})
// Allocate a NAT gateway in the public subnet
refNG := v.rs.newResource("NATGateway"+subnetAZResourceName, &gfnec2.NatGateway{
AllocationId: gfnt.MakeFnGetAttString("NATIP"+subnetAZResourceName, "AllocationId"),
SubnetId: gfnt.MakeRef("SubnetPublic" + subnetAZResourceName),
})
// Allocate a routing table for the private subnet
refRT := v.rs.newResource("PrivateRouteTable"+subnetAZResourceName, &gfnec2.RouteTable{
VpcId: v.vpcID,
})
// Create a route that sends Internet traffic through the NAT gateway
v.rs.newResource("NATPrivateSubnetRoute"+subnetAZResourceName, &gfnec2.Route{
RouteTableId: refRT,
DestinationCidrBlock: gfnt.NewString(InternetCIDR),
NatGatewayId: refNG,
})
// Associate the routing table with the subnet
v.rs.newResource("RouteTableAssociationPrivate"+subnetAZResourceName, &gfnec2.SubnetRouteTableAssociation{
SubnetId: gfnt.MakeRef("SubnetPrivate" + subnetAZResourceName),
RouteTableId: refRT,
})
}
}
func (v *IPv4VPCResourceSet) singleNAT() {
sortedAZs := v.clusterConfig.AvailabilityZones
firstUpperAZ := makeAZResourceName(sortedAZs[0])
v.rs.newResource("NATIP", &gfnec2.EIP{
Domain: gfnt.NewString("vpc"),
})
refNG := v.rs.newResource("NATGateway", &gfnec2.NatGateway{
AllocationId: gfnt.MakeFnGetAttString("NATIP", "AllocationId"),
SubnetId: gfnt.MakeRef("SubnetPublic" + firstUpperAZ),
})
forEachNATSubnet(v.clusterConfig.VPC, func(subnetAlias string) {
subnetAZResourceName := makeAZResourceName(subnetAlias)
refRT := v.rs.newResource("PrivateRouteTable"+subnetAZResourceName, &gfnec2.RouteTable{
VpcId: v.vpcID,
})
v.rs.newResource("NATPrivateSubnetRoute"+subnetAZResourceName, &gfnec2.Route{
RouteTableId: refRT,
DestinationCidrBlock: gfnt.NewString(InternetCIDR),
NatGatewayId: refNG,
})
v.rs.newResource("RouteTableAssociationPrivate"+subnetAZResourceName, &gfnec2.SubnetRouteTableAssociation{
SubnetId: gfnt.MakeRef("SubnetPrivate" + subnetAZResourceName),
RouteTableId: refRT,
})
})
}
func (v *IPv4VPCResourceSet) noNAT() {
forEachNATSubnet(v.clusterConfig.VPC, func(subnetAlias string) {
subnetAZResourceName := makeAZResourceName(subnetAlias)
refRT := v.rs.newResource("PrivateRouteTable"+subnetAZResourceName, &gfnec2.RouteTable{
VpcId: v.vpcID,
})
v.rs.newResource("RouteTableAssociationPrivate"+subnetAZResourceName, &gfnec2.SubnetRouteTableAssociation{
SubnetId: gfnt.MakeRef("SubnetPrivate" + subnetAZResourceName),
RouteTableId: refRT,
})
})
}
func forEachNATSubnet(clusterVPC *api.ClusterVPC, fn func(subnetAlias string)) {
for subnetAlias := range clusterVPC.Subnets.Private {
fn(subnetAlias)
}
if clusterVPC.LocalZoneSubnets != nil {
for subnetAlias := range clusterVPC.LocalZoneSubnets.Private {
fn(subnetAlias)
}
}
}
func makeAZResourceName(subnetAZ string) string {
return strings.ToUpper(strings.ReplaceAll(subnetAZ, "-", ""))
}
func maybeSetHostnameType(vpc *api.ClusterVPC, subnet *gfnec2.Subnet) {
if hostnameType := vpc.HostnameType; hostnameType != "" {
subnet.PrivateDnsNameOptionsOnLaunch = &gfnec2.Subnet_PrivateDnsNameOptionsOnLaunch{
HostnameType: gfnt.NewString(hostnameType),
}
}
}