From d5157d0673e3b4aa04ff788400ba840cb4bfa53f Mon Sep 17 00:00:00 2001 From: "ben.zhang" Date: Fri, 24 Apr 2026 15:45:06 +0800 Subject: [PATCH] fix: prevent nil map panic when updating node annotations and labels --- pkg/ccm/instance.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/ccm/instance.go b/pkg/ccm/instance.go index 3b70cd7..d83dad6 100644 --- a/pkg/ccm/instance.go +++ b/pkg/ccm/instance.go @@ -66,6 +66,10 @@ func (i *instances) NodeAddressesByProviderID(ctx context.Context, providerID st // 20240117 node updates Annotation in real time and compatible with Cpu bundled core if len(res.Data.Annotations) != 0 { + // init annotations map if nil + if nodeAnnotations.ObjectMeta.Annotations == nil { + nodeAnnotations.ObjectMeta.Annotations = make(map[string]string) + } for _, annotation := range res.Data.Annotations { for key, value := range annotation { nodeAnnotations.ObjectMeta.Annotations[key] = value @@ -196,6 +200,10 @@ func (i *instances) InstanceTypeByProviderID(ctx context.Context, providerID str // set labels returnInstanceTypeValue := "" if len(res.Data.Labels) != 0 { + // init labels map if nil + if nodeLabels.ObjectMeta.Labels == nil { + nodeLabels.ObjectMeta.Labels = make(map[string]string) + } for _, label := range res.Data.Labels { for key, value := range label { if key == "node.kubernetes.io/instance-type" { @@ -219,6 +227,10 @@ func (i *instances) InstanceTypeByProviderID(ctx context.Context, providerID str // set Annotations nodeAnnotations, err := i.k8sClient.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{}) if len(res.Data.Annotations) != 0 { + // init annotations map if nil + if nodeAnnotations.ObjectMeta.Annotations == nil { + nodeAnnotations.ObjectMeta.Annotations = make(map[string]string) + } for _, annotation := range res.Data.Annotations { for key, value := range annotation { nodeAnnotations.ObjectMeta.Annotations[key] = value