Merge pull request #79391 from danwinship/nodeaddresses-update-fix
Don't use strategic merge patch on Node.Status.Addresses
This commit is contained in:
@@ -2169,3 +2169,156 @@ func TestNodeStatusHasChanged(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateNodeAddresses(t *testing.T) {
|
||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||
defer testKubelet.Cleanup()
|
||||
kubelet := testKubelet.kubelet
|
||||
kubeClient := testKubelet.fakeKubeClient
|
||||
|
||||
existingNode := v1.Node{ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname}}
|
||||
kubeClient.ReactionChain = fake.NewSimpleClientset(&v1.NodeList{Items: []v1.Node{existingNode}}).ReactionChain
|
||||
|
||||
tests := []struct {
|
||||
Name string
|
||||
Before []v1.NodeAddress
|
||||
After []v1.NodeAddress
|
||||
}{
|
||||
{
|
||||
Name: "nil to populated",
|
||||
Before: nil,
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "empty to populated",
|
||||
Before: []v1.NodeAddress{},
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "populated to nil",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: nil,
|
||||
},
|
||||
{
|
||||
Name: "populated to empty",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: []v1.NodeAddress{},
|
||||
},
|
||||
{
|
||||
Name: "multiple addresses of same type, no change",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.3"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.3"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "1 InternalIP to 2 InternalIP",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "2 InternalIP to 1 InternalIP",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "2 InternalIP to 2 different InternalIP",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.3"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.4"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "2 InternalIP to reversed order",
|
||||
Before: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
After: []v1.NodeAddress{
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.2"},
|
||||
{Type: v1.NodeInternalIP, Address: "127.0.0.1"},
|
||||
{Type: v1.NodeHostName, Address: testKubeletHostname},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
oldNode := &v1.Node{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname},
|
||||
Spec: v1.NodeSpec{},
|
||||
Status: v1.NodeStatus{
|
||||
Addresses: test.Before,
|
||||
},
|
||||
}
|
||||
expectedNode := &v1.Node{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname},
|
||||
Spec: v1.NodeSpec{},
|
||||
Status: v1.NodeStatus{
|
||||
Addresses: test.After,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := kubeClient.CoreV1().Nodes().Update(oldNode)
|
||||
assert.NoError(t, err)
|
||||
kubelet.setNodeStatusFuncs = []func(*v1.Node) error{
|
||||
func(node *v1.Node) error {
|
||||
node.Status.Addresses = expectedNode.Status.Addresses
|
||||
return nil
|
||||
},
|
||||
}
|
||||
assert.NoError(t, kubelet.updateNodeStatus())
|
||||
|
||||
actions := kubeClient.Actions()
|
||||
lastAction := actions[len(actions)-1]
|
||||
assert.IsType(t, core.PatchActionImpl{}, lastAction)
|
||||
patchAction := lastAction.(core.PatchActionImpl)
|
||||
|
||||
updatedNode, err := applyNodeStatusPatch(oldNode, patchAction.GetPatch())
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, apiequality.Semantic.DeepEqual(updatedNode, expectedNode), "%s", diff.ObjectDiff(expectedNode, updatedNode))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user