From ab384e1c84eb219219be7282fe4cbb546485fddb Mon Sep 17 00:00:00 2001 From: Feruzjon Muyassarov Date: Mon, 20 Dec 2021 13:37:51 +0200 Subject: [PATCH] Fix adding appended list directly causing changing previous value Baclport of https://github.com/kubernetes-sigs/cluster-api/pull/5670 to v1alpha4 API in release-0.4 branch. --- .../v1alpha4/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/v1alpha4/kubeadm_control_plane_webhook.go b/controlplane/kubeadm/api/v1alpha4/kubeadm_control_plane_webhook.go index 42eca65980fc..ef1ea8ce4c34 100644 --- a/controlplane/kubeadm/api/v1alpha4/kubeadm_control_plane_webhook.go +++ b/controlplane/kubeadm/api/v1alpha4/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/v1alpha4/kubeadm_control_plane_webhook_test.go b/controlplane/kubeadm/api/v1alpha4/kubeadm_control_plane_webhook_test.go index bc22cdb193e8..0e36e05067d1 100644 --- a/controlplane/kubeadm/api/v1alpha4/kubeadm_control_plane_webhook_test.go +++ b/controlplane/kubeadm/api/v1alpha4/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) {