@@ -19,6 +19,7 @@ package controllers
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "strings"
22
23
"time"
23
24
24
25
"github.com/go-logr/logr"
@@ -290,6 +291,11 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
290
291
}
291
292
292
293
if isDeleteNodeAllowed {
294
+ // pre-drain.delete lifecycle hook
295
+ // Return early without error, will requeue if/when the Hook is removed.
296
+ if waitForHook (clusterv1 .PreDrainDeleteHookAnnotationPrefix , m .ObjectMeta .Annotations ) {
297
+ return ctrl.Result {}, nil
298
+ }
293
299
// Drain node before deletion.
294
300
if _ , exists := m .ObjectMeta .Annotations [clusterv1 .ExcludeNodeDrainingAnnotation ]; ! exists {
295
301
logger .Info ("Draining node" , "node" , m .Status .NodeRef .Name )
@@ -301,6 +307,12 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
301
307
}
302
308
}
303
309
310
+ // pre-term.delete lifecycle hook
311
+ // Return early without error, will requeue if/when the Hook is removed.
312
+ if waitForHook (clusterv1 .PreTerminateDeleteHookAnnotationPrefix , m .ObjectMeta .Annotations ) {
313
+ return ctrl.Result {}, nil
314
+ }
315
+
304
316
if ok , err := r .reconcileDeleteExternal (ctx , m ); ! ok || err != nil {
305
317
// Return early and don't remove the finalizer if we got an error or
306
318
// the external reconciliation deletion isn't ready.
@@ -491,6 +503,15 @@ func (r *MachineReconciler) shouldAdopt(m *clusterv1.Machine) bool {
491
503
return metav1 .GetControllerOf (m ) == nil && ! util .HasOwner (m .OwnerReferences , clusterv1 .GroupVersion .String (), []string {"Cluster" })
492
504
}
493
505
506
+ func waitForHook (hookName string , annotations map [string ]string ) bool {
507
+ for key := range annotations {
508
+ if strings .HasPrefix (key , hookName ) {
509
+ return true
510
+ }
511
+ }
512
+ return false
513
+ }
514
+
494
515
// writer implements io.Writer interface as a pass-through for klog.
495
516
type writer struct {
496
517
logFunc func (args ... interface {})
0 commit comments