Skip to content

Conversation

squeed
Copy link
Contributor

@squeed squeed commented Oct 1, 2024

This change stops the manual "splitting" of Except cidrs within ToCIDRSet rules. Rather, it uses the DoesNotExist label selector on any excepted CIDRs.

Previously, any entries within the Except block would cause the selected CIDR to be "split" on bit boundaries. This example policy would cause 8 match entries to be created, rather than 1:

toCIDRSet:
- cidr: 10.0.0.0/8
  except: 
    - 10.0.0.0/16

Starting from 3887fcc, CIDR identities are no longer special within the policymap. This means that CIDR selectors have the exactly expected effect, and we no longer synthesize smaller entries in the event of a covering policy rule.

Now that CIDR selectors work exactly as expected, we can exploit this fact, along with the LPM nature of the ipcache, to implement Except without splitting any CIDRs.

The above example now produces the label selector string cidr:10.0.0.0/8 !cidr:10.0.0.0/16 which means "select all identities with the key cidr:10.0.0.0/8 but not with the key cidr:10.0.0.0/16". This means that all identities contained in prefix 10.0.0.0/16 will never be selected by the policymap, and will thus be dropped.

This also fixes a (never shipped) bug introduced in #33441 where Except blocks stopped working with CIDRGroupRef rules.

CIDRGroup Except blocks now produce fewer PolicyMap entries, improving scalability.

@squeed squeed added release-note/minor This PR changes functionality that users may find relevant to operating Cilium. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies. labels Oct 1, 2024
@squeed squeed requested review from a team as code owners October 1, 2024 08:04
@squeed squeed requested review from learnitall and doniacld October 1, 2024 08:04
@squeed
Copy link
Contributor Author

squeed commented Oct 1, 2024

/test

Copy link
Contributor

@learnitall learnitall left a comment

Choose a reason for hiding this comment

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

LGTM!

@squeed squeed added the needs/e2e-test This issue is not covered by existing CI tests, but should be. label Oct 9, 2024
@squeed
Copy link
Contributor Author

squeed commented Oct 9, 2024

This really needs an e2e test, for which #35314 is first necessary.

@squeed squeed force-pushed the cidr-except-cleverness branch from 153bba8 to e1410cb Compare October 11, 2024 09:54
@squeed squeed requested a review from a team as a code owner October 11, 2024 09:54
@squeed squeed requested a review from christarazi October 11, 2024 09:54
@squeed
Copy link
Contributor Author

squeed commented Oct 11, 2024

e2e test added; this is ready for review.

@squeed squeed removed the needs/e2e-test This issue is not covered by existing CI tests, but should be. label Oct 11, 2024
@squeed
Copy link
Contributor Author

squeed commented Oct 11, 2024

/test

@squeed
Copy link
Contributor Author

squeed commented Oct 14, 2024

Whoops, failing a unit test. Hold please.

@squeed squeed force-pushed the cidr-except-cleverness branch from e1410cb to 0a72272 Compare October 14, 2024 13:31
@squeed
Copy link
Contributor Author

squeed commented Oct 18, 2024

/test

This change stops the manual "splitting" of Except cidrs within
ToCIDRSet rules. Rather, it uses the DoesNotExist label selector on any
excepted CIDRs.

Previously, any entries within the Except block would
cause the selected CIDR to be "split" on bit boundaries. This example
policy would cause *8* match entries to be created, rather than 1:

toCIDRSet:
- cidr: 10.0.0.0/8
  except:
    - 10.0.0.0/16

Starting from 3887fcc, CIDR identities are no longer special within
the policymap. This means that CIDR selectors have the exactly expected
effect, and we no longer synthesize smaller entries in the event of a
covering policy rule.

Now that CIDR selectors work exactly as expected, we can exploit this
fact, along with the LPM nature of the ipcache, to implement Except
without splitting any CIDRs.

The above example now produces the label selector string
"cidr:10.0.0.0/8 !cidr:10.0.0.0/16" which means "select all identities
with the key cidr:10.0.0.0/8 but not with the key cidr:10.0.0.0/16".
This means that all identities contained in prefix 10.0.0.0/16 will
*never* be selected by the policymap, and will thus be dropped.

This also fixes a (never shipped) bug introduced in cilium#33441 where Except
blocks stopped working with CIDRGroupRef rules.

Signed-off-by: Casey Callendrello <cdc@isovalent.com>
This creates a CiliumNetworkPolicy equivalent to another CIDR-based
scenario, but uses a CiliumCIDRGroup instead of specifying the CIDR
directly.

This ensures that both CIDRGroups as well as Except fields work as
expected.

Signed-off-by: Casey Callendrello <cdc@isovalent.com>
@squeed squeed force-pushed the cidr-except-cleverness branch from 0a72272 to 6138677 Compare October 23, 2024 14:45
@squeed
Copy link
Contributor Author

squeed commented Oct 23, 2024

/test

@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Oct 23, 2024
@squeed squeed added this pull request to the merge queue Oct 24, 2024
Merged via the queue into cilium:main with commit 78455a5 Oct 24, 2024
63 checks passed
@squeed squeed deleted the cidr-except-cleverness branch October 24, 2024 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/minor This PR changes functionality that users may find relevant to operating Cilium. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants