Unverified Commit d4ac54ad authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #66307 from guoshimin/fixnilmap

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. fix a panic due to assignment to nil map **What this PR does / why we need it**: fix a panic due to assignment to nil map **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: **Special notes for your reviewer**: **Release note**: ```release-note Fixed a panic in the node status update logic when existing node has nil labels. ```
parents b1d97e5b e8cd28ae
...@@ -162,6 +162,9 @@ func (kl *Kubelet) updateDefaultLabels(initialNode, existingNode *v1.Node) bool ...@@ -162,6 +162,9 @@ func (kl *Kubelet) updateDefaultLabels(initialNode, existingNode *v1.Node) bool
} }
var needsUpdate bool = false var needsUpdate bool = false
if existingNode.Labels == nil {
existingNode.Labels = make(map[string]string)
}
//Set default labels but make sure to not set labels with empty values //Set default labels but make sure to not set labels with empty values
for _, label := range defaultLabels { for _, label := range defaultLabels {
if _, hasInitialValue := initialNode.Labels[label]; !hasInitialValue { if _, hasInitialValue := initialNode.Labels[label]; !hasInitialValue {
......
...@@ -1414,6 +1414,33 @@ func TestUpdateDefaultLabels(t *testing.T) { ...@@ -1414,6 +1414,33 @@ func TestUpdateDefaultLabels(t *testing.T) {
kubeletapis.LabelArch: "new-arch", kubeletapis.LabelArch: "new-arch",
}, },
}, },
{
name: "not panic when existing node has nil labels",
initialNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
kubeletapis.LabelHostname: "new-hostname",
kubeletapis.LabelZoneFailureDomain: "new-zone-failure-domain",
kubeletapis.LabelZoneRegion: "new-zone-region",
kubeletapis.LabelInstanceType: "new-instance-type",
kubeletapis.LabelOS: "new-os",
kubeletapis.LabelArch: "new-arch",
},
},
},
existingNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{},
},
needsUpdate: true,
finalLabels: map[string]string{
kubeletapis.LabelHostname: "new-hostname",
kubeletapis.LabelZoneFailureDomain: "new-zone-failure-domain",
kubeletapis.LabelZoneRegion: "new-zone-region",
kubeletapis.LabelInstanceType: "new-instance-type",
kubeletapis.LabelOS: "new-os",
kubeletapis.LabelArch: "new-arch",
},
},
} }
for _, tc := range cases { for _, tc := range cases {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment