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

Add net.cidr_merge function to produce smallest possible list of CIDRs #2713

Merged

Conversation

ashutosh-narkar
Copy link
Member

This commit adds a new builtin to merge adjacent subnets and return the
smallest possible list of CIDRs.

To help with computing CIDR blocks between two
IP networks, an implemetation from https://github.com/cilium/cilium
is leveraged.

Fixes: #2692

Signed-off-by: Ashutosh Narkar anarkar4387@gmail.com

Copy link
Contributor

@patrick-east patrick-east left a comment

Choose a reason for hiding this comment

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

Implementation looks good 👍

Just one comment on the tests

@@ -44,3 +46,133 @@ func TestNetCIDRExpandCancellation(t *testing.T) {
}

}

func TestBuiltinNetCIDRMerge(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Tests updated to newer format.

tsandall
tsandall previously approved these changes Sep 24, 2020
Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

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

Two minor comments. Other than that LGTM.

ast/builtins.go Outdated
Name: "net.cidr_merge",
Decl: types.NewFunction(
types.Args(
types.NewArray(nil, types.NewAny(types.S)),
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason we can't accept a set of strings as well?

Copy link
Contributor

@patrick-east patrick-east Sep 24, 2020

Choose a reason for hiding this comment

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

I was wondering about that, seems like it would be easy enough to add later if anyone needed it too

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the builtin to accept set of strings.

- note: netcidrmerge/cidr single subnet
modules:
- |
package generated
Copy link
Member

Choose a reason for hiding this comment

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

The generated package was just used for migrating the existing tests. We can just use something a bit more human-friendly for new tests write (e.g., package test).

Copy link
Member

Choose a reason for hiding this comment

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

It would be worthwhile to add a test case that provides IPs as inputs (not CIDRs) as the function should support both.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated package name to test and added test cases that include IPs.

…t of CIDRs

This commit adds a new builtin to merge adjacent subnets and return the
smallest possible list of CIDRs.

To help with computing CIDR blocks between two
IP networks, an implemetation from https://github.com/cilium/cilium
is leveraged.

Fixes: open-policy-agent#2692

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add net.cidr_merge function to produce smallest possible list of subnets
3 participants