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 Except for Antrea-native ipBlock #6658

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Dyanngg
Copy link
Contributor

@Dyanngg Dyanngg commented Sep 9, 2024

Fixes #6428

This PR adds an "except" field for all ipBlocks in Antrea-native policies and groups. Users can exclude certain CIDRs from the ipBlock.cidr in all resources that support ipBlocks, including AntreaClusterNetworkPolicy, AntreaNetworkPolicy, ClusterGroup and Group. Group membership and IP association query logic are also updated to accommodate this change. Documentation will follow in a separate PR.

@Dyanngg Dyanngg force-pushed the add-ipblock-except-antrea branch 4 times, most recently from c840536 to cd59fed Compare September 10, 2024 02:28
@Dyanngg Dyanngg changed the title [WIP] Add Except for Antrea-native policy ipBlock Add Except for Antrea-native ipBlock Sep 11, 2024
@Dyanngg Dyanngg force-pushed the add-ipblock-except-antrea branch 2 times, most recently from cc5df40 to 1153d8c Compare September 11, 2024 18:18
@Dyanngg Dyanngg added this to the Antrea v2.2 release milestone Sep 11, 2024
@Dyanngg Dyanngg added the area/network-policy Issues or PRs related to network policies. label Sep 11, 2024
@antoninbas antoninbas added action/release-note Indicates a PR that should be included in release notes. kind/feature Categorizes issue or PR as related to a new feature. labels Sep 23, 2024
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

a few minor comments, functionality wise it seems fine to me

hopefully @tnqn and @qiyueyao have time to take a quick look as well

ipNet := &group.IPNets[idx]
if ipNet.Contains(ip) {
for _, ipNet := range group.IPNets {
if ipNet != nil && ipNet.Contains(ip) {
Copy link
Contributor

Choose a reason for hiding this comment

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

just out-of-curiosity, is the nil case possible here (I am not asking to change the code even if this case is not possible in theory)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Practically no. In theory there can be the case where a bad CIDR somehow got pass openAPI validation and then net.ParseCIDR returned a nil ipNet

}
return antreaIPBlock, nil
}

// computeEffectiveIPNetForIPBlocks calculates the list of net.IPNet CIDRs after the
// "except" CIDRs are subtracted from each corresponding ipBlock.
func computeEffectiveIPNetForIPBlocks(ipBlocks []crdv1beta1.IPBlock) []*net.IPNet {
Copy link
Contributor

Choose a reason for hiding this comment

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

this function should be unit tested independently

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. See the latest commit

controlplaneIPNet, _ := cidrStrToIPNet(cidr)
controlplaneIPNetExcept1, _ := cidrStrToIPNet(exceptCIDR1)
controlplaneIPNetExcept2, _ := cidrStrToIPNet(exceptCIDR2)
_, controlplaneIPNetDiff, _ := net.ParseCIDR("10.0.0.192/26")
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use a slightly more complicated case, where the computed controlplaneIPNetDiff is not a single cidr?
I notice that the except cidrs you have are either at the very beginning or at the very end of the IP block, which means we only ever have a single resulting cidr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've put a more extensive set of tests in crd_utils_test.go

Comment on lines +759 to +761
if ipb.CIDR == "" {
return "field 'cidr' is required in an ipBlock", false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We can keep this, but I assume this is guaranteed by the OpenAPI spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it should already be guaranteed by openAPI, just trying to keep it on par with https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/networking/validation/validation.go#L219

pkg/controller/networkpolicy/validate_test.go Show resolved Hide resolved
test/e2e/antreapolicy_test.go Outdated Show resolved Hide resolved
test/e2e/antreapolicy_test.go Outdated Show resolved Hide resolved
test/e2e/antreapolicy_test.go Outdated Show resolved Hide resolved
test/e2e/antreapolicy_test.go Outdated Show resolved Hide resolved
test/e2e/antreapolicy_test.go Outdated Show resolved Hide resolved
Signed-off-by: Dyanngg <dingyang@vmware.com>
Signed-off-by: Dyanngg <dingyang@vmware.com>
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/release-note Indicates a PR that should be included in release notes. area/network-policy Issues or PRs related to network policies. kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add except to ipBlock
2 participants