Skip to content

Commit d5646a9

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

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

api/v1alpha3/machine_types.go

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ 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+
// PreDrainDeleteHookAnnotationPrefix annotation specifies the prefix we
42+
// search each annotation for during the pre-drain.delete lifecycle hook
43+
// to pause reconciliation of deletion. These hooks will prevent removal of
44+
// draining the associated node until all are removed.
45+
PreDrainDeleteHookAnnotationPrefix = "pre-drain.delete.hook.machine.cluster.x-k8s.io"
46+
47+
// PreTerminateDeleteHookAnnotationPrefix annotation specifies the prefix we
48+
// search each annotation for during the pre-terminate.delete lifecycle hook
49+
// to pause reconciliation of deletion. These hooks will prevent removal of
50+
// an instance from an infrastructure provider until all are removed.
51+
PreTerminateDeleteHookAnnotationPrefix = "pre-terminate.delete.hook.machine.cluster.x-k8s.io"
4052
)
4153

4254
// ANCHOR: MachineSpec

controllers/machine_controller.go

+21
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.PreDrainDeleteHookAnnotationPrefix, 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.PreTerminateDeleteHookAnnotationPrefix, 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,15 @@ 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+
if strings.HasPrefix(key, hookName) {
509+
return true
510+
}
511+
}
512+
return false
513+
}
514+
494515
// writer implements io.Writer interface as a pass-through for klog.
495516
type writer struct {
496517
logFunc func(args ...interface{})

0 commit comments

Comments
 (0)