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.
```
This commit is contained in:
Kubernetes Submit Queue 2018-07-18 14:34:29 -07:00 committed by GitHub
commit d4ac54ad64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -162,6 +162,9 @@ func (kl *Kubelet) updateDefaultLabels(initialNode, existingNode *v1.Node) bool
}
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
for _, label := range defaultLabels {
if _, hasInitialValue := initialNode.Labels[label]; !hasInitialValue {

View File

@ -1414,6 +1414,33 @@ func TestUpdateDefaultLabels(t *testing.T) {
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 {