Skip to content

Commit

Permalink
Merge pull request #3273 from mgugino-upstream-stage/machine-deletion…
Browse files Browse the repository at this point in the history
…-hooks-impl

✨ Implement deletion lifecycle hooks
  • Loading branch information
k8s-ci-robot authored Aug 26, 2020
2 parents 6fd476a + a53412e commit 7e445a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
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"

// 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"
)

// 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 @@ -270,6 +270,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 @@ -281,6 +286,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
}

0 comments on commit 7e445a1

Please sign in to comment.