Skip to content

Commit cb50d8a

Browse files
hakuna-matatahHarish Kuna
andauthored
Provision KIT CP nodes in private subnets vs public (#67)
Co-authored-by: Harish Kuna <[email protected]>
1 parent 37b18ed commit cb50d8a

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

testbed/stack.ts

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,51 +31,23 @@ export class Testbed extends cdk.Stack {
3131
],
3232
});
3333

34-
//Tag pub subnets for KIT CP
35-
const selection = vpc.selectSubnets({
36-
subnetType: ec2.SubnetType.PUBLIC
37-
});
38-
selection.subnets.forEach(subnet => {
39-
Tags.of(subnet).add('kit/hostcluster', `${id}-controlplane`)
40-
})
41-
4234
//ToDo: revisit once this is resolved - https://github.com/aws/aws-cdk/issues/5927
43-
// index<=8 will give us 9 /16 cidrs additionally to make a mega VPC.
35+
//create private subnets for KIT operator CP nodes/pods in all AZs
36+
for (let index = 0; index < cdk.Stack.of(this).availabilityZones.length; index++) {
37+
//Also, pick up non overlapping cidrs with KIT operator DP nodes;
38+
let privateSubnet = this.createPrivateSubnetForVPC(id, vpc, `10.${index + 20}.0.0/16`, cdk.Stack.of(this).availabilityZones[index])
39+
//Tag private subnets for KIT CP
40+
Tags.of(privateSubnet).add('kit/hostcluster', `${id}-controlplane`)
41+
let natSubnet = this.createPublicSubnetForVPC(id, vpc, `10.0.80.${index * 16}/28`, cdk.Stack.of(this).availabilityZones[index])
42+
this.configureNatProviderForPrivateSubnet(vpc, natSubnet, privateSubnet)
43+
}
44+
// index<=8 will give us 9 /16 cidrs additionally to make a mega VPC for DP nodes.
4445
for (let index = 0; index <= 8; index++) {
45-
let additionalCidr = new ec2.CfnVPCCidrBlock(this, `${id}-cidr-${index}`, {
46-
vpcId: vpc.vpcId,
47-
cidrBlock: `10.${index + 1}.0.0/16`
48-
});
49-
let privateSubnet = new ec2.PrivateSubnet(this, `${id}-private-subnet-${index}`, {
50-
availabilityZone: cdk.Stack.of(this).availabilityZones[index % cdk.Stack.of(this).availabilityZones.length],
51-
vpcId: vpc.vpcId,
52-
cidrBlock: `10.${index + 1}.0.0/16`
53-
})
54-
privateSubnet.node.addDependency(additionalCidr);
55-
//Tag pub subnets for KIT DP
46+
let privateSubnet = this.createPrivateSubnetForVPC(id, vpc, `10.${index + 1}.0.0/16`, cdk.Stack.of(this).availabilityZones[index % cdk.Stack.of(this).availabilityZones.length])
47+
//Tag private subnets for KIT DP
5648
Tags.of(privateSubnet).add('kit/hostcluster', `${id}-dataplane`)
57-
let natSubnet = new ec2.PublicSubnet(this, `${id}-nat-subnet-${index}`, {
58-
availabilityZone: cdk.Stack.of(this).availabilityZones[index % cdk.Stack.of(this).availabilityZones.length],
59-
vpcId: vpc.vpcId,
60-
cidrBlock: `10.0.64.${index * 16}/28`
61-
})
62-
//add igw route for nat subnets
63-
let routeTableId = natSubnet.routeTable.routeTableId
64-
new ec2.CfnRoute(this, 'publicIGWRoute' + index, {
65-
routeTableId,
66-
gatewayId: vpc.internetGatewayId,
67-
destinationCidrBlock: "0.0.0.0/0"
68-
})
69-
70-
ec2.NatProvider.gateway().configureNat({
71-
natSubnets: [
72-
natSubnet
73-
],
74-
privateSubnets: [
75-
privateSubnet
76-
],
77-
vpc: vpc
78-
})
49+
let natSubnet = this.createPublicSubnetForVPC(id, vpc, `10.0.64.${index * 16}/28`, cdk.Stack.of(this).availabilityZones[index % cdk.Stack.of(this).availabilityZones.length])
50+
this.configureNatProviderForPrivateSubnet(vpc, natSubnet, privateSubnet)
7951
}
8052

8153
const cluster = new eks.Cluster(this, 'cluster', {
@@ -116,4 +88,44 @@ export class Testbed extends cdk.Stack {
11688
// Tag all resources for discovery by Karpenter
11789
Tags.of(this).add(`kubernetes.io/cluster/${id}`, "owned")
11890
}
91+
92+
createPrivateSubnetForVPC(id: string, vpc: ec2.Vpc, cidr: string, az: string): ec2.PrivateSubnet {
93+
let additionalCidr = new ec2.CfnVPCCidrBlock(this, `${id}-cidr-${cidr}`, {
94+
vpcId: vpc.vpcId,
95+
cidrBlock: cidr
96+
});
97+
let privateSubnet = new ec2.PrivateSubnet(this, `${id}-private-subnet-${cidr}`, {
98+
availabilityZone: az,
99+
vpcId: vpc.vpcId,
100+
cidrBlock: cidr
101+
})
102+
privateSubnet.node.addDependency(additionalCidr);
103+
return privateSubnet
104+
}
105+
createPublicSubnetForVPC(id: string, vpc: ec2.Vpc, cidr: string, az: string): ec2.PublicSubnet {
106+
let publicSubnet = new ec2.PublicSubnet(this, `${id}-nat-subnet-${cidr}`, {
107+
availabilityZone: az,
108+
vpcId: vpc.vpcId,
109+
cidrBlock: cidr
110+
})
111+
//add igw route for nat subnets
112+
let routeTableId = publicSubnet.routeTable.routeTableId
113+
new ec2.CfnRoute(this, `publicIGWRoute-${cidr}`, {
114+
routeTableId,
115+
gatewayId: vpc.internetGatewayId,
116+
destinationCidrBlock: "0.0.0.0/0"
117+
})
118+
return publicSubnet
119+
}
120+
configureNatProviderForPrivateSubnet(vpc: ec2.Vpc, natSubnet: ec2.PublicSubnet, privateSubnet: ec2.PrivateSubnet): void {
121+
ec2.NatProvider.gateway().configureNat({
122+
natSubnets: [
123+
natSubnet
124+
],
125+
privateSubnets: [
126+
privateSubnet
127+
],
128+
vpc: vpc
129+
})
130+
}
119131
}

0 commit comments

Comments
 (0)