Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 165238d

Browse files
committed
add a test to ensure that exactly one control plane machine initializes if there are multiple control plane machines defined
1 parent 6e2b7d1 commit 165238d

File tree

1 file changed

+81
-8
lines changed

1 file changed

+81
-8
lines changed

controllers/kubeadmconfig_controller_test.go

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
220220
workerMachine := newWorkerMachine(cluster)
221221
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
222222

223-
controlPaneMachine := newControlPlaneMachine(cluster)
223+
controlPaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
224224
controlPaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPaneMachine, "control-plane-join-cfg")
225225

226226
testcases := []struct {
@@ -286,7 +286,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
286286
cluster := newCluster("cluster")
287287
cluster.Status.InfrastructureReady = true
288288

289-
controlPlaneMachine := newControlPlaneMachine(cluster)
289+
controlPlaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
290290
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig(controlPlaneMachine, "control-plane-init-cfg")
291291

292292
objects := []runtime.Object{
@@ -384,7 +384,7 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
384384
cluster.Status.ControlPlaneInitialized = true
385385
cluster.Status.APIEndpoints = []clusterv1.APIEndpoint{{Host: "100.105.150.1", Port: 6443}}
386386

387-
controlPaneMachine := newControlPlaneMachine(cluster)
387+
controlPaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
388388
controlPaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPaneMachine, "control-plane-join-cfg")
389389
controlPaneJoinConfig.Spec.JoinConfiguration.ControlPlane = nil // Makes controlPaneJoinConfig invalid for a control plane machine
390390

@@ -463,7 +463,7 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
463463
workerMachine := newWorkerMachine(cluster)
464464
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
465465

466-
controlPaneMachine := newControlPlaneMachine(cluster)
466+
controlPaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
467467
controlPaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPaneMachine, "control-plane-join-cfg")
468468

469469
objects := []runtime.Object{
@@ -970,6 +970,67 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
970970
}
971971
}
972972

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+
9731034
// test utils
9741035

9751036
// newCluster return a CAPI cluster object
@@ -1018,8 +1079,8 @@ func newWorkerMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
10181079
return newMachine(cluster, "worker-machine") // machine by default is a worker node (not the bootstrapNode)
10191080
}
10201081

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)
10231084
m.Labels[clusterv1.MachineControlPlaneLabelName] = "true"
10241085
return m
10251086
}
@@ -1106,9 +1167,21 @@ func (f FakeSecretFactory) NewSecretsClient(client client.Client, cluster *clust
11061167
return f.client, nil
11071168
}
11081169

1109-
type myInitLocker struct{}
1170+
type myInitLocker struct {
1171+
locked bool
1172+
}
11101173

11111174
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+
}
11121186
return true
11131187
}
1114-
func (m *myInitLocker) Unlock(_ context.Context, _ *clusterv1.Cluster) bool { return true }

0 commit comments

Comments
 (0)