@@ -2604,6 +2604,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26042604 name string
26052605 cluster * clusterv1.Cluster
26062606 machine * clusterv1.Machine
2607+ infraMachine * unstructured.Unstructured
26072608 expectedError error
26082609 }{
26092610 {
@@ -2630,6 +2631,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26302631 },
26312632 Status : clusterv1.MachineStatus {},
26322633 },
2634+ infraMachine : nil ,
26332635 expectedError : errNilNodeRef ,
26342636 },
26352637 {
@@ -2660,6 +2662,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26602662 },
26612663 },
26622664 },
2665+ infraMachine : nil ,
26632666 expectedError : errNoControlPlaneNodes ,
26642667 },
26652668 {
@@ -2692,6 +2695,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
26922695 },
26932696 },
26942697 },
2698+ infraMachine : nil ,
26952699 expectedError : errNoControlPlaneNodes ,
26962700 },
26972701 {
@@ -2722,6 +2726,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27222726 },
27232727 },
27242728 },
2729+ infraMachine : nil ,
27252730 expectedError : nil ,
27262731 },
27272732 {
@@ -2735,6 +2740,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27352740 },
27362741 },
27372742 machine : & clusterv1.Machine {},
2743+ infraMachine : nil ,
27382744 expectedError : errClusterIsBeingDeleted ,
27392745 },
27402746 {
@@ -2773,6 +2779,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
27732779 },
27742780 },
27752781 },
2782+ infraMachine : nil ,
27762783 expectedError : nil ,
27772784 },
27782785 {
@@ -2811,6 +2818,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28112818 },
28122819 },
28132820 },
2821+ infraMachine : nil ,
28142822 expectedError : errControlPlaneIsBeingDeleted ,
28152823 },
28162824 {
@@ -2849,8 +2857,40 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28492857 },
28502858 },
28512859 },
2860+ infraMachine : nil ,
28522861 expectedError : errControlPlaneIsBeingDeleted ,
28532862 },
2863+ {
2864+ name : "no nodeRef, infrastructure machine has providerID" ,
2865+ cluster : & clusterv1.Cluster {
2866+ ObjectMeta : metav1.ObjectMeta {
2867+ Name : "test-cluster" ,
2868+ Namespace : metav1 .NamespaceDefault ,
2869+ },
2870+ },
2871+ machine : & clusterv1.Machine {
2872+ ObjectMeta : metav1.ObjectMeta {
2873+ Name : "created" ,
2874+ Namespace : metav1 .NamespaceDefault ,
2875+ Labels : map [string ]string {
2876+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2877+ },
2878+ Finalizers : []string {clusterv1 .MachineFinalizer },
2879+ },
2880+ Spec : clusterv1.MachineSpec {
2881+ ClusterName : "test-cluster" ,
2882+ InfrastructureRef : corev1.ObjectReference {},
2883+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2884+ },
2885+ Status : clusterv1.MachineStatus {},
2886+ },
2887+ infraMachine : & unstructured.Unstructured {Object : map [string ]interface {}{
2888+ "spec" : map [string ]interface {}{
2889+ "providerID" : "test-node-1" ,
2890+ },
2891+ }},
2892+ expectedError : nil ,
2893+ },
28542894 }
28552895
28562896 emp := & unstructured.Unstructured {
@@ -2889,6 +2929,16 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
28892929 empBeingDeleted .SetDeletionTimestamp (& metav1.Time {Time : time .Now ()})
28902930 empBeingDeleted .SetFinalizers ([]string {"block-deletion" })
28912931
2932+ testNodeA := & corev1.Node {
2933+ ObjectMeta : metav1.ObjectMeta {
2934+ Name : "node-1" ,
2935+ },
2936+ Spec : corev1.NodeSpec {
2937+ ProviderID : "test-node-1" ,
2938+ },
2939+ }
2940+ remoteClient := fake .NewClientBuilder ().WithIndex (& corev1.Node {}, "spec.providerID" , index .NodeByProviderID ).WithObjects (testNodeA ).Build ()
2941+
28922942 for _ , tc := range testCases {
28932943 t .Run (tc .name , func (t * testing.T ) {
28942944 g := NewWithT (t )
@@ -2949,10 +2999,11 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
29492999 empBeingDeleted ,
29503000 ).Build ()
29513001 mr := & Reconciler {
2952- Client : c ,
3002+ Client : c ,
3003+ ClusterCache : clustercache .NewFakeClusterCache (remoteClient , client .ObjectKeyFromObject (tc .cluster )),
29533004 }
29543005
2955- err := mr .isDeleteNodeAllowed (ctx , tc .cluster , tc .machine )
3006+ err := mr .isDeleteNodeAllowed (ctx , tc .cluster , tc .machine , tc . infraMachine )
29563007 if tc .expectedError == nil {
29573008 g .Expect (err ).ToNot (HaveOccurred ())
29583009 } else {
0 commit comments