@@ -376,7 +376,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
376
376
assertMachinesNotHealthy (g , mhc , 0 )
377
377
})
378
378
379
- t .Run ("it marks unhealthy machines for remediation when there is one unhealthy Machine" , func (t * testing.T ) {
379
+ t .Run ("it marks unhealthy machines for remediation when there is one unhealthy Machine and skips deleting machines " , func (t * testing.T ) {
380
380
g := NewWithT (t )
381
381
cluster := createCluster (g , ns .Name )
382
382
@@ -404,7 +404,16 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
404
404
machineLabels (mhc .Spec .Selector .MatchLabels ),
405
405
)
406
406
defer cleanup2 ()
407
- machines = append (machines , unhealthyMachines ... )
407
+ // Unhealthy nodes and machines but already in deletion.
408
+ _ , unhealthyMachinesDeleting , cleanup3 := createMachinesWithNodes (g , cluster ,
409
+ count (1 ),
410
+ createNodeRefForMachine (true ),
411
+ nodeStatus (corev1 .ConditionUnknown ),
412
+ machineLabels (mhc .Spec .Selector .MatchLabels ),
413
+ machineDeleting (),
414
+ )
415
+ defer cleanup3 ()
416
+ machines = append (append (machines , unhealthyMachines ... ), unhealthyMachinesDeleting ... )
408
417
targetMachines := make ([]string , len (machines ))
409
418
for i , m := range machines {
410
419
targetMachines [i ] = m .Name
@@ -419,7 +428,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
419
428
}
420
429
return & mhc .Status
421
430
}).Should (MatchMachineHealthCheckStatus (& clusterv1.MachineHealthCheckStatus {
422
- ExpectedMachines : 3 ,
431
+ ExpectedMachines : 4 ,
423
432
CurrentHealthy : 2 ,
424
433
RemediationsAllowed : 2 ,
425
434
ObservedGeneration : 1 ,
@@ -441,7 +450,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
441
450
},
442
451
}))
443
452
444
- assertMachinesNotHealthy (g , mhc , 1 )
453
+ assertMachinesNotHealthy (g , mhc , 2 )
445
454
assertMachinesOwnerRemediated (g , mhc , 1 )
446
455
})
447
456
@@ -2450,6 +2459,8 @@ type machinesWithNodes struct {
2450
2459
labels map [string ]string
2451
2460
failureReason string
2452
2461
failureMessage string
2462
+ finalizers []string
2463
+ deleted bool
2453
2464
}
2454
2465
2455
2466
type machineWithNodesOption func (m * machinesWithNodes )
@@ -2496,6 +2507,13 @@ func machineFailureMessage(s string) machineWithNodesOption {
2496
2507
}
2497
2508
}
2498
2509
2510
+ func machineDeleting () machineWithNodesOption {
2511
+ return func (m * machinesWithNodes ) {
2512
+ m .finalizers = append (m .finalizers , "test.cluster.io/deleting" )
2513
+ m .deleted = true
2514
+ }
2515
+ }
2516
+
2499
2517
func createMachinesWithNodes (
2500
2518
g * WithT ,
2501
2519
c * clusterv1.Cluster ,
@@ -2535,9 +2553,18 @@ func createMachinesWithNodes(
2535
2553
Name : infraMachine .GetName (),
2536
2554
Namespace : infraMachine .GetNamespace (),
2537
2555
}
2556
+ if len (o .finalizers ) > 0 {
2557
+ machine .Finalizers = o .finalizers
2558
+ }
2538
2559
g .Expect (env .Create (ctx , machine )).To (Succeed ())
2539
2560
fmt .Printf ("machine created: %s\n " , machine .GetName ())
2540
2561
2562
+ // Set deletiontimestamp before updating status to ensure its not reconciled
2563
+ // without having the deletionTimestamp set.
2564
+ if o .deleted {
2565
+ g .Expect (env .Delete (ctx , machine )).To (Succeed ())
2566
+ }
2567
+
2541
2568
// Before moving on we want to ensure that the machine has a valid
2542
2569
// status. That is, LastUpdated should not be nil.
2543
2570
g .Eventually (func () * metav1.Time {
@@ -2618,7 +2645,16 @@ func createMachinesWithNodes(
2618
2645
}
2619
2646
}
2620
2647
for _ , m := range machines {
2621
- g .Expect (env .Delete (ctx , m )).To (Succeed ())
2648
+ if m .DeletionTimestamp .IsZero () {
2649
+ g .Expect (env .Delete (ctx , m )).To (Succeed ())
2650
+ }
2651
+ if len (m .Finalizers ) > 1 {
2652
+ g .Expect (env .Get (ctx , util .ObjectKey (m ), m )).To (Succeed ())
2653
+ machinePatchHelper , err := patch .NewHelper (m , env .Client )
2654
+ g .Expect (err ).ToNot (HaveOccurred ())
2655
+ m .Finalizers = nil
2656
+ g .Expect (machinePatchHelper .Patch (ctx , m )).To (Succeed ())
2657
+ }
2622
2658
}
2623
2659
for _ , im := range infraMachines {
2624
2660
if err := env .Delete (ctx , im ); ! apierrors .IsNotFound (err ) {
0 commit comments