Skip to content

fix: protect cidrMap with lock#98

Open
iamjaeholee wants to merge 4 commits intokubernetes-sigs:mainfrom
iamjaeholee:fix/concurrent-access-to-cidr-map
Open

fix: protect cidrMap with lock#98
iamjaeholee wants to merge 4 commits intokubernetes-sigs:mainfrom
iamjaeholee:fix/concurrent-access-to-cidr-map

Conversation

@iamjaeholee
Copy link
Copy Markdown

What this PR does / why we need it

Fixes concurrent access to multiCIDRRangeAllocator.cidrMap that was not protected by lock.

Closes #86

Changes

  • Added Protected by lock. comment to the cidrMap field declaration as requested in the issue.
  • Added lock.Lock()/lock.Unlock() around cidrMap accesses in NewMultiCIDRRangeAllocator that were identified as unprotected by the protectedby linter.
  • Added lock protection in tests (TestSyncClusterCIDRDeleteWithNodesAssociated, TestMultiCIDRSetDataRace) for consistent and race-free access.

How to verify

Run the protectedby linter to confirm no unprotected accesses remain:

go install github.com/mneverov/protectedby@latest
protectedby ./...

Run tests with the race detector:

go test -race ./pkg/controller/ipam/...

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: iamjaeholee
Once this PR has been reviewed and has the lgtm label, please assign thockin for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Welcome @iamjaeholee!

It looks like this is your first PR to kubernetes-sigs/node-ipam-controller 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/node-ipam-controller has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 3, 2026
Add lock/unlock around rangeAllocator.cidrMap accesses in
TestMultiCIDRAllocateOrOccupyCIDRSuccess, TestMultiCIDRAllocateOrOccupyCIDRFailure,
and TestMultiCIDRReleaseCIDRSuccess to satisfy the protectedby linter.
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Apr 3, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. and removed cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 3, 2026
Private helper functions that access cidrMap now receive it as a
parameter instead of accessing r.cidrMap directly. Lock-holding callers
(AllocateOrOccupyCIDR, ReleaseCIDR, reconcileCreate, reconcileBootstrap,
reconcileDelete, NewMultiCIDRRangeAllocator) pass r.cidrMap within
their lock scope, satisfying the protectedby linter without introducing
deadlocks.
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 3, 2026
@iamjaeholee iamjaeholee marked this pull request as draft April 3, 2026 01:30
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 3, 2026
@iamjaeholee iamjaeholee force-pushed the fix/concurrent-access-to-cidr-map branch from d06b80b to 91c6f7b Compare April 3, 2026 01:31
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 3, 2026
@iamjaeholee iamjaeholee marked this pull request as ready for review April 3, 2026 03:56
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 3, 2026
@k8s-ci-robot k8s-ci-robot requested review from aojea and mneverov April 3, 2026 03:56
// lock guards cidrMap to avoid races in CIDR allocation.
lock *sync.Mutex
// cidrMap maps ClusterCIDR labels to internal ClusterCIDR objects.
// cidrMap maps ClusterCIDR labels to internal ClusterCIDR objects. Protected by lock.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this comment is necessary.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

okay. i will delete it.


// occupyCIDRs marks node.PodCIDRs[...] as used in allocator's tracked cidrSet.
func (r *multiCIDRRangeAllocator) occupyCIDRs(logger klog.Logger, node *corev1.Node) error {
func (r *multiCIDRRangeAllocator) occupyCIDRs(logger klog.Logger, node *corev1.Node, cidrMap map[string][]*cidrset.ClusterCIDR) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A comment on these functions would be more useful, to point out that access to cidrMap should be protected by a lock before passing in for this and the similar functions below.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think so too. i will make a comment on the functions

@stmcginnis
Copy link
Copy Markdown
Contributor

I also wonder if it would be better to just use sync.Map at this point, with all the different places this is touching.

Remove "Protected by lock." from cidrMap field comment since the
locking semantics are now documented on each function that accesses
cidrMap. Add "requires the caller to hold r.lock" comments to all
private helper functions that accept cidrMap as a parameter.
@iamjaeholee
Copy link
Copy Markdown
Author

Thanks for the suggestion! I think keeping the mutex approach makes more sense here. The map values are slices ([]*cidrset.ClusterCIDR) that also need synchronized access, and several operations range over multiple keys atomically — which is harder to guarantee with sync.Map. The existing r.lock covers both the map and the slice mutations together, so I'd prefer to keep it as is.

@stmcginnis
Copy link
Copy Markdown
Contributor

Updates look good - thanks!

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent access to multiCIDRRangeAllocator.cidrMap is not protected

3 participants