@@ -466,6 +466,12 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
466
466
}
467
467
slices .Sort (hooks )
468
468
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
+ }
469
475
v1beta1conditions .MarkFalse (m , clusterv1 .PreDrainDeleteHookSucceededV1Beta1Condition , clusterv1 .WaitingExternalHookV1Beta1Reason , clusterv1 .ConditionSeverityInfo , "" )
470
476
s .deletingReason = clusterv1 .MachineDeletingWaitingForPreDrainHookReason
471
477
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
474
480
v1beta1conditions .MarkTrue (m , clusterv1 .PreDrainDeleteHookSucceededV1Beta1Condition )
475
481
476
482
// 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 ) {
478
485
patchHelper , err := patch .NewHelper (m , r .Client )
479
486
if err != nil {
480
487
s .deletingReason = clusterv1 .MachineDeletingInternalErrorReason
@@ -521,8 +528,8 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
521
528
522
529
// After node draining is completed, and if isNodeVolumeDetachingAllowed returns True, make sure all
523
530
// 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 ) {
526
533
if m .Status .Deletion == nil {
527
534
m .Status .Deletion = & clusterv1.MachineDeletionStatus {}
528
535
}
@@ -563,6 +570,12 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
563
570
}
564
571
slices .Sort (hooks )
565
572
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
+ }
566
579
v1beta1conditions .MarkFalse (m , clusterv1 .PreTerminateDeleteHookSucceededV1Beta1Condition , clusterv1 .WaitingExternalHookV1Beta1Reason , clusterv1 .ConditionSeverityInfo , "" )
567
580
s .deletingReason = clusterv1 .MachineDeletingWaitingForPreTerminateHookReason
568
581
s .deletingMessage = fmt .Sprintf ("Waiting for pre-terminate hooks to succeed (hooks: %s)" , strings .Join (hooks , "," ))
@@ -637,7 +650,7 @@ const (
637
650
KubeadmControlPlanePreTerminateHookCleanupAnnotation = clusterv1 .PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup"
638
651
)
639
652
640
- func (r * Reconciler ) isNodeDrainAllowed (m * clusterv1.Machine ) bool {
653
+ func (r * Reconciler ) isNodeDrainAllowed (m * clusterv1.Machine , infraMachine client. Object ) bool {
641
654
if util .IsControlPlaneMachine (m ) && util .HasOwner (m .GetOwnerReferences (), clusterv1 .GroupVersionControlPlane .String (), []string {"KubeadmControlPlane" }) {
642
655
if _ , exists := m .Annotations [KubeadmControlPlanePreTerminateHookCleanupAnnotation ]; ! exists {
643
656
return false
@@ -648,6 +661,14 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
648
661
return false
649
662
}
650
663
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
+
651
672
if r .nodeDrainTimeoutExceeded (m ) {
652
673
return false
653
674
}
@@ -657,7 +678,7 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
657
678
658
679
// isNodeVolumeDetachingAllowed returns False if either ExcludeWaitForNodeVolumeDetachAnnotation annotation is set OR
659
680
// 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 {
661
682
if util .IsControlPlaneMachine (m ) && util .HasOwner (m .GetOwnerReferences (), clusterv1 .GroupVersionControlPlane .String (), []string {"KubeadmControlPlane" }) {
662
683
if _ , exists := m .Annotations [KubeadmControlPlanePreTerminateHookCleanupAnnotation ]; ! exists {
663
684
return false
@@ -668,6 +689,14 @@ func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
668
689
return false
669
690
}
670
691
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
+
671
700
if r .nodeVolumeDetachTimeoutExceeded (m ) {
672
701
return false
673
702
}
0 commit comments