-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
vpc_ipv6.go
210 lines (177 loc) · 7.44 KB
/
vpc_ipv6.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
package builder
import (
"context"
"strings"
"github.com/weaveworks/eksctl/pkg/awsapi"
"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"
)
// A IPv6VPCResourceSet builds the resources required for the specified VPC
type IPv6VPCResourceSet struct {
rs *resourceSet
clusterConfig *api.ClusterConfig
ec2API awsapi.EC2
}
// NewIPv6VPCResourceSet creates and returns a new VPCResourceSet
func NewIPv6VPCResourceSet(rs *resourceSet, clusterConfig *api.ClusterConfig, ec2API awsapi.EC2) *IPv6VPCResourceSet {
return &IPv6VPCResourceSet{
rs: rs,
clusterConfig: clusterConfig,
ec2API: ec2API,
}
}
func (v *IPv6VPCResourceSet) CreateTemplate(ctx context.Context) (*gfnt.Value, *SubnetDetails, error) {
var publicSubnetResourceRefs, privateSubnetResourceRefs []*gfnt.Value
vpcResourceRef := v.rs.newResource(VPCResourceKey, &gfnec2.VPC{
CidrBlock: gfnt.NewString(v.clusterConfig.VPC.CIDR.String()),
EnableDnsSupport: gfnt.True(),
EnableDnsHostnames: gfnt.True(),
})
v.rs.defineOutput(outputs.ClusterVPC, vpcResourceRef, true, func(val string) error {
v.clusterConfig.VPC.ID = val
return nil
})
v.addIpv6CidrBlock()
addSubnetOutput := func(subnetRefs []*gfnt.Value, subnetMapping api.AZSubnetMapping, outputName string) {
v.rs.defineJoinedOutput(outputName, subnetRefs, true, func(value string) error {
return vpc.ImportSubnetsFromIDList(ctx, v.ec2API, v.clusterConfig, subnetMapping, strings.Split(value, ","))
})
}
var privateSubnets []SubnetResource
cidrPartitions := (len(v.clusterConfig.AvailabilityZones) * 2) + 2
for i, az := range v.clusterConfig.AvailabilityZones {
azFormatted := formatAZ(az)
rtRef := v.rs.newResource(PrivateRouteTableKey+azFormatted, &gfnec2.RouteTable{
VpcId: gfnt.MakeRef(VPCResourceKey),
})
subnet := v.createSubnet(az, azFormatted, i+len(v.clusterConfig.AvailabilityZones), cidrPartitions, true)
privateSubnetResourceRefs = append(privateSubnetResourceRefs, subnet)
privateSubnets = append(privateSubnets, SubnetResource{
Subnet: subnet,
AvailabilityZone: az,
RouteTable: rtRef,
})
v.rs.newResource(PrivateRouteTableAssociation+azFormatted, &gfnec2.SubnetRouteTableAssociation{
RouteTableId: rtRef,
SubnetId: subnet,
})
}
addSubnetOutput(privateSubnetResourceRefs, v.clusterConfig.VPC.Subnets.Private, outputs.ClusterSubnetsPrivate)
if v.clusterConfig.IsFullyPrivate() {
return vpcResourceRef, &SubnetDetails{
Private: privateSubnets,
}, nil
}
// add the rest of the public resources.
refIGW := v.rs.newResource(IGWKey, &gfnec2.InternetGateway{})
v.rs.newResource(GAKey, &gfnec2.VPCGatewayAttachment{
InternetGatewayId: gfnt.MakeRef(IGWKey),
VpcId: gfnt.MakeRef(VPCResourceKey),
})
firstPublicSubnet := PublicSubnetKey + formatAZ(v.clusterConfig.AvailabilityZones[0])
v.rs.newResource(NATGatewayKey, &gfnec2.NatGateway{
AWSCloudFormationDependsOn: []string{
ElasticIPKey,
firstPublicSubnet,
GAKey,
},
AllocationId: gfnt.MakeFnGetAtt(ElasticIPKey, gfnt.NewString("AllocationId")),
SubnetId: gfnt.MakeRef(firstPublicSubnet),
})
v.rs.newResource(ElasticIPKey, &gfnec2.EIP{
Domain: gfnt.NewString("vpc"),
AWSCloudFormationDependsOn: []string{GAKey},
})
v.rs.newResource(PubRouteTableKey, &gfnec2.RouteTable{
VpcId: gfnt.MakeRef(VPCResourceKey),
})
v.rs.newResource(PubSubRouteKey, &gfnec2.Route{
AWSCloudFormationDependsOn: []string{GAKey},
DestinationCidrBlock: gfnt.NewString(InternetCIDR),
GatewayId: refIGW,
RouteTableId: gfnt.MakeRef(PubRouteTableKey),
})
v.rs.newResource(PubSubIPv6RouteKey, &gfnec2.Route{
AWSCloudFormationDependsOn: []string{GAKey},
DestinationIpv6CidrBlock: gfnt.NewString(InternetIPv6CIDR),
GatewayId: refIGW,
RouteTableId: gfnt.MakeRef(PubRouteTableKey),
})
v.rs.newResource(EgressOnlyInternetGatewayKey, &gfnec2.EgressOnlyInternetGateway{
VpcId: gfnt.MakeRef(VPCResourceKey),
})
var publicSubnets []SubnetResource
for i, az := range v.clusterConfig.AvailabilityZones {
azFormatted := formatAZ(az)
subnet := v.createSubnet(az, azFormatted, i, cidrPartitions, false)
publicSubnets = append(publicSubnets, SubnetResource{Subnet: subnet, AvailabilityZone: az})
publicSubnetResourceRefs = append(publicSubnetResourceRefs, subnet)
v.rs.newResource(PubRouteTableAssociation+azFormatted, &gfnec2.SubnetRouteTableAssociation{
RouteTableId: gfnt.MakeRef(PubRouteTableKey),
SubnetId: gfnt.MakeRef(PublicSubnetKey + azFormatted),
})
v.rs.newResource(PrivateSubnetIpv6RouteKey+azFormatted, &gfnec2.Route{
DestinationIpv6CidrBlock: gfnt.NewString(InternetIPv6CIDR),
EgressOnlyInternetGatewayId: gfnt.MakeRef(EgressOnlyInternetGatewayKey),
RouteTableId: gfnt.MakeRef(PrivateRouteTableKey + azFormatted),
})
v.rs.newResource(PrivateSubnetRouteKey+azFormatted, &gfnec2.Route{
AWSCloudFormationDependsOn: []string{NATGatewayKey, GAKey},
DestinationCidrBlock: gfnt.NewString(InternetCIDR),
NatGatewayId: gfnt.MakeRef(NATGatewayKey),
RouteTableId: gfnt.MakeRef(PrivateRouteTableKey + azFormatted),
})
}
addSubnetOutput(publicSubnetResourceRefs, v.clusterConfig.VPC.Subnets.Public, outputs.ClusterSubnetsPublic)
return vpcResourceRef, &SubnetDetails{
Private: privateSubnets,
Public: publicSubnets,
}, nil
}
func (v *IPv6VPCResourceSet) addIpv6CidrBlock() {
if v.clusterConfig.VPC.IPv6Cidr != "" {
v.rs.newResource(IPv6CIDRBlockKey, &gfnec2.VPCCidrBlock{
AmazonProvidedIpv6CidrBlock: gfnt.False(),
Ipv6CidrBlock: gfnt.NewString(v.clusterConfig.VPC.IPv6Cidr),
Ipv6Pool: gfnt.NewString(v.clusterConfig.VPC.IPv6Pool),
VpcId: gfnt.MakeRef(VPCResourceKey),
})
return
}
v.rs.newResource(IPv6CIDRBlockKey, &gfnec2.VPCCidrBlock{
AmazonProvidedIpv6CidrBlock: gfnt.True(),
VpcId: gfnt.MakeRef(VPCResourceKey),
})
}
func (v *IPv6VPCResourceSet) RenderJSON() ([]byte, error) {
return v.rs.renderJSON()
}
func (v *IPv6VPCResourceSet) createSubnet(az, azFormatted string, i, cidrPartitions int, private bool) *gfnt.Value {
subnetKey := PublicSubnetKey + azFormatted
mapPublicIPOnLaunch := gfnt.True()
elbTagKey := "kubernetes.io/role/elb"
if private {
subnetKey = PrivateSubnetKey + azFormatted
mapPublicIPOnLaunch = nil
elbTagKey = "kubernetes.io/role/internal-elb"
}
subnet := &gfnec2.Subnet{
AWSCloudFormationDependsOn: []string{IPv6CIDRBlockKey},
AvailabilityZone: gfnt.NewString(az),
CidrBlock: gfnt.MakeFnSelect(gfnt.NewInteger(i), getSubnetIPv4CIDRBlock(cidrPartitions, v.clusterConfig.VPC.Network.CIDR)),
Ipv6CidrBlock: gfnt.MakeFnSelect(gfnt.NewInteger(i), getSubnetIPv6CIDRBlock(cidrPartitions)),
MapPublicIpOnLaunch: mapPublicIPOnLaunch,
AssignIpv6AddressOnCreation: gfnt.True(),
VpcId: gfnt.MakeRef(VPCResourceKey),
Tags: []cloudformation.Tag{{
Key: gfnt.NewString(elbTagKey),
Value: gfnt.NewString("1"),
}},
}
maybeSetHostnameType(v.clusterConfig.VPC, subnet)
return v.rs.newResource(subnetKey, subnet)
}