@@ -79,13 +79,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
79
79
}
80
80
result , err := k .Reconcile (request )
81
81
if err != nil {
82
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
82
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
83
83
}
84
84
if result .Requeue == true {
85
- t .Fatal ("did not expected to requeue" )
85
+ t .Fatal ("did not expect to requeue" )
86
86
}
87
87
if result .RequeueAfter != time .Duration (0 ) {
88
- t .Fatal ("did not expected to requeue after" )
88
+ t .Fatal ("did not expect to requeue after" )
89
89
}
90
90
}
91
91
@@ -142,13 +142,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasBootstrapData(
142
142
}
143
143
result , err := k .Reconcile (request )
144
144
if err != nil {
145
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
145
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
146
146
}
147
147
if result .Requeue == true {
148
- t .Fatal ("did not expected to requeue" )
148
+ t .Fatal ("did not expect to requeue" )
149
149
}
150
150
if result .RequeueAfter != time .Duration (0 ) {
151
- t .Fatal ("did not expected to requeue after" )
151
+ t .Fatal ("did not expect to requeue after" )
152
152
}
153
153
}
154
154
@@ -218,7 +218,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
218
218
workerMachine := newWorkerMachine (cluster )
219
219
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
220
220
221
- controlPlaneMachine := newControlPlaneMachine (cluster )
221
+ controlPlaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
222
222
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneMachine , "control-plane-join-cfg" )
223
223
224
224
testcases := []struct {
@@ -284,7 +284,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
284
284
cluster := newCluster ("cluster" )
285
285
cluster .Status .InfrastructureReady = true
286
286
287
- controlPlaneMachine := newControlPlaneMachine (cluster )
287
+ controlPlaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
288
288
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
289
289
290
290
objects := []runtime.Object {
@@ -313,10 +313,10 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
313
313
t .Fatalf ("Failed to reconcile:\n %+v" , err )
314
314
}
315
315
if result .Requeue != false {
316
- t .Fatal ("did not expected to requeue" )
316
+ t .Fatal ("did not expect to requeue" )
317
317
}
318
318
if result .RequeueAfter != time .Duration (0 ) {
319
- t .Fatal ("did not expected to requeue after" )
319
+ t .Fatal ("did not expect to requeue after" )
320
320
}
321
321
322
322
cfg , err := getKubeadmConfig (myclient , "control-plane-init-cfg" )
@@ -382,7 +382,7 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
382
382
cluster .Status .ControlPlaneInitialized = true
383
383
cluster .Status .APIEndpoints = []clusterv1.APIEndpoint {{Host : "100.105.150.1" , Port : 6443 }}
384
384
385
- controlPlaneMachine := newControlPlaneMachine (cluster )
385
+ controlPlaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
386
386
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneMachine , "control-plane-join-cfg" )
387
387
controlPlaneJoinConfig .Spec .JoinConfiguration .ControlPlane = nil // Makes controlPlaneJoinConfig invalid for a control plane machine
388
388
@@ -442,10 +442,10 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
442
442
}
443
443
result , err := k .Reconcile (request )
444
444
if err != nil {
445
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
445
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
446
446
}
447
447
if result .Requeue == true {
448
- t .Fatal ("did not expected to requeue" )
448
+ t .Fatal ("did not expect to requeue" )
449
449
}
450
450
if result .RequeueAfter != 10 * time .Second {
451
451
t .Fatal ("expected to requeue after 10s" )
@@ -461,7 +461,7 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
461
461
workerMachine := newWorkerMachine (cluster )
462
462
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
463
463
464
- controlPlaneMachine := newControlPlaneMachine (cluster )
464
+ controlPlaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
465
465
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneMachine , "control-plane-join-cfg" )
466
466
467
467
objects := []runtime.Object {
@@ -488,18 +488,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
488
488
}
489
489
result , err := k .Reconcile (request )
490
490
if err != nil {
491
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
491
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
492
492
}
493
493
if result .Requeue == true {
494
- t .Fatal ("did not expected to requeue" )
494
+ t .Fatal ("did not expect to requeue" )
495
495
}
496
496
if result .RequeueAfter != time .Duration (0 ) {
497
- t .Fatal ("did not expected to requeue after" )
497
+ t .Fatal ("did not expect to requeue after" )
498
498
}
499
499
500
500
cfg , err := getKubeadmConfig (myclient , "worker-join-cfg" )
501
501
if err != nil {
502
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
502
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
503
503
}
504
504
505
505
if cfg .Status .Ready != true {
@@ -518,18 +518,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
518
518
}
519
519
result , err = k .Reconcile (request )
520
520
if err != nil {
521
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
521
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
522
522
}
523
523
if result .Requeue == true {
524
- t .Fatal ("did not expected to requeue" )
524
+ t .Fatal ("did not expect to requeue" )
525
525
}
526
526
if result .RequeueAfter != time .Duration (0 ) {
527
- t .Fatal ("did not expected to requeue after" )
527
+ t .Fatal ("did not expect to requeue after" )
528
528
}
529
529
530
530
cfg , err = getKubeadmConfig (myclient , "control-plane-join-cfg" )
531
531
if err != nil {
532
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
532
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
533
533
}
534
534
535
535
if cfg .Status .Ready != true {
@@ -543,11 +543,11 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
543
543
myremoteclient , _ := k .SecretsClientFactory .NewSecretsClient (nil , nil )
544
544
l , err := myremoteclient .List (metav1.ListOptions {})
545
545
if err != nil {
546
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
546
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
547
547
}
548
548
549
549
if len (l .Items ) != 2 {
550
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
550
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
551
551
}
552
552
}
553
553
@@ -968,6 +968,67 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
968
968
}
969
969
}
970
970
971
+ // Exactly one control plane machine initializes if there are multiple control plane machines defined
972
+ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitializes (t * testing.T ) {
973
+ cluster := newCluster ("cluster" )
974
+ cluster .Status .InfrastructureReady = true
975
+
976
+ controlPlaneInitMachineFirst := newControlPlaneMachine (cluster , "control-plane-init-machine-first" )
977
+ controlPlaneInitConfigFirst := newControlPlaneInitKubeadmConfig (controlPlaneInitMachineFirst , "control-plane-init-cfg-first" )
978
+
979
+ controlPlaneInitMachineSecond := newControlPlaneMachine (cluster , "control-plane-init-machine-second" )
980
+ controlPlaneInitConfigSecond := newControlPlaneInitKubeadmConfig (controlPlaneInitMachineSecond , "control-plane-init-cfg-second" )
981
+
982
+ objects := []runtime.Object {
983
+ cluster ,
984
+ controlPlaneInitMachineFirst ,
985
+ controlPlaneInitConfigFirst ,
986
+ controlPlaneInitMachineSecond ,
987
+ controlPlaneInitConfigSecond ,
988
+ }
989
+ myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
990
+ k := & KubeadmConfigReconciler {
991
+ Log : log .Log ,
992
+ Client : myclient ,
993
+ SecretsClientFactory : newFakeSecretFactory (),
994
+ KubeadmInitLock : & myInitLocker {},
995
+ }
996
+
997
+ request := ctrl.Request {
998
+ NamespacedName : types.NamespacedName {
999
+ Namespace : "default" ,
1000
+ Name : "control-plane-init-cfg-first" ,
1001
+ },
1002
+ }
1003
+ result , err := k .Reconcile (request )
1004
+ if err != nil {
1005
+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
1006
+ }
1007
+ if result .Requeue == true {
1008
+ t .Fatal ("did not expect to requeue" )
1009
+ }
1010
+ if result .RequeueAfter != time .Duration (0 ) {
1011
+ t .Fatal ("did not expect to requeue after" )
1012
+ }
1013
+
1014
+ request = ctrl.Request {
1015
+ NamespacedName : types.NamespacedName {
1016
+ Namespace : "default" ,
1017
+ Name : "control-plane-init-cfg-second" ,
1018
+ },
1019
+ }
1020
+ result , err = k .Reconcile (request )
1021
+ if err != nil {
1022
+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
1023
+ }
1024
+ if result .Requeue == true {
1025
+ t .Fatal ("did not expect to requeue" )
1026
+ }
1027
+ if result .RequeueAfter != 30 * time .Second {
1028
+ t .Fatal ("expected to requeue after 30s" )
1029
+ }
1030
+ }
1031
+
971
1032
// test utils
972
1033
973
1034
// newCluster return a CAPI cluster object
@@ -1016,8 +1077,8 @@ func newWorkerMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
1016
1077
return newMachine (cluster , "worker-machine" ) // machine by default is a worker node (not the bootstrapNode)
1017
1078
}
1018
1079
1019
- func newControlPlaneMachine (cluster * clusterv1.Cluster ) * clusterv1.Machine {
1020
- m := newMachine (cluster , "control-plane-machine" )
1080
+ func newControlPlaneMachine (cluster * clusterv1.Cluster , name string ) * clusterv1.Machine {
1081
+ m := newMachine (cluster , name )
1021
1082
m .Labels [clusterv1 .MachineControlPlaneLabelName ] = "true"
1022
1083
return m
1023
1084
}
@@ -1104,9 +1165,21 @@ func (f FakeSecretFactory) NewSecretsClient(client client.Client, cluster *clust
1104
1165
return f .client , nil
1105
1166
}
1106
1167
1107
- type myInitLocker struct {}
1168
+ type myInitLocker struct {
1169
+ locked bool
1170
+ }
1108
1171
1109
1172
func (m * myInitLocker ) Lock (_ context.Context , _ * clusterv1.Cluster , _ * clusterv1.Machine ) bool {
1173
+ if ! m .locked {
1174
+ m .locked = true
1175
+ return true
1176
+ }
1177
+ return false
1178
+ }
1179
+
1180
+ func (m * myInitLocker ) Unlock (_ context.Context , _ * clusterv1.Cluster ) bool {
1181
+ if m .locked {
1182
+ m .locked = false
1183
+ }
1110
1184
return true
1111
1185
}
1112
- func (m * myInitLocker ) Unlock (_ context.Context , _ * clusterv1.Cluster ) bool { return true }
0 commit comments