From 558e82f8169553ed28e0c78f8abe6d8fc2810af0 Mon Sep 17 00:00:00 2001 From: APPLE Date: Mon, 3 Aug 2026 00:26:48 +0530 Subject: [PATCH 1/2] fix(yurtmanager): prevent nil map panic in hubleader controller when LeaderNodeLabelSelector is nil --- .../hubleader/hubleader_controller.go | 6 ++- .../hubleader/hubleader_controller_test.go | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pkg/yurtmanager/controller/hubleader/hubleader_controller.go b/pkg/yurtmanager/controller/hubleader/hubleader_controller.go index c387937ac64..810c46aadce 100644 --- a/pkg/yurtmanager/controller/hubleader/hubleader_controller.go +++ b/pkg/yurtmanager/controller/hubleader/hubleader_controller.go @@ -176,8 +176,10 @@ func (r *ReconcileHubLeader) reconcileHubLeader(ctx context.Context, nodepool *a // Set match labels matchLabels := make(map[string]string) if nodepool.Spec.LeaderElectionStrategy == string(appsv1beta2.ElectionStrategyMark) { - // Add mark strategy match labels - matchLabels = nodepool.Spec.LeaderNodeLabelSelector + // Add mark strategy match labels safely without overwriting matchLabels with a potential nil map + for k, v := range nodepool.Spec.LeaderNodeLabelSelector { + matchLabels[k] = v + } } matchLabels[projectinfo.GetNodePoolLabel()] = nodepool.GetName() diff --git a/pkg/yurtmanager/controller/hubleader/hubleader_controller_test.go b/pkg/yurtmanager/controller/hubleader/hubleader_controller_test.go index b347b967c79..db8e0ad0ffe 100644 --- a/pkg/yurtmanager/controller/hubleader/hubleader_controller_test.go +++ b/pkg/yurtmanager/controller/hubleader/hubleader_controller_test.go @@ -328,6 +328,48 @@ func TestReconcile(t *testing.T) { }, expectErr: false, }, + "mark election strategy with nil LeaderNodeLabelSelector": { + pool: &appsv1beta2.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + }, + Spec: appsv1beta2.NodePoolSpec{ + Type: appsv1beta2.Edge, + Labels: map[string]string{ + "region": "hangzhou", + }, + LeaderElectionStrategy: string(appsv1beta2.ElectionStrategyMark), + LeaderNodeLabelSelector: nil, + LeaderReplicas: 1, + EnableLeaderElection: true, + }, + }, + expectedNodePool: &appsv1beta2.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + }, + Spec: appsv1beta2.NodePoolSpec{ + Type: appsv1beta2.Edge, + Labels: map[string]string{ + "region": "hangzhou", + }, + LeaderElectionStrategy: string(appsv1beta2.ElectionStrategyMark), + LeaderNodeLabelSelector: nil, + EnableLeaderElection: true, + LeaderReplicas: 1, + }, + Status: appsv1beta2.NodePoolStatus{ + LeaderEndpoints: []appsv1beta2.Leader{ + { + NodeName: "ready with internal IP", + Address: "10.0.0.1", + }, + }, + LeaderNum: 1, + }, + }, + expectErr: false, + }, "no potential leaders in hangzhou with mark strategy": { pool: &appsv1beta2.NodePool{ ObjectMeta: metav1.ObjectMeta{ From d327ea7620b54371ddb696c5a0242d728458c78b Mon Sep 17 00:00:00 2001 From: Nishant Date: Sat, 15 Aug 2026 20:30:17 +0530 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- pkg/yurtmanager/controller/hubleader/hubleader_controller.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/yurtmanager/controller/hubleader/hubleader_controller.go b/pkg/yurtmanager/controller/hubleader/hubleader_controller.go index 810c46aadce..03fe8361548 100644 --- a/pkg/yurtmanager/controller/hubleader/hubleader_controller.go +++ b/pkg/yurtmanager/controller/hubleader/hubleader_controller.go @@ -177,9 +177,7 @@ func (r *ReconcileHubLeader) reconcileHubLeader(ctx context.Context, nodepool *a matchLabels := make(map[string]string) if nodepool.Spec.LeaderElectionStrategy == string(appsv1beta2.ElectionStrategyMark) { // Add mark strategy match labels safely without overwriting matchLabels with a potential nil map - for k, v := range nodepool.Spec.LeaderNodeLabelSelector { - matchLabels[k] = v - } + maps.Copy(matchLabels, nodepool.Spec.LeaderNodeLabelSelector) } matchLabels[projectinfo.GetNodePoolLabel()] = nodepool.GetName()