Skip to content

Split CIDR consistently across different settings #7840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/cfn/builder/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/cfn/builder/fakes"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
"github.com/weaveworks/eksctl/pkg/utils/ipnet"

_ "embed"
)
Expand Down Expand Up @@ -107,6 +108,7 @@ var _ = Describe("Cluster Template Builder", func() {

Context("when ipFamily is set to IPv6", func() {
BeforeEach(func() {
cfg.VPC.CIDR = &ipnet.IPNet{}
cfg.KubernetesNetworkConfig.IPFamily = api.IPV6Family
})

Expand Down Expand Up @@ -364,6 +366,7 @@ var _ = Describe("Cluster Template Builder", func() {
Context("when private networking is set", func() {
BeforeEach(func() {
cfg.PrivateCluster = &api.PrivateCluster{Enabled: true}
cfg.VPC.CIDR = &ipnet.IPNet{}

detailsJSON := `
{
Expand Down
5 changes: 4 additions & 1 deletion pkg/cfn/builder/vpc_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ func (v *IPv6VPCResourceSet) CreateTemplate(ctx context.Context) (*gfnt.Value, *
})
}

cidrPartitions, _, err := vpc.GetSubnetNetworkSize(v.clusterConfig.VPC.CIDR.IPNet, len(v.clusterConfig.AvailabilityZones)*2)
if err != nil {
return nil, nil, err
}
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{
Expand Down
12 changes: 6 additions & 6 deletions pkg/cfn/builder/vpc_ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ var _ = Describe("IPv6 VPC builder", func() {
},
))

expectedFnIPv4CIDR := `{ "Fn::Cidr": [{ "Fn::GetAtt": ["VPC", "CidrBlock"]}, 6, 13 ]}`
expectedFnIPv4CIDR := `{ "Fn::Cidr": [{ "Fn::GetAtt": ["VPC", "CidrBlock"]}, 8, 13 ]}`
assertCidrBlockCreatedWithSelect(vpcTemplate.Resources[subnetKey].Properties.CidrBlock, expectedFnIPv4CIDR, cidrBlockIndex)

expectedFnIPv6CIDR := `{ "Fn::Cidr": [{ "Fn::Select": [ 0, { "Fn::GetAtt": ["VPC", "Ipv6CidrBlocks"] }]}, 6, 64 ]}`
expectedFnIPv6CIDR := `{ "Fn::Cidr": [{ "Fn::Select": [ 0, { "Fn::GetAtt": ["VPC", "Ipv6CidrBlocks"] }]}, 8, 64 ]}`
assertCidrBlockCreatedWithSelect(vpcTemplate.Resources[subnetKey].Properties.Ipv6CidrBlock, expectedFnIPv6CIDR, cidrBlockIndex)
}
assertSubnetSet(azA, builder.PublicSubnetKey+azAFormatted, "kubernetes.io/role/elb", float64(0), true)
Expand Down Expand Up @@ -390,10 +390,10 @@ var _ = Describe("IPv6 VPC builder", func() {
))

// Note, this is the important difference.
expectedFnIPv4CIDR := `{ "Fn::Cidr": [{ "Fn::GetAtt": ["VPC", "CidrBlock"]}, 6, 9 ]}`
expectedFnIPv4CIDR := `{ "Fn::Cidr": [{ "Fn::GetAtt": ["VPC", "CidrBlock"]}, 8, 9 ]}`
assertCidrBlockCreatedWithSelect(vpcTemplate.Resources[subnetKey].Properties.CidrBlock, expectedFnIPv4CIDR, cidrBlockIndex)

expectedFnIPv6CIDR := `{ "Fn::Cidr": [{ "Fn::Select": [ 0, { "Fn::GetAtt": ["VPC", "Ipv6CidrBlocks"] }]}, 6, 64 ]}`
expectedFnIPv6CIDR := `{ "Fn::Cidr": [{ "Fn::Select": [ 0, { "Fn::GetAtt": ["VPC", "Ipv6CidrBlocks"] }]}, 8, 64 ]}`
assertCidrBlockCreatedWithSelect(vpcTemplate.Resources[subnetKey].Properties.Ipv6CidrBlock, expectedFnIPv6CIDR, cidrBlockIndex)
}
assertSubnetSet(azA, builder.PublicSubnetKey+azAFormatted, "kubernetes.io/role/elb", float64(0), true)
Expand Down Expand Up @@ -542,14 +542,14 @@ var _ = Describe("IPv6 VPC builder", func() {
},
))

expectedFnIPv4CIDR := `{ "Fn::Cidr": [{ "Fn::GetAtt": ["VPC", "CidrBlock"]}, 6, 13 ]}`
expectedFnIPv4CIDR := `{ "Fn::Cidr": [{ "Fn::GetAtt": ["VPC", "CidrBlock"]}, 8, 13 ]}`
Expect(vpcTemplate.Resources[subnetKey].Properties.CidrBlock.(map[string]interface{})["Fn::Select"]).To(HaveLen(2))
Expect(vpcTemplate.Resources[subnetKey].Properties.CidrBlock.(map[string]interface{})["Fn::Select"].([]interface{})[0].(float64)).To(Equal(cidrBlockIndex))
actualFnCIDR, err := json.Marshal(vpcTemplate.Resources[subnetKey].Properties.CidrBlock.(map[string]interface{})["Fn::Select"].([]interface{})[1])
Expect(err).NotTo(HaveOccurred())
Expect(actualFnCIDR).To(MatchJSON([]byte(expectedFnIPv4CIDR)))

expectedFnIPv6CIDR := `{ "Fn::Cidr": [{ "Fn::Select": [ 0, { "Fn::GetAtt": ["VPC", "Ipv6CidrBlocks"] }]}, 6, 64 ]}`
expectedFnIPv6CIDR := `{ "Fn::Cidr": [{ "Fn::Select": [ 0, { "Fn::GetAtt": ["VPC", "Ipv6CidrBlocks"] }]}, 8, 64 ]}`
assertCidrBlockCreatedWithSelect(vpcTemplate.Resources[subnetKey].Properties.Ipv6CidrBlock, expectedFnIPv6CIDR, cidrBlockIndex)
}
assertSubnetSet(azA, builder.PrivateSubnetKey+azAFormatted, "kubernetes.io/role/internal-elb", float64(2), false)
Expand Down
6 changes: 3 additions & 3 deletions pkg/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func SetSubnets(vpc *api.ClusterVPC, availabilityZones, localZones []string) err
zonesTotal := len(availabilityZones) + len(localZones)
subnetsTotal := zonesTotal * 2

subnetSize, networkLength, err := getSubnetNetworkSize(vpc.CIDR.IPNet, subnetsTotal)
subnetSize, networkLength, err := GetSubnetNetworkSize(vpc.CIDR.IPNet, subnetsTotal)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,7 +93,7 @@ type SubnetPair struct {

// ExtendWithOutpostSubnets extends the VPC by returning public and private subnet CIDRs for Outposts.
func ExtendWithOutpostSubnets(vpcCIDR net.IPNet, existingSubnetsCount int, outpostARN, outpostAZ string) (*SubnetPair, error) {
subnetSize, networkLength, err := getSubnetNetworkSize(vpcCIDR, existingSubnetsCount)
subnetSize, networkLength, err := GetSubnetNetworkSize(vpcCIDR, existingSubnetsCount)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func ExtendWithOutpostSubnets(vpcCIDR net.IPNet, existingSubnetsCount int, outpo
}, nil
}

func getSubnetNetworkSize(vpcCIDR net.IPNet, subnetsTotal int) (subnetSize, networkLength int, err error) {
func GetSubnetNetworkSize(vpcCIDR net.IPNet, subnetsTotal int) (subnetSize, networkLength int, err error) {
switch maskSize, _ := vpcCIDR.Mask.Size(); {
case subnetsTotal == 2:
subnetSize = 2
Expand Down