@@ -119,13 +119,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
119
119
}
120
120
result , err := k .Reconcile (request )
121
121
if err != nil {
122
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
122
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
123
123
}
124
124
if result .Requeue == true {
125
- t .Fatal ("did not expected to requeue" )
125
+ t .Fatal ("did not expect to requeue" )
126
126
}
127
127
if result .RequeueAfter != time .Duration (0 ) {
128
- t .Fatal ("did not expected to requeue after" )
128
+ t .Fatal ("did not expect to requeue after" )
129
129
}
130
130
}
131
131
@@ -182,13 +182,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasBootstrapData(
182
182
}
183
183
result , err := k .Reconcile (request )
184
184
if err != nil {
185
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
185
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
186
186
}
187
187
if result .Requeue == true {
188
- t .Fatal ("did not expected to requeue" )
188
+ t .Fatal ("did not expect to requeue" )
189
189
}
190
190
if result .RequeueAfter != time .Duration (0 ) {
191
- t .Fatal ("did not expected to requeue after" )
191
+ t .Fatal ("did not expect to requeue after" )
192
192
}
193
193
}
194
194
@@ -258,8 +258,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
258
258
workerMachine := newWorkerMachine (cluster )
259
259
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
260
260
261
- controlPlaneMachine := newControlPlaneMachine (cluster )
262
- controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneMachine , "control-plane-join-cfg" )
261
+ controlPlaneJoinMachine := newControlPlaneMachine (cluster , "control-plane-join-machine" )
262
+ controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneJoinMachine , "control-plane-join-cfg" )
263
263
264
264
testcases := []struct {
265
265
name string
@@ -290,7 +290,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
290
290
},
291
291
objects : []runtime.Object {
292
292
cluster ,
293
- controlPlaneMachine ,
293
+ controlPlaneJoinMachine ,
294
294
controlPlaneJoinConfig ,
295
295
},
296
296
},
@@ -324,12 +324,12 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
324
324
cluster := newCluster ("cluster" )
325
325
cluster .Status .InfrastructureReady = true
326
326
327
- controlPlaneMachine := newControlPlaneMachine (cluster )
328
- controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
327
+ controlPlaneInitMachine := newControlPlaneMachine (cluster , "control-plane-init-machine" )
328
+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "control-plane-init-cfg" )
329
329
330
330
objects := []runtime.Object {
331
331
cluster ,
332
- controlPlaneMachine ,
332
+ controlPlaneInitMachine ,
333
333
controlPlaneInitConfig ,
334
334
}
335
335
objects = append (objects , createSecrets (t , cluster , controlPlaneInitConfig )... )
@@ -353,10 +353,10 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
353
353
t .Fatalf ("Failed to reconcile:\n %+v" , err )
354
354
}
355
355
if result .Requeue != false {
356
- t .Fatal ("did not expected to requeue" )
356
+ t .Fatal ("did not expect to requeue" )
357
357
}
358
358
if result .RequeueAfter != time .Duration (0 ) {
359
- t .Fatal ("did not expected to requeue after" )
359
+ t .Fatal ("did not expect to requeue after" )
360
360
}
361
361
362
362
cfg , err := getKubeadmConfig (myclient , "control-plane-init-cfg" )
@@ -414,10 +414,10 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
414
414
cluster .Status .InfrastructureReady = true
415
415
cluster .Status .ControlPlaneInitialized = true
416
416
cluster .Status .APIEndpoints = []clusterv1.APIEndpoint {{Host : "100.105.150.1" , Port : 6443 }}
417
- controlPlaneMachine := newControlPlaneMachine (cluster )
418
- controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
417
+ controlPlaneInitMachine := newControlPlaneMachine (cluster , "control-plane-init-machine" )
418
+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "control-plane-init-cfg" )
419
419
420
- controlPlaneJoinMachine := newControlPlaneMachine (cluster )
420
+ controlPlaneJoinMachine := newControlPlaneMachine (cluster , "control-plane-join-machine" )
421
421
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneJoinMachine , "control-plane-join-cfg" )
422
422
controlPlaneJoinConfig .Spec .JoinConfiguration .ControlPlane = nil // Makes controlPlaneJoinConfig invalid for a control plane machine
423
423
@@ -453,8 +453,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
453
453
cluster := newCluster ("cluster" )
454
454
cluster .Status .InfrastructureReady = true
455
455
cluster .Status .ControlPlaneInitialized = true
456
- controlPlaneMachine := newControlPlaneMachine (cluster )
457
- controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
456
+ controlPlaneInitMachine := newControlPlaneMachine (cluster , "control-plane-init-machine" )
457
+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "control-plane-init-cfg" )
458
458
459
459
workerMachine := newWorkerMachine (cluster )
460
460
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
@@ -482,10 +482,10 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
482
482
}
483
483
result , err := k .Reconcile (request )
484
484
if err != nil {
485
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
485
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
486
486
}
487
487
if result .Requeue == true {
488
- t .Fatal ("did not expected to requeue" )
488
+ t .Fatal ("did not expect to requeue" )
489
489
}
490
490
if result .RequeueAfter != 10 * time .Second {
491
491
t .Fatal ("expected to requeue after 10s" )
@@ -497,20 +497,20 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
497
497
cluster .Status .InfrastructureReady = true
498
498
cluster .Status .ControlPlaneInitialized = true
499
499
cluster .Status .APIEndpoints = []clusterv1.APIEndpoint {{Host : "100.105.150.1" , Port : 6443 }}
500
- controlPlaneInitMachine := newControlPlaneMachine (cluster )
501
- initConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "my- control-plane-init-config" )
500
+ controlPlaneInitMachine := newControlPlaneMachine (cluster , "control-plane-init-machine" )
501
+ initConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "control-plane-init-config" )
502
502
503
503
workerMachine := newWorkerMachine (cluster )
504
504
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
505
505
506
- controlPlaneMachine := newControlPlaneMachine (cluster )
507
- controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneMachine , "control-plane-join-cfg" )
506
+ controlPlaneJoinMachine := newControlPlaneMachine (cluster , "control-plane-join-machine" )
507
+ controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneJoinMachine , "control-plane-join-cfg" )
508
508
509
509
objects := []runtime.Object {
510
510
cluster ,
511
511
workerMachine ,
512
512
workerJoinConfig ,
513
- controlPlaneMachine ,
513
+ controlPlaneJoinMachine ,
514
514
controlPlaneJoinConfig ,
515
515
}
516
516
objects = append (objects , createSecrets (t , cluster , initConfig )... )
@@ -530,18 +530,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
530
530
}
531
531
result , err := k .Reconcile (request )
532
532
if err != nil {
533
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
533
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
534
534
}
535
535
if result .Requeue == true {
536
- t .Fatal ("did not expected to requeue" )
536
+ t .Fatal ("did not expect to requeue" )
537
537
}
538
538
if result .RequeueAfter != time .Duration (0 ) {
539
- t .Fatal ("did not expected to requeue after" )
539
+ t .Fatal ("did not expect to requeue after" )
540
540
}
541
541
542
542
cfg , err := getKubeadmConfig (myclient , "worker-join-cfg" )
543
543
if err != nil {
544
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
544
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
545
545
}
546
546
547
547
if cfg .Status .Ready != true {
@@ -560,18 +560,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
560
560
}
561
561
result , err = k .Reconcile (request )
562
562
if err != nil {
563
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
563
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
564
564
}
565
565
if result .Requeue == true {
566
- t .Fatal ("did not expected to requeue" )
566
+ t .Fatal ("did not expect to requeue" )
567
567
}
568
568
if result .RequeueAfter != time .Duration (0 ) {
569
- t .Fatal ("did not expected to requeue after" )
569
+ t .Fatal ("did not expect to requeue after" )
570
570
}
571
571
572
572
cfg , err = getKubeadmConfig (myclient , "control-plane-join-cfg" )
573
573
if err != nil {
574
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
574
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
575
575
}
576
576
577
577
if cfg .Status .Ready != true {
@@ -585,11 +585,11 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
585
585
myremoteclient , _ := k .SecretsClientFactory .NewSecretsClient (nil , nil )
586
586
l , err := myremoteclient .List (metav1.ListOptions {})
587
587
if err != nil {
588
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
588
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
589
589
}
590
590
591
591
if len (l .Items ) != 2 {
592
- t .Fatal ( fmt . Sprintf ( "Failed to reconcile:\n %+v" , err ) )
592
+ t .Fatalf ( "Failed to reconcile:\n %+v" , err )
593
593
}
594
594
}
595
595
@@ -898,7 +898,7 @@ func TestKubeadmConfigReconciler_Reconcile_AlwaysCheckCAVerificationUnlessReques
898
898
Port : 6443 ,
899
899
},
900
900
}
901
- controlPlaneInitMachine := newControlPlaneMachine (cluster )
901
+ controlPlaneInitMachine := newControlPlaneMachine (cluster , "my-control-plane-init-machine" )
902
902
initConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "my-control-plane-init-config" )
903
903
904
904
controlPlaneMachineName := "my-machine"
@@ -1017,7 +1017,7 @@ func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist(t
1017
1017
cluster := newCluster ("my-cluster" )
1018
1018
cluster .Status .InfrastructureReady = true
1019
1019
cluster .Status .ControlPlaneInitialized = false
1020
- m := newControlPlaneMachine (cluster )
1020
+ m := newControlPlaneMachine (cluster , "control-plane-machine" )
1021
1021
configName := "my-config"
1022
1022
c := newControlPlaneInitKubeadmConfig (m , configName )
1023
1023
scrt := & corev1.Secret {
@@ -1044,6 +1044,67 @@ func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist(t
1044
1044
}
1045
1045
}
1046
1046
1047
+ // Exactly one control plane machine initializes if there are multiple control plane machines defined
1048
+ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitializes (t * testing.T ) {
1049
+ cluster := newCluster ("cluster" )
1050
+ cluster .Status .InfrastructureReady = true
1051
+
1052
+ controlPlaneInitMachineFirst := newControlPlaneMachine (cluster , "control-plane-init-machine-first" )
1053
+ controlPlaneInitConfigFirst := newControlPlaneInitKubeadmConfig (controlPlaneInitMachineFirst , "control-plane-init-cfg-first" )
1054
+
1055
+ controlPlaneInitMachineSecond := newControlPlaneMachine (cluster , "control-plane-init-machine-second" )
1056
+ controlPlaneInitConfigSecond := newControlPlaneInitKubeadmConfig (controlPlaneInitMachineSecond , "control-plane-init-cfg-second" )
1057
+
1058
+ objects := []runtime.Object {
1059
+ cluster ,
1060
+ controlPlaneInitMachineFirst ,
1061
+ controlPlaneInitConfigFirst ,
1062
+ controlPlaneInitMachineSecond ,
1063
+ controlPlaneInitConfigSecond ,
1064
+ }
1065
+ myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
1066
+ k := & KubeadmConfigReconciler {
1067
+ Log : log .Log ,
1068
+ Client : myclient ,
1069
+ SecretsClientFactory : newFakeSecretFactory (),
1070
+ KubeadmInitLock : & myInitLocker {},
1071
+ }
1072
+
1073
+ request := ctrl.Request {
1074
+ NamespacedName : types.NamespacedName {
1075
+ Namespace : "default" ,
1076
+ Name : "control-plane-init-cfg-first" ,
1077
+ },
1078
+ }
1079
+ result , err := k .Reconcile (request )
1080
+ if err != nil {
1081
+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
1082
+ }
1083
+ if result .Requeue == true {
1084
+ t .Fatal ("did not expect to requeue" )
1085
+ }
1086
+ if result .RequeueAfter != time .Duration (0 ) {
1087
+ t .Fatal ("did not expect to requeue after" )
1088
+ }
1089
+
1090
+ request = ctrl.Request {
1091
+ NamespacedName : types.NamespacedName {
1092
+ Namespace : "default" ,
1093
+ Name : "control-plane-init-cfg-second" ,
1094
+ },
1095
+ }
1096
+ result , err = k .Reconcile (request )
1097
+ if err != nil {
1098
+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
1099
+ }
1100
+ if result .Requeue == true {
1101
+ t .Fatal ("did not expect to requeue" )
1102
+ }
1103
+ if result .RequeueAfter != 30 * time .Second {
1104
+ t .Fatal ("expected to requeue after 30s" )
1105
+ }
1106
+ }
1107
+
1047
1108
// test utils
1048
1109
1049
1110
// newCluster return a CAPI cluster object
@@ -1092,8 +1153,8 @@ func newWorkerMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
1092
1153
return newMachine (cluster , "worker-machine" ) // machine by default is a worker node (not the bootstrapNode)
1093
1154
}
1094
1155
1095
- func newControlPlaneMachine (cluster * clusterv1.Cluster ) * clusterv1.Machine {
1096
- m := newMachine (cluster , "control-plane-machine" )
1156
+ func newControlPlaneMachine (cluster * clusterv1.Cluster , name string ) * clusterv1.Machine {
1157
+ m := newMachine (cluster , name )
1097
1158
m .Labels [clusterv1 .MachineControlPlaneLabelName ] = "true"
1098
1159
return m
1099
1160
}
@@ -1180,9 +1241,21 @@ func (f FakeSecretFactory) NewSecretsClient(client client.Client, cluster *clust
1180
1241
return f .client , nil
1181
1242
}
1182
1243
1183
- type myInitLocker struct {}
1244
+ type myInitLocker struct {
1245
+ locked bool
1246
+ }
1184
1247
1185
1248
func (m * myInitLocker ) Lock (_ context.Context , _ * clusterv1.Cluster , _ * clusterv1.Machine ) bool {
1249
+ if ! m .locked {
1250
+ m .locked = true
1251
+ return true
1252
+ }
1253
+ return false
1254
+ }
1255
+
1256
+ func (m * myInitLocker ) Unlock (_ context.Context , _ * clusterv1.Cluster ) bool {
1257
+ if m .locked {
1258
+ m .locked = false
1259
+ }
1186
1260
return true
1187
1261
}
1188
- func (m * myInitLocker ) Unlock (_ context.Context , _ * clusterv1.Cluster ) bool { return true }
0 commit comments