@@ -2529,9 +2529,53 @@ func nodeNameIndex(o client.Object) []string {
2529
2529
func TestIsDeleteNodeAllowed (t * testing.T ) {
2530
2530
deletionts := metav1 .Now ()
2531
2531
2532
+ cp1 := & clusterv1.Machine {
2533
+ ObjectMeta : metav1.ObjectMeta {
2534
+ Name : "cp1" ,
2535
+ Namespace : metav1 .NamespaceDefault ,
2536
+ Labels : map [string ]string {
2537
+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2538
+ clusterv1 .MachineControlPlaneLabel : "" ,
2539
+ },
2540
+ Finalizers : []string {clusterv1 .MachineFinalizer },
2541
+ },
2542
+ Spec : clusterv1.MachineSpec {
2543
+ ClusterName : "test-cluster" ,
2544
+ InfrastructureRef : corev1.ObjectReference {},
2545
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2546
+ },
2547
+ Status : clusterv1.MachineStatus {
2548
+ NodeRef : & corev1.ObjectReference {
2549
+ Name : "test1" ,
2550
+ },
2551
+ },
2552
+ }
2553
+ cp2 := & clusterv1.Machine {
2554
+ ObjectMeta : metav1.ObjectMeta {
2555
+ Name : "cp2" ,
2556
+ Namespace : metav1 .NamespaceDefault ,
2557
+ Labels : map [string ]string {
2558
+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2559
+ clusterv1 .MachineControlPlaneLabel : "" ,
2560
+ },
2561
+ Finalizers : []string {clusterv1 .MachineFinalizer },
2562
+ },
2563
+ Spec : clusterv1.MachineSpec {
2564
+ ClusterName : "test-cluster" ,
2565
+ InfrastructureRef : corev1.ObjectReference {},
2566
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2567
+ },
2568
+ Status : clusterv1.MachineStatus {
2569
+ NodeRef : & corev1.ObjectReference {
2570
+ Name : "test2" ,
2571
+ },
2572
+ },
2573
+ }
2574
+
2532
2575
testCases := []struct {
2533
2576
name string
2534
2577
cluster * clusterv1.Cluster
2578
+ extraMachines []* clusterv1.Machine
2535
2579
machine * clusterv1.Machine
2536
2580
expectedError error
2537
2581
}{
@@ -2559,10 +2603,13 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2559
2603
},
2560
2604
Status : clusterv1.MachineStatus {},
2561
2605
},
2606
+ extraMachines : nil ,
2562
2607
expectedError : errNilNodeRef ,
2563
2608
},
2564
2609
{
2565
- name : "no control plane members" ,
2610
+ // This situation can happen when the control plane is not implemented via Cluster API,
2611
+ // so for Cluster API the control plane machine members count is zero in this case.
2612
+ name : "no control plane members, deleting a worker" ,
2566
2613
cluster : & clusterv1.Cluster {
2567
2614
ObjectMeta : metav1.ObjectMeta {
2568
2615
Name : "test-cluster" ,
@@ -2576,7 +2623,8 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2576
2623
Labels : map [string ]string {
2577
2624
clusterv1 .ClusterNameLabel : "test-cluster" ,
2578
2625
},
2579
- Finalizers : []string {clusterv1 .MachineFinalizer },
2626
+ Finalizers : []string {clusterv1 .MachineFinalizer },
2627
+ DeletionTimestamp : & metav1.Time {Time : time .Now ().UTC ()},
2580
2628
},
2581
2629
Spec : clusterv1.MachineSpec {
2582
2630
ClusterName : "test-cluster" ,
@@ -2589,10 +2637,11 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2589
2637
},
2590
2638
},
2591
2639
},
2592
- expectedError : errNoControlPlaneNodes ,
2640
+ extraMachines : nil ,
2641
+ expectedError : nil ,
2593
2642
},
2594
2643
{
2595
- name : "is last control plane member" ,
2644
+ name : "is last control plane member, trying to delete it " ,
2596
2645
cluster : & clusterv1.Cluster {
2597
2646
ObjectMeta : metav1.ObjectMeta {
2598
2647
Name : "test-cluster" ,
@@ -2621,7 +2670,40 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2621
2670
},
2622
2671
},
2623
2672
},
2624
- expectedError : errNoControlPlaneNodes ,
2673
+ extraMachines : nil ,
2674
+ expectedError : errLastControlPlaneNode ,
2675
+ },
2676
+ {
2677
+ name : "only one control plane member, trying to delete a worker" ,
2678
+ cluster : & clusterv1.Cluster {
2679
+ ObjectMeta : metav1.ObjectMeta {
2680
+ Name : "test-cluster" ,
2681
+ Namespace : metav1 .NamespaceDefault ,
2682
+ },
2683
+ },
2684
+ machine : & clusterv1.Machine {
2685
+ ObjectMeta : metav1.ObjectMeta {
2686
+ Name : "created" ,
2687
+ Namespace : metav1 .NamespaceDefault ,
2688
+ Labels : map [string ]string {
2689
+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2690
+ },
2691
+ Finalizers : []string {clusterv1 .MachineFinalizer },
2692
+ DeletionTimestamp : & metav1.Time {Time : time .Now ().UTC ()},
2693
+ },
2694
+ Spec : clusterv1.MachineSpec {
2695
+ ClusterName : "test-cluster" ,
2696
+ InfrastructureRef : corev1.ObjectReference {},
2697
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2698
+ },
2699
+ Status : clusterv1.MachineStatus {
2700
+ NodeRef : & corev1.ObjectReference {
2701
+ Name : "test" ,
2702
+ },
2703
+ },
2704
+ },
2705
+ extraMachines : nil ,
2706
+ expectedError : nil ,
2625
2707
},
2626
2708
{
2627
2709
name : "has nodeRef and control plane is healthy" ,
@@ -2651,6 +2733,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2651
2733
},
2652
2734
},
2653
2735
},
2736
+ extraMachines : []* clusterv1.Machine {cp1 , cp2 },
2654
2737
expectedError : nil ,
2655
2738
},
2656
2739
{
@@ -2664,6 +2747,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2664
2747
},
2665
2748
},
2666
2749
machine : & clusterv1.Machine {},
2750
+ extraMachines : nil ,
2667
2751
expectedError : errClusterIsBeingDeleted ,
2668
2752
},
2669
2753
{
@@ -2702,6 +2786,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2702
2786
},
2703
2787
},
2704
2788
},
2789
+ extraMachines : []* clusterv1.Machine {cp1 , cp2 },
2705
2790
expectedError : nil ,
2706
2791
},
2707
2792
{
@@ -2740,6 +2825,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2740
2825
},
2741
2826
},
2742
2827
},
2828
+ extraMachines : nil ,
2743
2829
expectedError : errControlPlaneIsBeingDeleted ,
2744
2830
},
2745
2831
{
@@ -2778,6 +2864,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2778
2864
},
2779
2865
},
2780
2866
},
2867
+ extraMachines : nil ,
2781
2868
expectedError : errControlPlaneIsBeingDeleted ,
2782
2869
},
2783
2870
}
@@ -2822,61 +2909,19 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2822
2909
t .Run (tc .name , func (t * testing.T ) {
2823
2910
g := NewWithT (t )
2824
2911
2825
- m1 := & clusterv1.Machine {
2826
- ObjectMeta : metav1.ObjectMeta {
2827
- Name : "cp1" ,
2828
- Namespace : metav1 .NamespaceDefault ,
2829
- Labels : map [string ]string {
2830
- clusterv1 .ClusterNameLabel : "test-cluster" ,
2831
- },
2832
- Finalizers : []string {clusterv1 .MachineFinalizer },
2833
- },
2834
- Spec : clusterv1.MachineSpec {
2835
- ClusterName : "test-cluster" ,
2836
- InfrastructureRef : corev1.ObjectReference {},
2837
- Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2838
- },
2839
- Status : clusterv1.MachineStatus {
2840
- NodeRef : & corev1.ObjectReference {
2841
- Name : "test1" ,
2842
- },
2843
- },
2844
- }
2845
- m2 := & clusterv1.Machine {
2846
- ObjectMeta : metav1.ObjectMeta {
2847
- Name : "cp2" ,
2848
- Namespace : metav1 .NamespaceDefault ,
2849
- Labels : map [string ]string {
2850
- clusterv1 .ClusterNameLabel : "test-cluster" ,
2851
- },
2852
- Finalizers : []string {clusterv1 .MachineFinalizer },
2853
- },
2854
- Spec : clusterv1.MachineSpec {
2855
- ClusterName : "test-cluster" ,
2856
- InfrastructureRef : corev1.ObjectReference {},
2857
- Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2858
- },
2859
- Status : clusterv1.MachineStatus {
2860
- NodeRef : & corev1.ObjectReference {
2861
- Name : "test2" ,
2862
- },
2863
- },
2864
- }
2865
- // For isDeleteNodeAllowed to be true we assume a healthy control plane.
2866
- if tc .expectedError == nil {
2867
- m1 .Labels [clusterv1 .MachineControlPlaneLabel ] = ""
2868
- m2 .Labels [clusterv1 .MachineControlPlaneLabel ] = ""
2869
- }
2870
-
2871
- c := fake .NewClientBuilder ().WithObjects (
2912
+ objects := []client.Object {
2872
2913
tc .cluster ,
2873
2914
tc .machine ,
2874
- m1 ,
2875
- m2 ,
2876
2915
emp ,
2877
2916
mcpBeingDeleted ,
2878
2917
empBeingDeleted ,
2879
- ).Build ()
2918
+ }
2919
+
2920
+ for _ , e := range tc .extraMachines {
2921
+ objects = append (objects , e )
2922
+ }
2923
+
2924
+ c := fake .NewClientBuilder ().WithObjects (objects ... ).Build ()
2880
2925
mr := & Reconciler {
2881
2926
Client : c ,
2882
2927
}
0 commit comments