Skip to content
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

Fix the CIDR range calculation in case of a custom CIDR block #4904

Merged
merged 8 commits into from
Mar 10, 2022

Conversation

Skarlso
Copy link
Contributor

@Skarlso Skarlso commented Mar 4, 2022

Description

Closes #4838

TODO

This is still failing even though I'm calculating the CIDR size correctly. Maybe I'm not taking into account that AWS also reserves some ips...

I'm calculating 23 correctly and yet:

2022-03-04 18:21:39 [✖]  AWS::EC2::Subnet/PublicSubnetUSWEST2B: CREATE_FAILED – "Template error: Template error: invalid CIDR block size 23"

Our IPV4 calculations with the same custom cidr also calculate 23 as cidr and that works. I suspect we mess something up with the CF template maybe?

Will have to take a look at that.

        "CidrBlock": {
          "Fn::Select": [
            3,
            {
              "Fn::Cidr": [
                {
                  "Fn::GetAtt": [
                    "VPC",
                    "CidrBlock"
                  ]
                },
                8,
                23
              ]
            }
          ]
        },

As an exercise.... Our hardcoded values add up as well. /16 bit -> 65535 -> /8 -> 8192 -> log2(8192) -> 13. And indeed that is a working cidr block size. So why is 23 not a valid block size? 🤔

DONE!

TODO:

  • Add some test coverage

Checklist

  • Added tests that cover your change (if possible)
  • Added/modified documentation as required (such as the README.md, or the userdocs directory)
  • Manually tested
  • Made sure the title of the PR is a good description that can go into the release notes
  • (Core team) Added labels for change area (e.g. area/nodegroup) and kind (e.g. kind/improvement)

BONUS POINTS checklist: complete for good vibes and maybe prizes?! 🤯

  • Backfilled missing tests for code in same general area 🎉
  • Refactored something and made the world a better place 🌟

@Skarlso
Copy link
Contributor Author

Skarlso commented Mar 4, 2022

Oh dang, I think I figured it out... I can't read, again... :D They are already subtracting the bit from 32. We don't have to do that. We specify the number they calculate a cidr bit from it.

cidrBits
The number of subnet bits for the CIDR. For example, specifying a value "8" for this parameter will create a CIDR with a >mask of "/24".

Note
Subnet bits is the inverse of subnet mask. To calculate the required host bits for a given subnet bits, subtract the subnet >bits from 32 for IPv4 or 128 for IPv6.

THE NOTE IS MISLEADING! It makes you think that you have to subtract it from 32. BUT YOU DON'T!

refSubnetSlices := gfnt.MakeFnCIDR(gfnt.MakeFnGetAttString("VPC", "CidrBlock"), gfnt.NewInteger(cidrPartitions), gfnt.NewInteger(32-desiredMask))

This was wrong. Should be.

refSubnetSlices := gfnt.MakeFnCIDR(gfnt.MakeFnGetAttString("VPC", "CidrBlock"), gfnt.NewInteger(cidrPartitions), gfnt.NewInteger(desiredMask))

Created a subnet with block 198.18.160.0/23. So this works. :)

@Skarlso
Copy link
Contributor Author

Skarlso commented Mar 4, 2022

Works for the original path now as well. Creates the correct range subnet IP:

subnet-0f99d72c80225f9ef | Available | vpc-0151de9fef3f12b30 | eksctl-invalid-ipv6-cidr-6-nocustomcidr-cluster/VPC | 192.168.32.0/19

@Skarlso
Copy link
Contributor Author

Skarlso commented Mar 4, 2022

With custom cidr ipv6:

2022-03-04 19:18:37 [ℹ]  no recommended policies found, proceeding without any IAM
2022-03-04 19:18:37 [ℹ]  creating addon
2022-03-04 19:18:38 [ℹ]  successfully created addon
2022-03-04 19:18:39 [ℹ]  kubectl command should work with "/Users/skarlso/.kube/config", try 'kubectl get nodes'
2022-03-04 19:18:39 [✔]  EKS cluster "invalid-ipv6-cidr-5" in "us-west-2" region is ready
➜  invalid_ipv6_cidr

Without custom cidr ipv6:

2022-03-04 19:41:01 [ℹ]  no recommended policies found, proceeding without any IAM
2022-03-04 19:41:01 [ℹ]  creating addon
2022-03-04 19:41:02 [ℹ]  successfully created addon
2022-03-04 19:41:03 [ℹ]  kubectl command should work with "/Users/skarlso/.kube/config", try 'kubectl get nodes'
2022-03-04 19:41:03 [✔]  EKS cluster "invalid-ipv6-cidr-6-nocustomcidr" in "us-west-2" region is ready
➜  invalid_ipv6_cidr

Since I only altered ipv6 path, I only launch a test cluster for ipv4 to see that the original path didn't break.

@Skarlso Skarlso force-pushed the fix_invalid_cidr branch 3 times, most recently from 580a752 to 50ffe71 Compare March 4, 2022 20:26
@Skarlso Skarlso marked this pull request as ready for review March 4, 2022 20:28
pkg/cfn/builder/vpc.go Outdated Show resolved Hide resolved
pkg/cfn/builder/vpc.go Outdated Show resolved Hide resolved
Copy link
Contributor

@nikimanoledaki nikimanoledaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, well done! :)

pkg/cfn/builder/vpc.go Outdated Show resolved Hide resolved
pkg/cfn/builder/vpc.go Outdated Show resolved Hide resolved
pkg/cfn/builder/vpc.go Outdated Show resolved Hide resolved
@Skarlso Skarlso enabled auto-merge (squash) March 10, 2022 13:53
@Skarlso Skarlso merged commit 856a393 into eksctl-io:main Mar 10, 2022
@nikimanoledaki nikimanoledaki changed the title Fixing the cidr range calculation in case of a custom cidr block Fix the CIDR range calculation in case of a custom CIDR block Mar 14, 2022
@hspencer77 hspencer77 mentioned this pull request Jul 8, 2022
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

invalid CIDR block size error when creating a cluster with IPV6 and custom IPV4 cidr
5 participants