@@ -405,14 +405,26 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
405405 )
406406 defer cleanup2 ()
407407 // Unhealthy nodes and machines but already in deletion.
408+ // Note: deletionTimestamp gets set by deletion below which also removes the skip remediation annotation.
408409 _ , unhealthyMachinesDeleting , cleanup3 := createMachinesWithNodes (g , cluster ,
409410 count (1 ),
410411 createNodeRefForMachine (true ),
411412 nodeStatus (corev1 .ConditionUnknown ),
412413 machineLabels (mhc .Spec .Selector .MatchLabels ),
413- machineDeleting (),
414+ machineFinalizers ("test.cluster.io/delete-protection" ),
415+ machineAnnotations (map [string ]string {clusterv1 .MachineSkipRemediationAnnotation : "" }),
414416 )
415417 defer cleanup3 ()
418+ // Mark machines for deletion and drop skip remediation annotation
419+ // Note: without the skip remediation annotation the MHC controller might already reconcile the condition leading to a flaky test.
420+ for _ , m := range unhealthyMachinesDeleting {
421+ g .Expect (env .Delete (ctx , m )).To (Succeed ())
422+ g .Expect (env .Get (ctx , client .ObjectKeyFromObject (m ), m )).To (Succeed ())
423+ deletingMachinePatchHelper , err := patch .NewHelper (m , env .GetClient ())
424+ g .Expect (err ).ToNot (HaveOccurred ())
425+ m .Annotations = map [string ]string {}
426+ g .Expect (deletingMachinePatchHelper .Patch (ctx , m )).To (Succeed ())
427+ }
416428 machines = append (append (machines , unhealthyMachines ... ), unhealthyMachinesDeleting ... )
417429 targetMachines := make ([]string , len (machines ))
418430 for i , m := range machines {
@@ -2456,11 +2468,11 @@ type machinesWithNodes struct {
24562468 nodeStatus corev1.ConditionStatus
24572469 createNodeRefForMachine bool
24582470 firstMachineAsControlPlane bool
2471+ annotations map [string ]string
24592472 labels map [string ]string
24602473 failureReason string
24612474 failureMessage string
24622475 finalizers []string
2463- deleted bool
24642476}
24652477
24662478type machineWithNodesOption func (m * machinesWithNodes )
@@ -2507,10 +2519,15 @@ func machineFailureMessage(s string) machineWithNodesOption {
25072519 }
25082520}
25092521
2510- func machineDeleting ( ) machineWithNodesOption {
2522+ func machineAnnotations ( a map [ string ] string ) machineWithNodesOption {
25112523 return func (m * machinesWithNodes ) {
2512- m .finalizers = append (m .finalizers , "test.cluster.io/deleting" )
2513- m .deleted = true
2524+ m .annotations = a
2525+ }
2526+ }
2527+
2528+ func machineFinalizers (f ... string ) machineWithNodesOption {
2529+ return func (m * machinesWithNodes ) {
2530+ m .finalizers = append (m .finalizers , f ... )
25142531 }
25152532}
25162533
@@ -2556,15 +2573,12 @@ func createMachinesWithNodes(
25562573 if len (o .finalizers ) > 0 {
25572574 machine .Finalizers = o .finalizers
25582575 }
2576+ if o .annotations != nil {
2577+ machine .ObjectMeta .Annotations = o .annotations
2578+ }
25592579 g .Expect (env .Create (ctx , machine )).To (Succeed ())
25602580 fmt .Printf ("machine created: %s\n " , machine .GetName ())
25612581
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-
25682582 // Before moving on we want to ensure that the machine has a valid
25692583 // status. That is, LastUpdated should not be nil.
25702584 g .Eventually (func () * metav1.Time {
0 commit comments