From 6d9699a82c6e08e3e257c2da18308b238fc78a0b Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Thu, 16 Jul 2020 17:41:21 +0200 Subject: [PATCH] fix matchClusterConfiguration --- .../internal/machinefilters/machine_filters.go | 10 ++++++++-- .../kubeadm/internal/machinefilters/util_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/controlplane/kubeadm/internal/machinefilters/machine_filters.go b/controlplane/kubeadm/internal/machinefilters/machine_filters.go index ca718f6eb817..bade7bb07c54 100644 --- a/controlplane/kubeadm/internal/machinefilters/machine_filters.go +++ b/controlplane/kubeadm/internal/machinefilters/machine_filters.go @@ -277,8 +277,14 @@ func matchClusterConfiguration(kcp *controlplanev1.KubeadmControlPlane, machine if ok { machineClusterConfig := &kubeadmv1.ClusterConfiguration{} // ClusterConfiguration annotation is not correct, only solution is to rollout. - if err := json.Unmarshal([]byte(machineClusterConfigStr), machineClusterConfig); err != nil { - return false + // NOTE: it is required to handle a nil ClusterConfiguration (that is serialized into "null") as a special case, + // because Unmarshal returns &kubeadmv1.ClusterConfiguration{}, not nil; see https://github.com/kubernetes-sigs/cluster-api/issues/3353 + if machineClusterConfigStr != "null" { + if err := json.Unmarshal([]byte(machineClusterConfigStr), machineClusterConfig); err != nil { + return false + } + } else { + machineClusterConfig = nil } return reflect.DeepEqual(machineClusterConfig, kcp.Spec.KubeadmConfigSpec.ClusterConfiguration) } diff --git a/controlplane/kubeadm/internal/machinefilters/util_test.go b/controlplane/kubeadm/internal/machinefilters/util_test.go index 21a5775a785f..69638a6090a9 100644 --- a/controlplane/kubeadm/internal/machinefilters/util_test.go +++ b/controlplane/kubeadm/internal/machinefilters/util_test.go @@ -90,6 +90,22 @@ func TestMatchClusterConfiguration(t *testing.T) { } g.Expect(matchClusterConfiguration(kcp, m)).To(gomega.BeFalse()) }) + t.Run("Return true if cluster configuration is nil (special case)", func(t *testing.T) { + g := gomega.NewWithT(t) + kcp := &controlplanev1.KubeadmControlPlane{ + Spec: controlplanev1.KubeadmControlPlaneSpec{ + KubeadmConfigSpec: bootstrapv1.KubeadmConfigSpec{}, + }, + } + m := &clusterv1.Machine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + controlplanev1.KubeadmClusterConfigurationAnnotation: "null", + }, + }, + } + g.Expect(matchClusterConfiguration(kcp, m)).To(gomega.BeTrue()) + }) } func TestGetAdjustedKcpConfig(t *testing.T) {