Skip to content

fix(yurtmanager): prevent nil map assignment panic in hubleader controller - #2720

Open
nishantbkl3345-ship-it wants to merge 2 commits into
openyurtio:masterfrom
nishantbkl3345-ship-it:fix/hubleader-nil-map-panic
Open

fix(yurtmanager): prevent nil map assignment panic in hubleader controller#2720
nishantbkl3345-ship-it wants to merge 2 commits into
openyurtio:masterfrom
nishantbkl3345-ship-it:fix/hubleader-nil-map-panic

Conversation

@nishantbkl3345-ship-it

Copy link
Copy Markdown

What this PR does / why we need it

Fixes a potential nil map assignment panic in hubleader_controller.

When nodepool.Spec.LeaderElectionStrategy is set to mark but nodepool.Spec.LeaderNodeLabelSelector is nil (or omitted), matchLabels = nodepool.Spec.LeaderNodeLabelSelector overwrote the non-nil matchLabels map with nil. Attempting to write matchLabels[projectinfo.GetNodePoolLabel()] = nodepool.GetName() subsequently triggered a runtime panic (panic: assignment to entry in nil map).

This PR safely iterates and copies key-value pairs from nodepool.Spec.LeaderNodeLabelSelector into matchLabels without replacing the non-nil map reference.

Which issue(s) this PR fixes

N/A

Special notes for your reviewer

  • Added unit test case mark election strategy with nil LeaderNodeLabelSelector to pkg/yurtmanager/controller/hubleader/hubleader_controller_test.go.
  • All package unit tests and race detection tests pass cleanly.

Copilot AI review requested due to automatic review settings August 2, 2026 19:12
@nishantbkl3345-ship-it
nishantbkl3345-ship-it requested a review from a team as a code owner August 2, 2026 19:12

Copilot AI left a comment

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.

Pull request overview

This PR fixes a potential runtime panic in the hubleader controller when using the mark leader election strategy with a nil/omitted LeaderNodeLabelSelector, and adds a unit test to prevent regressions.

Changes:

  • Avoids overwriting an initialized matchLabels map with a potentially nil LeaderNodeLabelSelector.
  • Copies selector key/value pairs into matchLabels instead of reassigning the map reference (preventing nil-map assignment panic).
  • Adds a unit test covering mark strategy with LeaderNodeLabelSelector: nil.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/yurtmanager/controller/hubleader/hubleader_controller.go Prevents nil-map assignment panic by copying selector labels into an initialized matchLabels map.
pkg/yurtmanager/controller/hubleader/hubleader_controller_test.go Adds a regression test for mark strategy with a nil LeaderNodeLabelSelector.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/yurtmanager/controller/hubleader/hubleader_controller.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

@nishantbkl3345-ship-it

Copy link
Copy Markdown
Author

@zhangzhenyuyu hey! please look upon the pr when you get a chance!

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.

2 participants