@@ -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,11 @@ 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
+ name : "no control plane members, deleting a worker " ,
2566
2611
cluster : & clusterv1.Cluster {
2567
2612
ObjectMeta : metav1.ObjectMeta {
2568
2613
Name : "test-cluster" ,
@@ -2576,7 +2621,8 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2576
2621
Labels : map [string ]string {
2577
2622
clusterv1 .ClusterNameLabel : "test-cluster" ,
2578
2623
},
2579
- Finalizers : []string {clusterv1 .MachineFinalizer },
2624
+ Finalizers : []string {clusterv1 .MachineFinalizer },
2625
+ DeletionTimestamp : & metav1.Time {Time : time .Now ().UTC ()},
2580
2626
},
2581
2627
Spec : clusterv1.MachineSpec {
2582
2628
ClusterName : "test-cluster" ,
@@ -2589,10 +2635,11 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2589
2635
},
2590
2636
},
2591
2637
},
2592
- expectedError : errNoControlPlaneNodes ,
2638
+ extraMachines : nil ,
2639
+ expectedError : nil ,
2593
2640
},
2594
2641
{
2595
- name : "is last control plane member" ,
2642
+ name : "is last control plane member, trying to delete it " ,
2596
2643
cluster : & clusterv1.Cluster {
2597
2644
ObjectMeta : metav1.ObjectMeta {
2598
2645
Name : "test-cluster" ,
@@ -2621,7 +2668,40 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2621
2668
},
2622
2669
},
2623
2670
},
2624
- expectedError : errNoControlPlaneNodes ,
2671
+ extraMachines : nil ,
2672
+ expectedError : errLastControlPlaneNode ,
2673
+ },
2674
+ {
2675
+ name : "only one control plane member, trying to delete a worker" ,
2676
+ cluster : & clusterv1.Cluster {
2677
+ ObjectMeta : metav1.ObjectMeta {
2678
+ Name : "test-cluster" ,
2679
+ Namespace : metav1 .NamespaceDefault ,
2680
+ },
2681
+ },
2682
+ machine : & clusterv1.Machine {
2683
+ ObjectMeta : metav1.ObjectMeta {
2684
+ Name : "created" ,
2685
+ Namespace : metav1 .NamespaceDefault ,
2686
+ Labels : map [string ]string {
2687
+ clusterv1 .ClusterNameLabel : "test-cluster" ,
2688
+ },
2689
+ Finalizers : []string {clusterv1 .MachineFinalizer },
2690
+ DeletionTimestamp : & metav1.Time {Time : time .Now ().UTC ()},
2691
+ },
2692
+ Spec : clusterv1.MachineSpec {
2693
+ ClusterName : "test-cluster" ,
2694
+ InfrastructureRef : corev1.ObjectReference {},
2695
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
2696
+ },
2697
+ Status : clusterv1.MachineStatus {
2698
+ NodeRef : & corev1.ObjectReference {
2699
+ Name : "test" ,
2700
+ },
2701
+ },
2702
+ },
2703
+ extraMachines : nil ,
2704
+ expectedError : nil ,
2625
2705
},
2626
2706
{
2627
2707
name : "has nodeRef and control plane is healthy" ,
@@ -2651,6 +2731,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2651
2731
},
2652
2732
},
2653
2733
},
2734
+ extraMachines : []* clusterv1.Machine {cp1 , cp2 },
2654
2735
expectedError : nil ,
2655
2736
},
2656
2737
{
@@ -2664,6 +2745,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2664
2745
},
2665
2746
},
2666
2747
machine : & clusterv1.Machine {},
2748
+ extraMachines : nil ,
2667
2749
expectedError : errClusterIsBeingDeleted ,
2668
2750
},
2669
2751
{
@@ -2702,6 +2784,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2702
2784
},
2703
2785
},
2704
2786
},
2787
+ extraMachines : []* clusterv1.Machine {cp1 , cp2 },
2705
2788
expectedError : nil ,
2706
2789
},
2707
2790
{
@@ -2740,6 +2823,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2740
2823
},
2741
2824
},
2742
2825
},
2826
+ extraMachines : nil ,
2743
2827
expectedError : errControlPlaneIsBeingDeleted ,
2744
2828
},
2745
2829
{
@@ -2778,6 +2862,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2778
2862
},
2779
2863
},
2780
2864
},
2865
+ extraMachines : nil ,
2781
2866
expectedError : errControlPlaneIsBeingDeleted ,
2782
2867
},
2783
2868
}
@@ -2822,61 +2907,19 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
2822
2907
t .Run (tc .name , func (t * testing.T ) {
2823
2908
g := NewWithT (t )
2824
2909
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 (
2910
+ objects := []client.Object {
2872
2911
tc .cluster ,
2873
2912
tc .machine ,
2874
- m1 ,
2875
- m2 ,
2876
2913
emp ,
2877
2914
mcpBeingDeleted ,
2878
2915
empBeingDeleted ,
2879
- ).Build ()
2916
+ }
2917
+
2918
+ for _ , e := range tc .extraMachines {
2919
+ objects = append (objects , e )
2920
+ }
2921
+
2922
+ c := fake .NewClientBuilder ().WithObjects (objects ... ).Build ()
2880
2923
mr := & Reconciler {
2881
2924
Client : c ,
2882
2925
}
0 commit comments