Skip to content

Commit fbd64db

Browse files
Add waitForPreDrainHookStartTime and waitForPreTerminateHookStartTime and omit draining/detachment on existence
Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
1 parent d2c325e commit fbd64db

File tree

10 files changed

+307
-18
lines changed

10 files changed

+307
-18
lines changed

api/core/v1beta1/machine_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,18 @@ type MachineDeletionStatus struct {
622622
// Only present when the Machine has a deletionTimestamp and waiting for volume detachments had been started.
623623
// +optional
624624
WaitForNodeVolumeDetachStartTime *metav1.Time `json:"waitForNodeVolumeDetachStartTime,omitempty"`
625+
626+
// waitForPreDrainHookStartTime is the time when waiting for pre-drain hooks started
627+
// and is used to determine if the pre-drain hooks are taking too long.
628+
// Only present when the Machine has a deletionTimestamp and waiting for pre-drain hooks had been started.
629+
// +optional
630+
WaitForPreDrainHookStartTime *metav1.Time `json:"waitForPreDrainHookStartTime,omitempty"`
631+
632+
// waitForPreTerminateHookStartTime is the time when waiting for pre-terminate hooks started
633+
// and is used to determine if the pre-terminate hooks are taking too long.
634+
// Only present when the Machine has a deletionTimestamp and waiting for pre-terminate hooks had been started.
635+
// +optional
636+
WaitForPreTerminateHookStartTime *metav1.Time `json:"waitForPreTerminateHookStartTime,omitempty"`
625637
}
626638

627639
// SetTypedPhase sets the Phase field to the string representation of MachinePhase.

api/core/v1beta1/zz_generated.conversion.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/core/v1beta1/zz_generated.deepcopy.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/core/v1beta1/zz_generated.openapi.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/core/v1beta2/machine_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,18 @@ type MachineDeletionStatus struct {
671671
// Only present when the Machine has a deletionTimestamp and waiting for volume detachments had been started.
672672
// +optional
673673
WaitForNodeVolumeDetachStartTime *metav1.Time `json:"waitForNodeVolumeDetachStartTime,omitempty"`
674+
675+
// waitForPreDrainHookStartTime is the time when waiting for pre-drain hooks started
676+
// and is used to determine if the pre-drain hooks are taking too long.
677+
// Only present when the Machine has a deletionTimestamp and waiting for pre-drain hooks had been started.
678+
// +optional
679+
WaitForPreDrainHookStartTime *metav1.Time `json:"waitForPreDrainHookStartTime,omitempty"`
680+
681+
// waitForPreTerminateHookStartTime is the time when waiting for pre-terminate hooks started
682+
// and is used to determine if the pre-terminate hooks are taking too long.
683+
// Only present when the Machine has a deletionTimestamp and waiting for pre-terminate hooks had been started.
684+
// +optional
685+
WaitForPreTerminateHookStartTime *metav1.Time `json:"waitForPreTerminateHookStartTime,omitempty"`
674686
}
675687

676688
// SetTypedPhase sets the Phase field to the string representation of MachinePhase.

api/core/v1beta2/zz_generated.deepcopy.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/core/v1beta2/zz_generated.openapi.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/cluster.x-k8s.io_machines.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controllers/machine/machine_controller.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
466466
}
467467
slices.Sort(hooks)
468468
log.Info("Waiting for pre-drain hooks to succeed", "hooks", strings.Join(hooks, ","))
469+
if m.Status.Deletion == nil {
470+
m.Status.Deletion = &clusterv1.MachineDeletionStatus{}
471+
}
472+
if m.Status.Deletion.WaitForPreDrainHookStartTime == nil {
473+
m.Status.Deletion.WaitForPreDrainHookStartTime = ptr.To(metav1.Now())
474+
}
469475
v1beta1conditions.MarkFalse(m, clusterv1.PreDrainDeleteHookSucceededV1Beta1Condition, clusterv1.WaitingExternalHookV1Beta1Reason, clusterv1.ConditionSeverityInfo, "")
470476
s.deletingReason = clusterv1.MachineDeletingWaitingForPreDrainHookReason
471477
s.deletingMessage = fmt.Sprintf("Waiting for pre-drain hooks to succeed (hooks: %s)", strings.Join(hooks, ","))
@@ -474,7 +480,8 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
474480
v1beta1conditions.MarkTrue(m, clusterv1.PreDrainDeleteHookSucceededV1Beta1Condition)
475481

476482
// Drain node before deletion and issue a patch in order to make this operation visible to the users.
477-
if r.isNodeDrainAllowed(m) {
483+
// In case the preTerminateHook is started or the infra machine is not found the detachment is skipped.
484+
if r.isNodeDrainAllowed(m, s.infraMachine) {
478485
patchHelper, err := patch.NewHelper(m, r.Client)
479486
if err != nil {
480487
s.deletingReason = clusterv1.MachineDeletingInternalErrorReason
@@ -521,8 +528,8 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
521528

522529
// After node draining is completed, and if isNodeVolumeDetachingAllowed returns True, make sure all
523530
// volumes are detached before proceeding to delete the Node.
524-
// In case the node is unreachable, the detachment is skipped.
525-
if r.isNodeVolumeDetachingAllowed(m) {
531+
// In case the node is unreachable, preTerminateHook is started or the infra machine is not found the detachment is skipped.
532+
if r.isNodeVolumeDetachingAllowed(m, s.infraMachine) {
526533
if m.Status.Deletion == nil {
527534
m.Status.Deletion = &clusterv1.MachineDeletionStatus{}
528535
}
@@ -563,6 +570,12 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
563570
}
564571
slices.Sort(hooks)
565572
log.Info("Waiting for pre-terminate hooks to succeed", "hooks", strings.Join(hooks, ","))
573+
if m.Status.Deletion == nil {
574+
m.Status.Deletion = &clusterv1.MachineDeletionStatus{}
575+
}
576+
if m.Status.Deletion.WaitForPreTerminateHookStartTime == nil {
577+
m.Status.Deletion.WaitForPreTerminateHookStartTime = ptr.To(metav1.Now())
578+
}
566579
v1beta1conditions.MarkFalse(m, clusterv1.PreTerminateDeleteHookSucceededV1Beta1Condition, clusterv1.WaitingExternalHookV1Beta1Reason, clusterv1.ConditionSeverityInfo, "")
567580
s.deletingReason = clusterv1.MachineDeletingWaitingForPreTerminateHookReason
568581
s.deletingMessage = fmt.Sprintf("Waiting for pre-terminate hooks to succeed (hooks: %s)", strings.Join(hooks, ","))
@@ -637,7 +650,7 @@ const (
637650
KubeadmControlPlanePreTerminateHookCleanupAnnotation = clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup"
638651
)
639652

640-
func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
653+
func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine, infraMachine client.Object) bool {
641654
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersionControlPlane.String(), []string{"KubeadmControlPlane"}) {
642655
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
643656
return false
@@ -648,6 +661,14 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
648661
return false
649662
}
650663

664+
if m.Status.Deletion != nil && m.Status.Deletion.WaitForPreTerminateHookStartTime != nil {
665+
return false
666+
}
667+
668+
if infraMachine == nil || !infraMachine.GetDeletionTimestamp().IsZero() {
669+
return false
670+
}
671+
651672
if r.nodeDrainTimeoutExceeded(m) {
652673
return false
653674
}
@@ -657,7 +678,7 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
657678

658679
// isNodeVolumeDetachingAllowed returns False if either ExcludeWaitForNodeVolumeDetachAnnotation annotation is set OR
659680
// nodeVolumeDetachTimeoutExceeded timeout is exceeded, otherwise returns True.
660-
func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
681+
func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine, infraMachine client.Object) bool {
661682
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersionControlPlane.String(), []string{"KubeadmControlPlane"}) {
662683
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
663684
return false
@@ -668,6 +689,14 @@ func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
668689
return false
669690
}
670691

692+
if m.Status.Deletion != nil && m.Status.Deletion.WaitForPreTerminateHookStartTime != nil {
693+
return false
694+
}
695+
696+
if infraMachine == nil || !infraMachine.GetDeletionTimestamp().IsZero() {
697+
return false
698+
}
699+
671700
if r.nodeVolumeDetachTimeoutExceeded(m) {
672701
return false
673702
}

0 commit comments

Comments
 (0)