Skip to content

Commit fcd4bc6

Browse files
committed
Implement deletion lifecycle hooks
1 parent 3ac7114 commit fcd4bc6

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

api/v1alpha3/machine_types.go

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ const (
3737

3838
// MachineDeploymentLabelName is the label set on machines if they're controlled by MachineDeployment
3939
MachineDeploymentLabelName = "cluster.x-k8s.io/deployment-name"
40+
41+
// PreDrainDeleteHookAnnotionPrefix annotation specifies the prefix we
42+
// search each annotation for during the pre-drain.delete lifecycle hook
43+
// to pause reconciliation.
44+
PreDrainDeleteHookAnnotionPrefix = "pre-drain.delete.hook.machine.cluster.x-k8s.io"
45+
46+
// PreTermDeleteHookAnnotionPrefix annotation specifies the prefix we
47+
// search each annotation for during the pre-drain.delete lifecycle hook
48+
// to pause reconciliation.
49+
PreTermDeleteHookAnnotionPrefix = "pre-term.delete.hook.machine.cluster.x-k8s.io"
4050
)
4151

4252
// ANCHOR: MachineSpec

controllers/machine_controller.go

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223
"time"
2324

2425
"github.com/go-logr/logr"
@@ -290,6 +291,11 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
290291
}
291292

292293
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.PreDrainDeleteHookAnnotionPrefix, m.ObjectMeta.Annotations) {
297+
return ctrl.Result{}, nil
298+
}
293299
// Drain node before deletion.
294300
if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeNodeDrainingAnnotation]; !exists {
295301
logger.Info("Draining node", "node", m.Status.NodeRef.Name)
@@ -301,6 +307,12 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
301307
}
302308
}
303309

310+
// pre-term.delete lifecycle hook
311+
// Return early without error, will requeue if/when the Hook is removed.
312+
if waitForHook(clusterv1.PreTermDeleteHookAnnotionPrefix, m.ObjectMeta.Annotations) {
313+
return ctrl.Result{}, nil
314+
}
315+
304316
if ok, err := r.reconcileDeleteExternal(ctx, m); !ok || err != nil {
305317
// Return early and don't remove the finalizer if we got an error or
306318
// the external reconciliation deletion isn't ready.
@@ -491,6 +503,14 @@ func (r *MachineReconciler) shouldAdopt(m *clusterv1.Machine) bool {
491503
return metav1.GetControllerOf(m) == nil && !util.HasOwner(m.OwnerReferences, clusterv1.GroupVersion.String(), []string{"Cluster"})
492504
}
493505

506+
func waitForHook(hookName string, annotations map[string]string) bool {
507+
for key := range annotations {
508+
strings.HasPrefix(key, hookName)
509+
return true
510+
}
511+
return false
512+
}
513+
494514
// writer implements io.Writer interface as a pass-through for klog.
495515
type writer struct {
496516
logFunc func(args ...interface{})

0 commit comments

Comments
 (0)