From 347b9ebbbd58f5724ced6604c8312a1aefc28e83 Mon Sep 17 00:00:00 2001 From: Yongxiu Cui Date: Tue, 16 Nov 2021 00:21:38 +0000 Subject: [PATCH] Fix adding appended list directly causing changing previous value * Added new unit test to cover this case, original this test case would fail. --- .../v1beta1/kubeadm_control_plane_webhook.go | 6 +++++- .../kubeadm_control_plane_webhook_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook.go b/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook.go index 63338bd41df9..0c78691c376c 100644 --- a/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook.go +++ b/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook.go @@ -431,7 +431,11 @@ func paths(path []string, diff map[string]interface{}) [][]string { for key, m := range diff { nested, ok := m.(map[string]interface{}) if !ok { - allPaths = append(allPaths, append(path, key)) + // We have to use a copy of path, because otherwise the slice we append to + // allPaths would be overwritten in another iteration. + tmp := make([]string, len(path)) + copy(tmp, path) + allPaths = append(allPaths, append(tmp, key)) continue } allPaths = append(allPaths, paths(append(path, key), nested)...) diff --git a/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook_test.go b/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook_test.go index b999b3ba1f88..38f257d6778e 100644 --- a/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook_test.go +++ b/controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook_test.go @@ -1045,6 +1045,23 @@ func TestPaths(t *testing.T) { diff: map[string]interface{}{}, expected: [][]string{}, }, + { + name: "long recursive check with two keys", + diff: map[string]interface{}{ + "spec": map[string]interface{}{ + "kubeadmConfigSpec": map[string]interface{}{ + "clusterConfiguration": map[string]interface{}{ + "version": "v2.0.1", + "abc": "d", + }, + }, + }, + }, + expected: [][]string{ + {"spec", "kubeadmConfigSpec", "clusterConfiguration", "version"}, + {"spec", "kubeadmConfigSpec", "clusterConfiguration", "abc"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {