diff --git a/controlplane/internal/controllers/rke2controlplane_controller.go b/controlplane/internal/controllers/rke2controlplane_controller.go index d471ed4..c4ee10d 100644 --- a/controlplane/internal/controllers/rke2controlplane_controller.go +++ b/controlplane/internal/controllers/rke2controlplane_controller.go @@ -550,6 +550,21 @@ func (r *RKE2ControlPlaneReconciler) reconcileNormal( desiredReplicas := int(*rcp.Spec.Replicas) switch { + case numMachines == desiredReplicas: + nonDeleteingMachines := controlPlane.Machines.Filter(collections.Not(collections.HasDeletionTimestamp)) + for _, machine := range nonDeleteingMachines { + annotaions := machine.GetAnnotations() + + if _, found := annotaions[controlplanev1.PreTerminateHookCleanupAnnotation]; !found { + annotaions[controlplanev1.PreTerminateHookCleanupAnnotation] = "" + machine.SetAnnotations(annotaions) + } + } + + // Patch machine annoations + if err := controlPlane.PatchMachines(ctx); err != nil { + return ctrl.Result{}, err + } // We are creating the first replica case numMachines < desiredReplicas && numMachines == 0: // Create new Machine w/ init @@ -945,7 +960,7 @@ func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Conte } // Return early if there are other pre-terminate hooks for the Machine. - // The KCP pre-terminate hook should be the one executed last, so that kubelet + // The CAPRKE2 pre-terminate hook should be the one executed last, so that kubelet // is still working while other pre-terminate hooks are run. // Note: This is done only for Kubernetes >= v1.31 to reduce the blast radius of this check. if version.Compare(parsedVersion, semver.MustParse("1.31.0"), version.WithoutPreReleases()) >= 0 { @@ -967,7 +982,8 @@ func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Conte if controlPlane.Machines.Len() > 1 { workloadCluster, err := r.GetWorkloadCluster(ctx, controlPlane) if err != nil { - return ctrl.Result{}, errors.Wrapf(err, "failed to remove etcd member for deleting Machine %s: failed to create client to workload cluster", klog.KObj(deletingMachine)) + return ctrl.Result{}, errors.Wrapf(err, + "failed to remove etcd member for deleting Machine %s: failed to create client to workload cluster", klog.KObj(deletingMachine)) } // Note: In regular deletion cases (remediation, scale down) the leader should have been already moved. @@ -994,7 +1010,9 @@ func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Conte return ctrl.Result{}, err } - log.Info("Waiting for Machines to be deleted", "machines", strings.Join(controlPlane.Machines.Filter(collections.HasDeletionTimestamp).Names(), ", ")) + log.Info("Waiting for Machines to be deleted", "machines", + strings.Join(controlPlane.Machines.Filter(collections.HasDeletionTimestamp).Names(), ", ")) + return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil } @@ -1004,6 +1022,7 @@ func machineHasOtherPreTerminateHooks(machine *clusterv1.Machine) bool { return true } } + return false } diff --git a/controlplane/internal/controllers/scale.go b/controlplane/internal/controllers/scale.go index d95bb7d..ba2c4dc 100644 --- a/controlplane/internal/controllers/scale.go +++ b/controlplane/internal/controllers/scale.go @@ -164,7 +164,6 @@ func (r *RKE2ControlPlaneReconciler) scaleDownControlPlane( // Also in this case the reconcileDelete code of the Machine controller won't execute Node drain // and wait for volume detach. if err := r.removePreTerminateHookAnnotationFromMachine(ctx, machineToDelete); err != nil { - return ctrl.Result{}, err } } @@ -196,9 +195,11 @@ func (r *RKE2ControlPlaneReconciler) removePreTerminateHookAnnotationFromMachine machineOriginal := machine.DeepCopy() delete(machine.Annotations, controlplanev1.PreTerminateHookCleanupAnnotation) + if err := r.Client.Patch(ctx, machine, client.MergeFrom(machineOriginal)); err != nil { return errors.Wrapf(err, "failed to remove pre-terminate hook from control plane Machine %s", klog.KObj(machine)) } + return nil } diff --git a/pkg/rke2/control_plane.go b/pkg/rke2/control_plane.go index 1e5556e..81af9b6 100644 --- a/pkg/rke2/control_plane.go +++ b/pkg/rke2/control_plane.go @@ -280,7 +280,9 @@ func (c *ControlPlane) SortedByDeletionTimestamp(s collections.Machines) []*clus for _, value := range s { res = append(res, value) } + sort.Sort(res) + return res } @@ -422,5 +424,6 @@ func (o machinesByDeletionTimestamp) Less(i, j int) bool { if o[i].DeletionTimestamp.Equal(o[j].DeletionTimestamp) { return o[i].Name < o[j].Name } + return o[i].DeletionTimestamp.Before(o[j].DeletionTimestamp) }