@@ -32,6 +32,7 @@ import (
32
32
"k8s.io/apimachinery/pkg/util/wait"
33
33
"k8s.io/client-go/tools/record"
34
34
"k8s.io/klog/v2"
35
+ "k8s.io/utils/ptr"
35
36
ctrl "sigs.k8s.io/controller-runtime"
36
37
"sigs.k8s.io/controller-runtime/pkg/builder"
37
38
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -388,13 +389,18 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
388
389
return ctrl.Result {}, err
389
390
}
390
391
391
- // The DrainingSucceededCondition never exists before the node is drained for the first time,
392
- // so its transition time can be used to record the first time draining.
393
- // This `if` condition prevents the transition time to be changed more than once.
392
+ // The DrainingSucceededCondition never exists before the node is drained for the first time.
394
393
if conditions .Get (m , clusterv1 .DrainingSucceededCondition ) == nil {
395
394
conditions .MarkFalse (m , clusterv1 .DrainingSucceededCondition , clusterv1 .DrainingReason , clusterv1 .ConditionSeverityInfo , "Draining the node before deletion" )
396
395
}
397
396
397
+ if m .Status .Deletion == nil {
398
+ m .Status .Deletion = & clusterv1.MachineDeletionStatus {}
399
+ }
400
+ if m .Status .Deletion .NodeDrainStartTime == nil {
401
+ m .Status .Deletion .NodeDrainStartTime = ptr .To (metav1 .Now ())
402
+ }
403
+
398
404
if err := patchMachine (ctx , patchHelper , m ); err != nil {
399
405
return ctrl.Result {}, errors .Wrap (err , "failed to patch Machine" )
400
406
}
@@ -418,13 +424,18 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
418
424
// volumes are detached before proceeding to delete the Node.
419
425
// In case the node is unreachable, the detachment is skipped.
420
426
if r .isNodeVolumeDetachingAllowed (m ) {
421
- // The VolumeDetachSucceededCondition never exists before we wait for volume detachment for the first time,
422
- // so its transition time can be used to record the first time we wait for volume detachment.
423
- // This `if` condition prevents the transition time to be changed more than once.
427
+ // The VolumeDetachSucceededCondition never exists before we wait for volume detachment for the first time.
424
428
if conditions .Get (m , clusterv1 .VolumeDetachSucceededCondition ) == nil {
425
429
conditions .MarkFalse (m , clusterv1 .VolumeDetachSucceededCondition , clusterv1 .WaitingForVolumeDetachReason , clusterv1 .ConditionSeverityInfo , "Waiting for node volumes to be detached" )
426
430
}
427
431
432
+ if m .Status .Deletion == nil {
433
+ m .Status .Deletion = & clusterv1.MachineDeletionStatus {}
434
+ }
435
+ if m .Status .Deletion .WaitForNodeVolumeDetachStartTime == nil {
436
+ m .Status .Deletion .WaitForNodeVolumeDetachStartTime = ptr .To (metav1 .Now ())
437
+ }
438
+
428
439
if ok , err := r .shouldWaitForNodeVolumes (ctx , cluster , m .Status .NodeRef .Name ); ok || err != nil {
429
440
if err != nil {
430
441
r .recorder .Eventf (m , corev1 .EventTypeWarning , "FailedWaitForVolumeDetach" , "error waiting for node volumes detaching, Machine's node %q: %v" , m .Status .NodeRef .Name , err )
@@ -540,38 +551,36 @@ func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
540
551
541
552
func (r * Reconciler ) nodeDrainTimeoutExceeded (machine * clusterv1.Machine ) bool {
542
553
// if the NodeDrainTimeout type is not set by user
543
- if machine .Spec .NodeDrainTimeout == nil || machine .Spec .NodeDrainTimeout .Seconds () <= 0 {
554
+ if machine .Status . Deletion == nil || machine . Spec .NodeDrainTimeout == nil || machine .Spec .NodeDrainTimeout .Seconds () <= 0 {
544
555
return false
545
556
}
546
557
547
- // if the draining succeeded condition does not exist
548
- if conditions . Get ( machine , clusterv1 . DrainingSucceededCondition ) == nil {
558
+ // if the NodeDrainStartTime does not exist
559
+ if machine . Status . Deletion . NodeDrainStartTime == nil {
549
560
return false
550
561
}
551
562
552
563
now := time .Now ()
553
- firstTimeDrain := conditions .GetLastTransitionTime (machine , clusterv1 .DrainingSucceededCondition )
554
- diff := now .Sub (firstTimeDrain .Time )
564
+ diff := now .Sub (machine .Status .Deletion .NodeDrainStartTime .Time )
555
565
return diff .Seconds () >= machine .Spec .NodeDrainTimeout .Seconds ()
556
566
}
557
567
558
568
// nodeVolumeDetachTimeoutExceeded returns False if either NodeVolumeDetachTimeout is set to nil or <=0 OR
559
- // VolumeDetachSucceededCondition is not set on the Machine. Otherwise returns true if the timeout is expired
560
- // since the last transition time of VolumeDetachSucceededCondition .
569
+ // WaitForNodeVolumeDetachStartTime is not set on the Machine. Otherwise returns true if the timeout is expired
570
+ // since the WaitForNodeVolumeDetachStartTime .
561
571
func (r * Reconciler ) nodeVolumeDetachTimeoutExceeded (machine * clusterv1.Machine ) bool {
562
572
// if the NodeVolumeDetachTimeout type is not set by user
563
- if machine .Spec .NodeVolumeDetachTimeout == nil || machine .Spec .NodeVolumeDetachTimeout .Seconds () <= 0 {
573
+ if machine .Status . Deletion == nil || machine . Spec .NodeVolumeDetachTimeout == nil || machine .Spec .NodeVolumeDetachTimeout .Seconds () <= 0 {
564
574
return false
565
575
}
566
576
567
- // if the volume detaching succeeded condition does not exist
568
- if conditions . Get ( machine , clusterv1 . VolumeDetachSucceededCondition ) == nil {
577
+ // if the WaitForNodeVolumeDetachStartTime does not exist
578
+ if machine . Status . Deletion . WaitForNodeVolumeDetachStartTime == nil {
569
579
return false
570
580
}
571
581
572
582
now := time .Now ()
573
- firstTimeDetach := conditions .GetLastTransitionTime (machine , clusterv1 .VolumeDetachSucceededCondition )
574
- diff := now .Sub (firstTimeDetach .Time )
583
+ diff := now .Sub (machine .Status .Deletion .WaitForNodeVolumeDetachStartTime .Time )
575
584
return diff .Seconds () >= machine .Spec .NodeVolumeDetachTimeout .Seconds ()
576
585
}
577
586
0 commit comments