Always check if default labels on node need to be updated in kubelet
This commit is contained in:
@@ -43,6 +43,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
core "k8s.io/client-go/testing"
|
||||
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
|
||||
"k8s.io/kubernetes/pkg/kubelet/cm"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
"k8s.io/kubernetes/pkg/kubelet/util/sliceutils"
|
||||
@@ -675,8 +676,15 @@ func TestRegisterWithApiServer(t *testing.T) {
|
||||
kubeClient.AddReactor("get", "nodes", func(action core.Action) (bool, runtime.Object, error) {
|
||||
// Return an existing (matching) node on get.
|
||||
return true, &v1.Node{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname},
|
||||
Spec: v1.NodeSpec{ExternalID: testKubeletHostname},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testKubeletHostname,
|
||||
Labels: map[string]string{
|
||||
kubeletapis.LabelHostname: testKubeletHostname,
|
||||
kubeletapis.LabelOS: goruntime.GOOS,
|
||||
kubeletapis.LabelArch: goruntime.GOARCH,
|
||||
},
|
||||
},
|
||||
Spec: v1.NodeSpec{ExternalID: testKubeletHostname},
|
||||
}, nil
|
||||
})
|
||||
kubeClient.AddReactor("*", "*", func(action core.Action) (bool, runtime.Object, error) {
|
||||
@@ -731,7 +739,13 @@ func TestTryRegisterWithApiServer(t *testing.T) {
|
||||
|
||||
newNode := func(cmad bool, externalID string) *v1.Node {
|
||||
node := &v1.Node{
|
||||
ObjectMeta: metav1.ObjectMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: map[string]string{
|
||||
kubeletapis.LabelHostname: testKubeletHostname,
|
||||
kubeletapis.LabelOS: goruntime.GOOS,
|
||||
kubeletapis.LabelArch: goruntime.GOARCH,
|
||||
},
|
||||
},
|
||||
Spec: v1.NodeSpec{
|
||||
ExternalID: externalID,
|
||||
},
|
||||
@@ -961,3 +975,164 @@ func TestUpdateNewNodeStatusTooLargeReservation(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, apiequality.Semantic.DeepEqual(expectedNode.Status.Allocatable, updatedNode.Status.Allocatable), "%s", diff.ObjectDiff(expectedNode.Status.Allocatable, updatedNode.Status.Allocatable))
|
||||
}
|
||||
|
||||
func TestUpdateDefaultLabels(t *testing.T) {
|
||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
initialNode *v1.Node
|
||||
existingNode *v1.Node
|
||||
needsUpdate bool
|
||||
finalLabels map[string]string
|
||||
}{
|
||||
{
|
||||
name: "make sure default labels exist",
|
||||
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{
|
||||
Labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
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",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "make sure default labels are up to date",
|
||||
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{
|
||||
Labels: map[string]string{
|
||||
kubeletapis.LabelHostname: "old-hostname",
|
||||
kubeletapis.LabelZoneFailureDomain: "old-zone-failure-domain",
|
||||
kubeletapis.LabelZoneRegion: "old-zone-region",
|
||||
kubeletapis.LabelInstanceType: "old-instance-type",
|
||||
kubeletapis.LabelOS: "old-os",
|
||||
kubeletapis.LabelArch: "old-arch",
|
||||
},
|
||||
},
|
||||
},
|
||||
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",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "make sure existing labels do not get deleted",
|
||||
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{
|
||||
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",
|
||||
"please-persist": "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
needsUpdate: false,
|
||||
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",
|
||||
"please-persist": "foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no update needed",
|
||||
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{
|
||||
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",
|
||||
},
|
||||
},
|
||||
},
|
||||
needsUpdate: false,
|
||||
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 {
|
||||
defer testKubelet.Cleanup()
|
||||
kubelet := testKubelet.kubelet
|
||||
|
||||
needsUpdate := kubelet.updateDefaultLabels(tc.initialNode, tc.existingNode)
|
||||
assert.Equal(t, tc.needsUpdate, needsUpdate, tc.name)
|
||||
assert.Equal(t, tc.finalLabels, tc.existingNode.Labels, tc.name)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user