@@ -220,7 +220,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
220
220
workerMachine := newWorkerMachine (cluster )
221
221
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
222
222
223
- controlPaneMachine := newControlPlaneMachine (cluster )
223
+ controlPaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
224
224
controlPaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPaneMachine , "control-plane-join-cfg" )
225
225
226
226
testcases := []struct {
@@ -286,7 +286,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
286
286
cluster := newCluster ("cluster" )
287
287
cluster .Status .InfrastructureReady = true
288
288
289
- controlPlaneMachine := newControlPlaneMachine (cluster )
289
+ controlPlaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
290
290
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
291
291
292
292
objects := []runtime.Object {
@@ -384,7 +384,7 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
384
384
cluster .Status .ControlPlaneInitialized = true
385
385
cluster .Status .APIEndpoints = []clusterv1.APIEndpoint {{Host : "100.105.150.1" , Port : 6443 }}
386
386
387
- controlPaneMachine := newControlPlaneMachine (cluster )
387
+ controlPaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
388
388
controlPaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPaneMachine , "control-plane-join-cfg" )
389
389
controlPaneJoinConfig .Spec .JoinConfiguration .ControlPlane = nil // Makes controlPaneJoinConfig invalid for a control plane machine
390
390
@@ -463,7 +463,7 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
463
463
workerMachine := newWorkerMachine (cluster )
464
464
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
465
465
466
- controlPaneMachine := newControlPlaneMachine (cluster )
466
+ controlPaneMachine := newControlPlaneMachine (cluster , "control-plane-machine" )
467
467
controlPaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPaneMachine , "control-plane-join-cfg" )
468
468
469
469
objects := []runtime.Object {
@@ -970,6 +970,67 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
970
970
}
971
971
}
972
972
973
+ // Exactly one control plane machine initializes if there are multiple control plane machines defined
974
+ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitializes (t * testing.T ) {
975
+ cluster := newCluster ("cluster" )
976
+ cluster .Status .InfrastructureReady = true
977
+
978
+ controlPlaneInitMachineFirst := newControlPlaneMachine (cluster , "control-plane-init-machine-first" )
979
+ controlPlaneInitConfigFirst := newControlPlaneInitKubeadmConfig (controlPlaneInitMachineFirst , "control-plane-init-cfg-first" )
980
+
981
+ controlPlaneInitMachineSecond := newControlPlaneMachine (cluster , "control-plane-init-machine-second" )
982
+ controlPlaneInitConfigSecond := newControlPlaneInitKubeadmConfig (controlPlaneInitMachineSecond , "control-plane-init-cfg-second" )
983
+
984
+ objects := []runtime.Object {
985
+ cluster ,
986
+ controlPlaneInitMachineFirst ,
987
+ controlPlaneInitConfigFirst ,
988
+ controlPlaneInitMachineSecond ,
989
+ controlPlaneInitConfigSecond ,
990
+ }
991
+ myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
992
+ k := & KubeadmConfigReconciler {
993
+ Log : log .Log ,
994
+ Client : myclient ,
995
+ SecretsClientFactory : newFakeSecretFactory (),
996
+ KubeadmInitLock : & myInitLocker {},
997
+ }
998
+
999
+ request := ctrl.Request {
1000
+ NamespacedName : types.NamespacedName {
1001
+ Namespace : "default" ,
1002
+ Name : "control-plane-init-cfg-first" ,
1003
+ },
1004
+ }
1005
+ result , err := k .Reconcile (request )
1006
+ if err != nil {
1007
+ t .Fatal (fmt .Sprintf ("Failed to reconcile:\n %+v" , err ))
1008
+ }
1009
+ if result .Requeue == true {
1010
+ t .Fatal ("did not expected to requeue" )
1011
+ }
1012
+ if result .RequeueAfter != time .Duration (0 ) {
1013
+ t .Fatal ("did not expected to requeue after" )
1014
+ }
1015
+
1016
+ request = ctrl.Request {
1017
+ NamespacedName : types.NamespacedName {
1018
+ Namespace : "default" ,
1019
+ Name : "control-plane-init-cfg-second" ,
1020
+ },
1021
+ }
1022
+ result , err = k .Reconcile (request )
1023
+ if err != nil {
1024
+ t .Fatal (fmt .Sprintf ("Failed to reconcile:\n %+v" , err ))
1025
+ }
1026
+ if result .Requeue == true {
1027
+ t .Fatal ("did not expected to requeue" )
1028
+ }
1029
+ if result .RequeueAfter != 30 * time .Second {
1030
+ t .Fatal ("expected to requeue after 30s" )
1031
+ }
1032
+ }
1033
+
973
1034
// test utils
974
1035
975
1036
// newCluster return a CAPI cluster object
@@ -1018,8 +1079,8 @@ func newWorkerMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
1018
1079
return newMachine (cluster , "worker-machine" ) // machine by default is a worker node (not the bootstrapNode)
1019
1080
}
1020
1081
1021
- func newControlPlaneMachine (cluster * clusterv1.Cluster ) * clusterv1.Machine {
1022
- m := newMachine (cluster , "control-plane-machine" )
1082
+ func newControlPlaneMachine (cluster * clusterv1.Cluster , name string ) * clusterv1.Machine {
1083
+ m := newMachine (cluster , name )
1023
1084
m .Labels [clusterv1 .MachineControlPlaneLabelName ] = "true"
1024
1085
return m
1025
1086
}
@@ -1106,9 +1167,21 @@ func (f FakeSecretFactory) NewSecretsClient(client client.Client, cluster *clust
1106
1167
return f .client , nil
1107
1168
}
1108
1169
1109
- type myInitLocker struct {}
1170
+ type myInitLocker struct {
1171
+ locked bool
1172
+ }
1110
1173
1111
1174
func (m * myInitLocker ) Lock (_ context.Context , _ * clusterv1.Cluster , _ * clusterv1.Machine ) bool {
1175
+ if ! m .locked {
1176
+ m .locked = true
1177
+ return true
1178
+ }
1179
+ return false
1180
+ }
1181
+
1182
+ func (m * myInitLocker ) Unlock (_ context.Context , _ * clusterv1.Cluster ) bool {
1183
+ if m .locked {
1184
+ m .locked = false
1185
+ }
1112
1186
return true
1113
1187
}
1114
- func (m * myInitLocker ) Unlock (_ context.Context , _ * clusterv1.Cluster ) bool { return true }
0 commit comments