Skip to content

Commit 7e445a1

Browse files
authored
Merge pull request #3273 from mgugino-upstream-stage/machine-deletion-hooks-impl
✨ Implement deletion lifecycle hooks
2 parents 6fd476a + a53412e commit 7e445a1

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
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

+11
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
270270
}
271271

272272
if isDeleteNodeAllowed {
273+
// pre-drain.delete lifecycle hook
274+
// Return early without error, will requeue if/when the hook owner removes the annotation.
275+
if annotations.HasWithPrefix(clusterv1.PreDrainDeleteHookAnnotationPrefix, m.ObjectMeta.Annotations) {
276+
return ctrl.Result{}, nil
277+
}
273278
// Drain node before deletion.
274279
if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeNodeDrainingAnnotation]; !exists {
275280
logger.Info("Draining node", "node", m.Status.NodeRef.Name)
@@ -281,6 +286,12 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
281286
}
282287
}
283288

289+
// pre-term.delete lifecycle hook
290+
// Return early without error, will requeue if/when the hook owner removes the annotation.
291+
if annotations.HasWithPrefix(clusterv1.PreTerminateDeleteHookAnnotationPrefix, m.ObjectMeta.Annotations) {
292+
return ctrl.Result{}, nil
293+
}
294+
284295
if ok, err := r.reconcileDeleteExternal(ctx, m); !ok || err != nil {
285296
// Return early and don't remove the finalizer if we got an error or
286297
// the external reconciliation deletion isn't ready.

docs/proposals/20200602-machine-deletion-phase-hooks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ reviewers:
88
- "@detiber"
99
- "@ncdc"
1010
creation-date: 2020-06-02
11-
last-updated: 2020-06-02
12-
status: implementable
11+
last-updated: 2020-08-07
12+
status: implemented
1313
---
1414

1515
# Machine Deletion Phase Hooks

util/annotations/paused.go renamed to util/annotations/helpers.go

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package annotations
1818

1919
import (
20+
"strings"
21+
2022
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2123
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2224
)
@@ -38,3 +40,12 @@ func HasPausedAnnotation(o metav1.Object) bool {
3840
_, ok := annotations[clusterv1.PausedAnnotation]
3941
return ok
4042
}
43+
44+
func HasWithPrefix(prefix string, annotations map[string]string) bool {
45+
for key := range annotations {
46+
if strings.HasPrefix(key, prefix) {
47+
return true
48+
}
49+
}
50+
return false
51+
}

0 commit comments

Comments
 (0)