Skip to content

Commit 3257651

Browse files
authored
Merge pull request #12758 from sbueringer/pr-simplify-kcp-rollout
✨ KCP: compare ClusterConfiguration via KubeadmConfig instead of annotation on Machine
2 parents 0a4ba47 + 82d1066 commit 3257651

File tree

10 files changed

+273
-626
lines changed

10 files changed

+273
-626
lines changed

api/controlplane/kubeadm/v1beta2/kubeadm_control_plane_types.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ const (
4646
// SkipKubeProxyAnnotation annotation explicitly skips reconciling kube-proxy if set.
4747
SkipKubeProxyAnnotation = "controlplane.cluster.x-k8s.io/skip-kube-proxy"
4848

49-
// KubeadmClusterConfigurationAnnotation is a machine annotation that stores the json-marshalled string of KCP ClusterConfiguration.
50-
// This annotation is used to detect any changes in ClusterConfiguration and trigger machine rollout in KCP.
51-
KubeadmClusterConfigurationAnnotation = "controlplane.cluster.x-k8s.io/kubeadm-cluster-configuration"
52-
5349
// RemediationInProgressAnnotation is used to keep track that a KCP remediation is in progress, and more
5450
// specifically it tracks that the system is in between having deleted an unhealthy machine and recreating its replacement.
5551
// NOTE: if something external to CAPI removes this annotation the system cannot detect the above situation; this can lead to

controlplane/kubeadm/internal/controllers/controller_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,9 @@ func TestReconcileCertificateExpiries(t *testing.T) {
983983

984984
cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault})
985985
kcp := &controlplanev1.KubeadmControlPlane{
986+
Spec: controlplanev1.KubeadmControlPlaneSpec{
987+
Version: "v1.30.0",
988+
},
986989
Status: controlplanev1.KubeadmControlPlaneStatus{
987990
Initialization: controlplanev1.KubeadmControlPlaneInitializationStatus{
988991
ControlPlaneInitialized: ptr.To(true),

controlplane/kubeadm/internal/controllers/helpers.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ func (r *KubeadmControlPlaneReconciler) updateMachine(ctx context.Context, machi
347347
return updatedMachine, nil
348348
}
349349

350+
// kubeadmClusterConfigurationAnnotation is an annotation that was set in Cluster API <= v1.11.
351+
// Starting with Cluster API v1.12 we remove it from existing Machines.
352+
//
353+
// Deprecated: This constant and corresponding cleanup code can be removed once we don't support upgrades from Cluster API v1.12 anymore.
354+
const kubeadmClusterConfigurationAnnotation = "controlplane.cluster.x-k8s.io/kubeadm-cluster-configuration"
355+
350356
// computeDesiredMachine computes the desired Machine.
351357
// This Machine will be used during reconciliation to:
352358
// * create a new Machine
@@ -376,15 +382,6 @@ func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev
376382
machineName = generatedMachineName
377383
version = kcp.Spec.Version
378384

379-
// Machine's bootstrap config may be missing ClusterConfiguration if it is not the first machine in the control plane.
380-
// We store ClusterConfiguration as annotation here to detect any changes in KCP ClusterConfiguration and rollout the machine if any.
381-
// Nb. This annotation is read when comparing the KubeadmConfig to check if a machine needs to be rolled out.
382-
clusterConfigurationAnnotation, err := internal.ClusterConfigurationToMachineAnnotationValue(&kcp.Spec.KubeadmConfigSpec.ClusterConfiguration)
383-
if err != nil {
384-
return nil, err
385-
}
386-
annotations[controlplanev1.KubeadmClusterConfigurationAnnotation] = clusterConfigurationAnnotation
387-
388385
// In case this machine is being created as a consequence of a remediation, then add an annotation
389386
// tracking remediating data.
390387
// NOTE: This is required in order to track remediation retries.
@@ -397,24 +394,8 @@ func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev
397394
machineUID = existingMachine.UID
398395
version = existingMachine.Spec.Version
399396

400-
// For existing machine only set the ClusterConfiguration annotation if the machine already has it.
401-
// We should not add the annotation if it was missing in the first place because we do not have enough
402-
// information.
403-
if clusterConfigurationAnnotation, ok := existingMachine.Annotations[controlplanev1.KubeadmClusterConfigurationAnnotation]; ok {
404-
// In case the annotation is outdated, update it.
405-
if internal.ClusterConfigurationAnnotationFromMachineIsOutdated(clusterConfigurationAnnotation) {
406-
clusterConfiguration, err := internal.ClusterConfigurationFromMachine(existingMachine)
407-
if err != nil {
408-
return nil, err
409-
}
410-
411-
clusterConfigurationAnnotation, err = internal.ClusterConfigurationToMachineAnnotationValue(clusterConfiguration)
412-
if err != nil {
413-
return nil, err
414-
}
415-
}
416-
annotations[controlplanev1.KubeadmClusterConfigurationAnnotation] = clusterConfigurationAnnotation
417-
}
397+
// Cleanup the KubeadmClusterConfigurationAnnotation annotation that was set in Cluster API <= v1.11.
398+
delete(annotations, kubeadmClusterConfigurationAnnotation)
418399

419400
// If the machine already has remediation data then preserve it.
420401
// NOTE: This is required in order to track remediation retries.

controlplane/kubeadm/internal/controllers/helpers_test.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -508,13 +508,11 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
508508
}
509509

510510
tests := []struct {
511-
name string
512-
kcp *controlplanev1.KubeadmControlPlane
513-
isUpdatingExistingMachine bool
514-
existingClusterConfigurationAnnotation string
515-
want []gomegatypes.GomegaMatcher
516-
wantClusterConfigurationAnnotation string
517-
wantErr bool
511+
name string
512+
kcp *controlplanev1.KubeadmControlPlane
513+
isUpdatingExistingMachine bool
514+
want []gomegatypes.GomegaMatcher
515+
wantErr bool
518516
}{
519517
{
520518
name: "should return the correct Machine object when creating a new Machine",
@@ -555,8 +553,7 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
555553
HavePrefix(kcpName + namingTemplateKey),
556554
Not(HaveSuffix("00000")),
557555
},
558-
wantClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"certificatesDir\":\"foo\"}",
559-
wantErr: false,
556+
wantErr: false,
560557
},
561558
{
562559
name: "should return error when creating a new Machine when '.random' is not added in template",
@@ -624,8 +621,7 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
624621
ContainSubstring(fmt.Sprintf("%053d", 0)),
625622
Not(HaveSuffix("00000")),
626623
},
627-
wantClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"certificatesDir\":\"foo\"}",
628-
wantErr: false,
624+
wantErr: false,
629625
},
630626
{
631627
name: "should return error when creating a new Machine with invalid template",
@@ -691,7 +687,6 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
691687
HavePrefix(kcpName),
692688
Not(HaveSuffix("00000")),
693689
},
694-
wantClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"certificatesDir\":\"foo\"}",
695690
},
696691
{
697692
name: "should return the correct Machine object when creating a new Machine with additional kcp readinessGates",
@@ -724,9 +719,8 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
724719
},
725720
},
726721
},
727-
isUpdatingExistingMachine: false,
728-
wantClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"certificatesDir\":\"foo\"}",
729-
wantErr: false,
722+
isUpdatingExistingMachine: false,
723+
wantErr: false,
730724
},
731725
{
732726
name: "should return the correct Machine object when updating an existing Machine (empty ClusterConfiguration annotation)",
@@ -762,10 +756,8 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
762756
},
763757
},
764758
},
765-
isUpdatingExistingMachine: true,
766-
existingClusterConfigurationAnnotation: "",
767-
wantClusterConfigurationAnnotation: "",
768-
wantErr: false,
759+
isUpdatingExistingMachine: true,
760+
wantErr: false,
769761
},
770762
{
771763
name: "should return the correct Machine object when updating an existing Machine (outdated ClusterConfiguration annotation)",
@@ -802,10 +794,7 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
802794
},
803795
},
804796
isUpdatingExistingMachine: true,
805-
806-
existingClusterConfigurationAnnotation: "{\"etcd\":{},\"apiServer\":{\"extraArgs\":{\"foo\":\"bar\"}},\"certificatesDir\":\"foo\"}",
807-
wantClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"apiServer\":{\"extraArgs\":[{\"name\":\"foo\",\"value\":\"bar\"}]},\"certificatesDir\":\"foo\"}",
808-
wantErr: false,
797+
wantErr: false,
809798
},
810799
{
811800
name: "should return the correct Machine object when updating an existing Machine (up to date ClusterConfiguration annotation)",
@@ -841,10 +830,8 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
841830
},
842831
},
843832
},
844-
isUpdatingExistingMachine: true,
845-
existingClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"etcd\":{},\"apiServer\":{\"extraArgs\":[{\"name\":\"foo\",\"value\":\"bar\"}]},\"controllerManager\":{},\"scheduler\":{},\"dns\":{},\"certificatesDir\":\"foo\"}",
846-
wantClusterConfigurationAnnotation: "{\"marshalVersion\":\"v1beta2\",\"etcd\":{},\"apiServer\":{\"extraArgs\":[{\"name\":\"foo\",\"value\":\"bar\"}]},\"controllerManager\":{},\"scheduler\":{},\"dns\":{},\"certificatesDir\":\"foo\"}",
847-
wantErr: false,
833+
isUpdatingExistingMachine: true,
834+
wantErr: false,
848835
},
849836
}
850837

@@ -887,9 +874,6 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
887874
ReadinessGates: []clusterv1.MachineReadinessGate{{ConditionType: "Foo"}},
888875
},
889876
}
890-
if tt.existingClusterConfigurationAnnotation != "" {
891-
existingMachine.Annotations[controlplanev1.KubeadmClusterConfigurationAnnotation] = tt.existingClusterConfigurationAnnotation
892-
}
893877

894878
desiredMachine, err = (&KubeadmControlPlaneReconciler{}).computeDesiredMachine(
895879
tt.kcp, cluster,
@@ -924,9 +908,6 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
924908
for k, v := range kcpMachineTemplateObjectMeta.Annotations {
925909
expectedAnnotations[k] = v
926910
}
927-
if tt.wantClusterConfigurationAnnotation != "" {
928-
expectedAnnotations[controlplanev1.KubeadmClusterConfigurationAnnotation] = tt.wantClusterConfigurationAnnotation
929-
}
930911
expectedAnnotations[controlplanev1.RemediationForAnnotation] = remediationData
931912
// The pre-terminate annotation should always be added
932913
expectedAnnotations[controlplanev1.PreTerminateHookCleanupAnnotation] = ""
@@ -962,7 +943,6 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
962943
for k, v := range kcpMachineTemplateObjectMeta.Annotations {
963944
expectedAnnotations[k] = v
964945
}
965-
expectedAnnotations[controlplanev1.KubeadmClusterConfigurationAnnotation] = tt.wantClusterConfigurationAnnotation
966946
// The pre-terminate annotation should always be added
967947
expectedAnnotations[controlplanev1.PreTerminateHookCleanupAnnotation] = ""
968948
g.Expect(desiredMachine.Annotations).To(Equal(expectedAnnotations))

0 commit comments

Comments
 (0)