Skip to content

Conversation

squeed
Copy link
Contributor

@squeed squeed commented Sep 19, 2023

When printing Labels (e.g. by cilium identity list), CIDR labels are de-expanded to only show the relevant ones. For example, the CIDR label 1.1.1.0/24 is under-the-hood expanded to 0.0.0.0/0, 0.0.0.0/1, ..., 1.1.0.0/23, 1.1.1.0/24. Then, when the identity is printed for human consumption, the CIDR labels are de-expanded. However, this code did not anticipate a single identity having multiple "leaf" CIDR labels.

So, change the printing logic to output all interesting CIDRs. This adds a O(N^2) check, but given that N is never over ~100, it shouldn't be an issue.

Note that this output is only for human consumption; it is not persisted or used as an API.

@squeed squeed requested review from a team as code owners September 19, 2023 14:15
@squeed squeed requested a review from nathanjsweet September 19, 2023 14:15
@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Sep 19, 2023
@squeed squeed requested a review from jrajahalme September 19, 2023 14:15
@squeed squeed added the release-note/minor This PR changes functionality that users may find relevant to operating Cilium. label Sep 19, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Sep 19, 2023
@squeed squeed force-pushed the label-multiple-cidrs branch from 33919a5 to 4829b40 Compare September 20, 2023 08:34
@squeed
Copy link
Contributor Author

squeed commented Sep 25, 2023

/test

@squeed squeed force-pushed the label-multiple-cidrs branch from 4829b40 to bb33022 Compare September 25, 2023 11:52
@squeed
Copy link
Contributor Author

squeed commented Sep 25, 2023

/test

@squeed
Copy link
Contributor Author

squeed commented Sep 25, 2023

Confirmed that this works as expected:

root@kind-control-plane:/home/cilium# cilium identity list
ID         LABELS
1          cidr:10.244.0.80/32
           cidr:172.19.0.2/32
           cidr:fc00:c111::2/128
           reserved:host
           reserved:kube-apiserver

@nathanjsweet nathanjsweet added the dont-merge/needs-rebase This PR needs to be rebased because it has merge conflicts. label Oct 6, 2023
@squeed squeed force-pushed the label-multiple-cidrs branch from bb33022 to b72b0fb Compare October 19, 2023 11:30
@squeed
Copy link
Contributor Author

squeed commented Oct 19, 2023

Rebased, refactored to use a real data structure. This improves performance over the existing, incorrect method as well as my previous naive attempt:

     │ GetPrintableModelOrig │      GetPrintableModelLeafCIDR      │       GetPrintableModelNaive        │
     │        sec/op         │   sec/op     vs base                │   sec/op     vs base                │
*-12            167.0µ ± 17%   108.8µ ± 3%  -34.84% (p=0.000 n=10)   285.4µ ± 5%  +70.93% (p=0.000 n=10)

     │ GetPrintableModelOrig │     GetPrintableModelLeafCIDR      │       GetPrintableModelNaive       │
     │       allocs/op       │ allocs/op   vs base                │ allocs/op   vs base                │
*-12             1440.0 ± 0%   557.0 ± 0%  -61.32% (p=0.000 n=10)   189.0 ± 0%  -86.88% (p=0.000 n=10)

This reduces time from 167µs to 108µs. Fewer allocs, too.

@squeed squeed requested a review from joestringer October 19, 2023 11:32
@squeed squeed force-pushed the label-multiple-cidrs branch from b72b0fb to e49753d Compare October 20, 2023 11:26
@squeed
Copy link
Contributor Author

squeed commented Oct 20, 2023

Code golf: got it even faster :-)

                     │   orig.txt    │               new.txt               │
                     │    sec/op     │   sec/op     vs base                │
GetPrintableModel-12   166.98µ ± 17%   88.62µ ± 3%  -46.93% (p=0.000 n=10)

                     │  orig.txt   │              new.txt               │
                     │  allocs/op  │ allocs/op   vs base                │
GetPrintableModel-12   1440.0 ± 0%   401.0 ± 0%  -72.15% (p=0.000 n=10)

@squeed squeed removed the dont-merge/needs-rebase This PR needs to be rebased because it has merge conflicts. label Oct 20, 2023
@squeed squeed force-pushed the label-multiple-cidrs branch from e49753d to 9945ca0 Compare October 20, 2023 12:09
Copy link
Member

@joestringer joestringer left a comment

Choose a reason for hiding this comment

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

Needs a rebase but otherwise LGTM. Nice one 🚀

@joestringer joestringer added the dont-merge/needs-rebase This PR needs to be rebased because it has merge conflicts. label Oct 20, 2023
@joestringer joestringer removed the request for review from jrajahalme October 20, 2023 22:11
@joestringer
Copy link
Member

We might consider adding affects/1.14 and other versions for tracking purposes.

This package implemented a single function and caused import loops. So,
just move it up a level to get rid of the loop.

Signed-off-by: Casey Callendrello <cdc@isovalent.com>
When printing Labels (e.g. by `cilium identity list`), CIDR labels are
de-expanded to only show the relevant ones. For example, the CIDR label
1.1.1.0/24 is under-the-hood expanded to 0.0.0.0/0, 0.0.0.0/1, ...,
1.1.1.0/23, 1.1.1.0/24. Then, when the identity is printed for human
consumption, the CIDR labels are de-expanded. However, this code did not
anticipate a single identity having multiple "leaf" CIDR labels.

So, change the printing logic to output all interesting CIDRs. This adds
a small utility datatype to efficiently prune a tree of cidrs to the
leaves. It winds up being about 30% faster than the original, incorrect
version.

Signed-off-by: Casey Callendrello <cdc@isovalent.com>
@squeed squeed force-pushed the label-multiple-cidrs branch from 9945ca0 to 6967cf1 Compare October 23, 2023 12:45
@squeed squeed added affects/v1.13 This issue affects v1.13 branch affects/v1.14 This issue affects v1.14 branch and removed dont-merge/needs-rebase This PR needs to be rebased because it has merge conflicts. labels Oct 23, 2023
@squeed
Copy link
Contributor Author

squeed commented Oct 23, 2023

/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, 2023
@dylandreimerink dylandreimerink merged commit 1ec121c into cilium:main Oct 24, 2023
@tklauser tklauser added backport/author The backport will be carried out by the author of the PR. needs-backport/1.14 and removed affects/v1.14 This issue affects v1.14 branch labels Oct 26, 2023
@tklauser tklauser removed the affects/v1.13 This issue affects v1.13 branch label Oct 26, 2023
@julianwiedmann
Copy link
Member

Is anyone still planning to tackle the backports for v1.13 / v1.14, or can we downgrade them to affects/ ?

@squeed squeed added backport-pending/1.14 The backport for Cilium 1.14.x for this PR is in progress. and removed needs-backport/1.13 labels May 13, 2024
@squeed squeed added needs-backport/1.13 backport/author The backport will be carried out by the author of the PR. and removed backport/author The backport will be carried out by the author of the PR. needs-backport/1.13 labels May 13, 2024
@julianwiedmann julianwiedmann added backport-done/1.14 The backport for Cilium 1.14.x for this PR is done. and removed backport-pending/1.14 The backport for Cilium 1.14.x for this PR is in progress. labels May 15, 2024
@julianwiedmann
Copy link
Member

Downgrading the v1.13 label to affects ...

@julianwiedmann julianwiedmann added affects/v1.13 This issue affects v1.13 branch and removed backport-pending/1.13 labels Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects/v1.13 This issue affects v1.13 branch backport/author The backport will be carried out by the author of the PR. backport-done/1.14 The backport for Cilium 1.14.x for this PR is done. 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.
Projects
No open projects
Status: Needs backport from main
Status: Needs backport from main
Status: Released
Development

Successfully merging this pull request may close these issues.

6 participants