@@ -2530,6 +2530,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
25302530 name string
25312531 cluster * clusterv1.Cluster
25322532 machine * clusterv1.Machine
2533+ infraMachine * unstructured.Unstructured
25332534 expectedError error
25342535 }{
25352536 {
@@ -2556,6 +2557,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
25562557 },
25572558 Status : clusterv1.MachineStatus {},
25582559 },
2560+ infraMachine : nil ,
25592561 expectedError : errNilNodeRef ,
25602562 },
25612563 {
@@ -2586,6 +2588,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
25862588 },
25872589 },
25882590 },
2591+ infraMachine : nil ,
25892592 expectedError : errNoControlPlaneNodes ,
25902593 },
25912594 {
@@ -2618,6 +2621,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26182621 },
26192622 },
26202623 },
2624+ infraMachine : nil ,
26212625 expectedError : errNoControlPlaneNodes ,
26222626 },
26232627 {
@@ -2648,6 +2652,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26482652 },
26492653 },
26502654 },
2655+ infraMachine : nil ,
26512656 expectedError : nil ,
26522657 },
26532658 {
@@ -2661,6 +2666,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26612666 },
26622667 },
26632668 machine : & clusterv1.Machine {},
2669+ infraMachine : nil ,
26642670 expectedError : errClusterIsBeingDeleted ,
26652671 },
26662672 {
@@ -2699,6 +2705,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26992705 },
27002706 },
27012707 },
2708+ infraMachine : nil ,
27022709 expectedError : nil ,
27032710 },
27042711 {
@@ -2737,6 +2744,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27372744 },
27382745 },
27392746 },
2747+ infraMachine : nil ,
27402748 expectedError : errControlPlaneIsBeingDeleted ,
27412749 },
27422750 {
@@ -2775,8 +2783,40 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27752783 },
27762784 },
27772785 },
2786+ infraMachine : nil ,
27782787 expectedError : errControlPlaneIsBeingDeleted ,
27792788 },
2789+ {
2790+ name : "no nodeRef, infrastructure machine has providerID" ,
2791+ cluster : & clusterv1.Cluster {
2792+ ObjectMeta : metav1.ObjectMeta {
2793+ Name : "test-cluster" ,
2794+ Namespace : metav1 .NamespaceDefault ,
2795+ },
2796+ },
2797+ machine : & clusterv1.Machine {
2798+ ObjectMeta : metav1.ObjectMeta {
2799+ Name : "created" ,
2800+ Namespace : metav1 .NamespaceDefault ,
2801+ Labels : map [string ]string {
2802+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2803+ },
2804+ Finalizers : []string {clusterv1 .MachineFinalizer },
2805+ },
2806+ Spec : clusterv1.MachineSpec {
2807+ ClusterName : "test-cluster" ,
2808+ InfrastructureRef : corev1.ObjectReference {},
2809+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2810+ },
2811+ Status : clusterv1.MachineStatus {},
2812+ },
2813+ infraMachine : & unstructured.Unstructured {Object : map [string ]interface {}{
2814+ "spec" : map [string ]interface {}{
2815+ "providerID" : "test-node-1" ,
2816+ },
2817+ }},
2818+ expectedError : nil ,
2819+ },
27802820 }
27812821
27822822 emp := & unstructured.Unstructured {
@@ -2815,6 +2855,16 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28152855 empBeingDeleted .SetDeletionTimestamp (& metav1.Time {Time : time .Now ()})
28162856 empBeingDeleted .SetFinalizers ([]string {"block-deletion" })
28172857
2858+ testNodeA := & corev1.Node {
2859+ ObjectMeta : metav1.ObjectMeta {
2860+ Name : "node-1" ,
2861+ },
2862+ Spec : corev1.NodeSpec {
2863+ ProviderID : "test-node-1" ,
2864+ },
2865+ }
2866+ remoteClient := fake .NewClientBuilder ().WithIndex (& corev1.Node {}, "spec.providerID" , index .NodeByProviderID ).WithObjects (testNodeA ).Build ()
2867+
28182868 for _ , tc := range testCases {
28192869 t .Run (tc .name , func (t * testing.T ) {
28202870 g := NewWithT (t )
@@ -2875,10 +2925,11 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28752925 empBeingDeleted ,
28762926 ).Build ()
28772927 mr := & Reconciler {
2878- Client : c ,
2928+ Client : c ,
2929+ ClusterCache : clustercache .NewFakeClusterCache (remoteClient , client .ObjectKeyFromObject (tc .cluster )),
28792930 }
28802931
2881- err := mr .isDeleteNodeAllowed (ctx , tc .cluster , tc .machine )
2932+ err := mr .isDeleteNodeAllowed (ctx , tc .cluster , tc .machine , tc . infraMachine )
28822933 if tc .expectedError == nil {
28832934 g .Expect (err ).ToNot (HaveOccurred ())
28842935 } else {
0 commit comments