Skip to content

Commit

Permalink
Merge pull request #3356 from fabriziopandini/fix-matchClusterConfigu…
Browse files Browse the repository at this point in the history
…ration

🐛 KCP: fix problem in rollout detection logic when ClusterConfiguration is nil
  • Loading branch information
k8s-ci-robot authored Jul 20, 2020
2 parents 9a5ef88 + bb05bd2 commit 545f589
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
32 changes: 23 additions & 9 deletions controlplane/kubeadm/internal/machinefilters/machine_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,31 @@ func MatchesKubeadmBootstrapConfig(ctx context.Context, c client.Client, kcp *co
// Users should use KCP.Spec.UpgradeAfter field to force a rollout in this case.
func matchClusterConfiguration(kcp *controlplanev1.KubeadmControlPlane, machine *clusterv1.Machine) bool {
machineClusterConfigStr, ok := machine.GetAnnotations()[controlplanev1.KubeadmClusterConfigurationAnnotation]
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
}
return reflect.DeepEqual(machineClusterConfig, kcp.Spec.KubeadmConfigSpec.ClusterConfiguration)
if !ok {
// We don't have enough information to make a decision; don't' trigger a roll out.
return true
}

machineClusterConfig := &kubeadmv1.ClusterConfiguration{}
// ClusterConfiguration annotation is not correct, only solution is to rollout.
// The call to json.Unmarshal has to take a pointer to the pointer struct defined above,
// otherwise we won't be able to handle a nil ClusterConfiguration (that is serialized into "null").
// See https://github.com/kubernetes-sigs/cluster-api/issues/3353.
if err := json.Unmarshal([]byte(machineClusterConfigStr), &machineClusterConfig); err != nil {
return false
}

// If any of the compared values are nil, treat them the same as an empty ClusterConfiguration.
if machineClusterConfig == nil {
machineClusterConfig = &kubeadmv1.ClusterConfiguration{}
}
kcpLocalClusterConfiguration := kcp.Spec.KubeadmConfigSpec.ClusterConfiguration
if kcpLocalClusterConfiguration == nil {
kcpLocalClusterConfiguration = &kubeadmv1.ClusterConfiguration{}
}

// We don't have enough information to make a decision; don't' trigger a roll out.
return true
// Compare and return.
return reflect.DeepEqual(machineClusterConfig, kcpLocalClusterConfiguration)
}

// matchInitOrJoinConfiguration verifies if KCP and machine InitConfiguration or JoinConfiguration matches.
Expand Down
16 changes: 16 additions & 0 deletions controlplane/kubeadm/internal/machinefilters/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 545f589

Please sign in to comment.