Skip to content

Commit 3d22daf

Browse files
authored
Merge pull request #11161 from sbueringer/pr-make-kcp-pre-terminate-more-robust
🐛 Make KCP pre-terminate hook more robust
2 parents 8df788d + 4c83455 commit 3d22daf

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, con
567567
}
568568

569569
// Delete control plane machines in parallel
570-
machinesToDelete := controlPlane.Machines.Filter(collections.Not(collections.HasDeletionTimestamp))
570+
machinesToDelete := controlPlane.Machines
571571
var errs []error
572572
for _, machineToDelete := range machinesToDelete {
573573
log := log.WithValues("Machine", klog.KObj(machineToDelete))
@@ -584,6 +584,11 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, con
584584
continue
585585
}
586586

587+
if !machineToDelete.DeletionTimestamp.IsZero() {
588+
// Nothing to do, Machine already has deletionTimestamp set.
589+
continue
590+
}
591+
587592
log.Info("Deleting control plane Machine")
588593
if err := r.Client.Delete(ctx, machineToDelete); err != nil && !apierrors.IsNotFound(err) {
589594
errs = append(errs, errors.Wrapf(err, "failed to delete control plane Machine %s", klog.KObj(machineToDelete)))
@@ -595,11 +600,19 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, con
595600
"Failed to delete control plane Machines for cluster %s control plane: %v", klog.KObj(controlPlane.Cluster), err)
596601
return ctrl.Result{}, err
597602
}
603+
604+
log.Info("Waiting for control plane Machines to not exist anymore")
605+
598606
conditions.MarkFalse(controlPlane.KCP, controlplanev1.ResizedCondition, clusterv1.DeletingReason, clusterv1.ConditionSeverityInfo, "")
599607
return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil
600608
}
601609

602610
func (r *KubeadmControlPlaneReconciler) removePreTerminateHookAnnotationFromMachine(ctx context.Context, machine *clusterv1.Machine) error {
611+
if _, exists := machine.Annotations[controlplanev1.PreTerminateHookCleanupAnnotation]; !exists {
612+
// Nothing to do, the annotation is not set (anymore) on the Machine
613+
return nil
614+
}
615+
603616
log := ctrl.LoggerFrom(ctx)
604617
log.Info("Removing pre-terminate hook from control plane Machine")
605618

controlplane/kubeadm/internal/controllers/controller_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,8 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) {
24782478
initObjs = append(initObjs, m)
24792479
machines.Insert(m)
24802480
}
2481+
// One Machine was already deleted before KCP, validate the pre-terminate hook is still removed.
2482+
machines.UnsortedList()[2].DeletionTimestamp = &metav1.Time{Time: time.Now()}
24812483

24822484
fakeClient := newFakeClient(initObjs...)
24832485

internal/controllers/machine/machine_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package machine
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223
"time"
2324

2425
"github.com/pkg/errors"
@@ -366,6 +367,13 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
366367
// pre-drain.delete lifecycle hook
367368
// Return early without error, will requeue if/when the hook owner removes the annotation.
368369
if annotations.HasWithPrefix(clusterv1.PreDrainDeleteHookAnnotationPrefix, m.ObjectMeta.Annotations) {
370+
var hooks []string
371+
for key := range m.ObjectMeta.Annotations {
372+
if strings.HasPrefix(key, clusterv1.PreDrainDeleteHookAnnotationPrefix) {
373+
hooks = append(hooks, key)
374+
}
375+
}
376+
log.Info("Waiting for pre-drain hooks to succeed", "hooks", strings.Join(hooks, ","))
369377
conditions.MarkFalse(m, clusterv1.PreDrainDeleteHookSucceededCondition, clusterv1.WaitingExternalHookReason, clusterv1.ConditionSeverityInfo, "")
370378
return ctrl.Result{}, nil
371379
}
@@ -430,6 +438,13 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
430438
// pre-term.delete lifecycle hook
431439
// Return early without error, will requeue if/when the hook owner removes the annotation.
432440
if annotations.HasWithPrefix(clusterv1.PreTerminateDeleteHookAnnotationPrefix, m.ObjectMeta.Annotations) {
441+
var hooks []string
442+
for key := range m.ObjectMeta.Annotations {
443+
if strings.HasPrefix(key, clusterv1.PreTerminateDeleteHookAnnotationPrefix) {
444+
hooks = append(hooks, key)
445+
}
446+
}
447+
log.Info("Waiting for pre-terminate hooks to succeed", "hooks", strings.Join(hooks, ","))
433448
conditions.MarkFalse(m, clusterv1.PreTerminateDeleteHookSucceededCondition, clusterv1.WaitingExternalHookReason, clusterv1.ConditionSeverityInfo, "")
434449
return ctrl.Result{}, nil
435450
}

0 commit comments

Comments
 (0)