From 8ed4065e8537e906b01efa21da0665139ba4b0bc Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Thu, 5 Sep 2024 16:07:24 +0200 Subject: [PATCH] fixup --- .../internal/controllers/controller.go | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/controlplane/kubeadm/internal/controllers/controller.go b/controlplane/kubeadm/internal/controllers/controller.go index 2dd618d220fb..bdcc4e3e36af 100644 --- a/controlplane/kubeadm/internal/controllers/controller.go +++ b/controlplane/kubeadm/internal/controllers/controller.go @@ -579,11 +579,8 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, con // to forward etcd leadership without any member left after we went through the Machine deletion. // Also in this case the reconcileDelete code of the Machine controller won't execute Node drain // and wait for volume detach. - log.Info("Removing pre-terminate hook from control plane machine") - deletingMachineOriginal := machineToDelete.DeepCopy() - delete(machineToDelete.Annotations, controlplanev1.PreTerminateHookCleanupAnnotation) - if err := r.Client.Patch(ctx, machineToDelete, client.MergeFrom(deletingMachineOriginal)); err != nil { - errs = append(errs, errors.Wrapf(err, "failed to remove pre-terminate hook from control plane Machine %s", klog.KObj(machineToDelete))) + if err := r.removePreTerminateHookAnnotationFromMachine(ctx, machineToDelete); err != nil { + errs = append(errs, err) continue } @@ -602,6 +599,18 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, con return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil } +func (r *KubeadmControlPlaneReconciler) removePreTerminateHookAnnotationFromMachine(ctx context.Context, machine *clusterv1.Machine) error { + log := ctrl.LoggerFrom(ctx) + log.Info("Removing pre-terminate hook from control plane Machine") + + 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 +} + // ClusterToKubeadmControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation // for KubeadmControlPlane based on updates to a Cluster. func (r *KubeadmControlPlaneReconciler) ClusterToKubeadmControlPlane(_ context.Context, o client.Object) []ctrl.Request { @@ -882,11 +891,8 @@ func (r *KubeadmControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Co } } - log.Info("Removing pre-terminate hook from control plane Machine") - deletingMachineOriginal := deletingMachine.DeepCopy() - delete(deletingMachine.Annotations, controlplanev1.PreTerminateHookCleanupAnnotation) - if err := r.Client.Patch(ctx, deletingMachine, client.MergeFrom(deletingMachineOriginal)); err != nil { - return ctrl.Result{}, errors.Wrapf(err, "failed to remove pre-terminate hook from control plane Machine %s", klog.KObj(deletingMachine)) + if err := r.removePreTerminateHookAnnotationFromMachine(ctx, deletingMachine); err != nil { + return ctrl.Result{}, err } log.Info("Waiting for Machines to be deleted", "machines", strings.Join(controlPlane.Machines.Filter(collections.HasDeletionTimestamp).Names(), ", "))