Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Implement deletion lifecycle hooks #3273

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/v1alpha3/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ const (

// MachineDeploymentLabelName is the label set on machines if they're controlled by MachineDeployment
MachineDeploymentLabelName = "cluster.x-k8s.io/deployment-name"

// PreDrainDeleteHookAnnotationPrefix annotation specifies the prefix we
// search each annotation for during the pre-drain.delete lifecycle hook
// to pause reconciliation of deletion. These hooks will prevent removal of
// draining the associated node until all are removed.
PreDrainDeleteHookAnnotationPrefix = "pre-drain.delete.hook.machine.cluster.x-k8s.io"
vincepri marked this conversation as resolved.
Show resolved Hide resolved

// PreTerminateDeleteHookAnnotationPrefix annotation specifies the prefix we
// search each annotation for during the pre-terminate.delete lifecycle hook
// to pause reconciliation of deletion. These hooks will prevent removal of
// an instance from an infrastructure provider until all are removed.
PreTerminateDeleteHookAnnotationPrefix = "pre-terminate.delete.hook.machine.cluster.x-k8s.io"
vincepri marked this conversation as resolved.
Show resolved Hide resolved
)

// ANCHOR: MachineSpec
Expand Down
11 changes: 11 additions & 0 deletions controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
}

if isDeleteNodeAllowed {
// pre-drain.delete lifecycle hook
// Return early without error, will requeue if/when the hook owner removes the annotation.
if annotations.HasWithPrefix(clusterv1.PreDrainDeleteHookAnnotationPrefix, m.ObjectMeta.Annotations) {
return ctrl.Result{}, nil
}
// Drain node before deletion.
if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeNodeDrainingAnnotation]; !exists {
logger.Info("Draining node", "node", m.Status.NodeRef.Name)
Expand All @@ -301,6 +306,12 @@ func (r *MachineReconciler) reconcileDelete(ctx context.Context, cluster *cluste
}
}

// pre-term.delete lifecycle hook
// Return early without error, will requeue if/when the hook owner removes the annotation.
if annotations.HasWithPrefix(clusterv1.PreTerminateDeleteHookAnnotationPrefix, m.ObjectMeta.Annotations) {
return ctrl.Result{}, nil
}

if ok, err := r.reconcileDeleteExternal(ctx, m); !ok || err != nil {
// Return early and don't remove the finalizer if we got an error or
// the external reconciliation deletion isn't ready.
Expand Down
4 changes: 2 additions & 2 deletions docs/proposals/20200602-machine-deletion-phase-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ reviewers:
- "@detiber"
- "@ncdc"
creation-date: 2020-06-02
last-updated: 2020-06-02
status: implementable
last-updated: 2020-08-07
status: implemented
---

# Machine Deletion Phase Hooks
Expand Down
11 changes: 11 additions & 0 deletions util/annotations/paused.go → util/annotations/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package annotations

import (
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)
Expand All @@ -38,3 +40,12 @@ func HasPausedAnnotation(o metav1.Object) bool {
_, ok := annotations[clusterv1.PausedAnnotation]
return ok
}

func HasWithPrefix(prefix string, annotations map[string]string) bool {
for key := range annotations {
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
}