@@ -2528,6 +2528,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
25282528 name string
25292529 cluster * clusterv1.Cluster
25302530 machine * clusterv1.Machine
2531+ infraMachine * unstructured.Unstructured
25312532 expectedError error
25322533 }{
25332534 {
@@ -2554,6 +2555,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
25542555 },
25552556 Status : clusterv1.MachineStatus {},
25562557 },
2558+ infraMachine : nil ,
25572559 expectedError : errNilNodeRef ,
25582560 },
25592561 {
@@ -2584,6 +2586,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
25842586 },
25852587 },
25862588 },
2589+ infraMachine : nil ,
25872590 expectedError : errNoControlPlaneNodes ,
25882591 },
25892592 {
@@ -2616,6 +2619,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26162619 },
26172620 },
26182621 },
2622+ infraMachine : nil ,
26192623 expectedError : errNoControlPlaneNodes ,
26202624 },
26212625 {
@@ -2646,6 +2650,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26462650 },
26472651 },
26482652 },
2653+ infraMachine : nil ,
26492654 expectedError : nil ,
26502655 },
26512656 {
@@ -2659,6 +2664,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26592664 },
26602665 },
26612666 machine : & clusterv1.Machine {},
2667+ infraMachine : nil ,
26622668 expectedError : errClusterIsBeingDeleted ,
26632669 },
26642670 {
@@ -2697,6 +2703,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26972703 },
26982704 },
26992705 },
2706+ infraMachine : nil ,
27002707 expectedError : nil ,
27012708 },
27022709 {
@@ -2735,6 +2742,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27352742 },
27362743 },
27372744 },
2745+ infraMachine : nil ,
27382746 expectedError : errControlPlaneIsBeingDeleted ,
27392747 },
27402748 {
@@ -2773,8 +2781,40 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27732781 },
27742782 },
27752783 },
2784+ infraMachine : nil ,
27762785 expectedError : errControlPlaneIsBeingDeleted ,
27772786 },
2787+ {
2788+ name : "no nodeRef, infrastructure machine has providerID" ,
2789+ cluster : & clusterv1.Cluster {
2790+ ObjectMeta : metav1.ObjectMeta {
2791+ Name : "test-cluster" ,
2792+ Namespace : metav1 .NamespaceDefault ,
2793+ },
2794+ },
2795+ machine : & clusterv1.Machine {
2796+ ObjectMeta : metav1.ObjectMeta {
2797+ Name : "created" ,
2798+ Namespace : metav1 .NamespaceDefault ,
2799+ Labels : map [string ]string {
2800+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2801+ },
2802+ Finalizers : []string {clusterv1 .MachineFinalizer },
2803+ },
2804+ Spec : clusterv1.MachineSpec {
2805+ ClusterName : "test-cluster" ,
2806+ InfrastructureRef : corev1.ObjectReference {},
2807+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2808+ },
2809+ Status : clusterv1.MachineStatus {},
2810+ },
2811+ infraMachine : & unstructured.Unstructured {Object : map [string ]interface {}{
2812+ "spec" : map [string ]interface {}{
2813+ "providerID" : "test-node-1" ,
2814+ },
2815+ }},
2816+ expectedError : nil ,
2817+ },
27782818 }
27792819
27802820 emp := & unstructured.Unstructured {
@@ -2813,6 +2853,16 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28132853 empBeingDeleted .SetDeletionTimestamp (& metav1.Time {Time : time .Now ()})
28142854 empBeingDeleted .SetFinalizers ([]string {"block-deletion" })
28152855
2856+ testNodeA := & corev1.Node {
2857+ ObjectMeta : metav1.ObjectMeta {
2858+ Name : "node-1" ,
2859+ },
2860+ Spec : corev1.NodeSpec {
2861+ ProviderID : "test-node-1" ,
2862+ },
2863+ }
2864+ remoteClient := fake .NewClientBuilder ().WithIndex (& corev1.Node {}, "spec.providerID" , index .NodeByProviderID ).WithObjects (testNodeA ).Build ()
2865+
28162866 for _ , tc := range testCases {
28172867 t .Run (tc .name , func (t * testing.T ) {
28182868 g := NewWithT (t )
@@ -2873,10 +2923,11 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28732923 empBeingDeleted ,
28742924 ).Build ()
28752925 mr := & Reconciler {
2876- Client : c ,
2926+ Client : c ,
2927+ ClusterCache : clustercache .NewFakeClusterCache (remoteClient , client .ObjectKeyFromObject (tc .cluster )),
28772928 }
28782929
2879- err := mr .isDeleteNodeAllowed (ctx , tc .cluster , tc .machine )
2930+ err := mr .isDeleteNodeAllowed (ctx , tc .cluster , tc .machine , tc . infraMachine )
28802931 if tc .expectedError == nil {
28812932 g .Expect (err ).ToNot (HaveOccurred ())
28822933 } else {
0 commit comments