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

Commit 279b3c3

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 b7c1ea3 commit 279b3c3

File tree

1 file changed

+115
-42
lines changed

1 file changed

+115
-42
lines changed

controllers/kubeadmconfig_controller_test.go

Lines changed: 115 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
119119
}
120120
result, err := k.Reconcile(request)
121121
if err != nil {
122-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
122+
t.Fatalf("Failed to reconcile:\n %+v", err)
123123
}
124124
if result.Requeue == true {
125-
t.Fatal("did not expected to requeue")
125+
t.Fatal("did not expect to requeue")
126126
}
127127
if result.RequeueAfter != time.Duration(0) {
128-
t.Fatal("did not expected to requeue after")
128+
t.Fatal("did not expect to requeue after")
129129
}
130130
}
131131

@@ -182,13 +182,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasBootstrapData(
182182
}
183183
result, err := k.Reconcile(request)
184184
if err != nil {
185-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
185+
t.Fatalf("Failed to reconcile:\n %+v", err)
186186
}
187187
if result.Requeue == true {
188-
t.Fatal("did not expected to requeue")
188+
t.Fatal("did not expect to requeue")
189189
}
190190
if result.RequeueAfter != time.Duration(0) {
191-
t.Fatal("did not expected to requeue after")
191+
t.Fatal("did not expect to requeue after")
192192
}
193193
}
194194

@@ -258,8 +258,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
258258
workerMachine := newWorkerMachine(cluster)
259259
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
260260

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")
263263

264264
testcases := []struct {
265265
name string
@@ -290,7 +290,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
290290
},
291291
objects: []runtime.Object{
292292
cluster,
293-
controlPlaneMachine,
293+
controlPlaneJoinMachine,
294294
controlPlaneJoinConfig,
295295
},
296296
},
@@ -324,12 +324,12 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
324324
cluster := newCluster("cluster")
325325
cluster.Status.InfrastructureReady = true
326326

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")
329329

330330
objects := []runtime.Object{
331331
cluster,
332-
controlPlaneMachine,
332+
controlPlaneInitMachine,
333333
controlPlaneInitConfig,
334334
}
335335
objects = append(objects, createSecrets(t, cluster, controlPlaneInitConfig)...)
@@ -353,10 +353,10 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
353353
t.Fatalf("Failed to reconcile:\n %+v", err)
354354
}
355355
if result.Requeue != false {
356-
t.Fatal("did not expected to requeue")
356+
t.Fatal("did not expect to requeue")
357357
}
358358
if result.RequeueAfter != time.Duration(0) {
359-
t.Fatal("did not expected to requeue after")
359+
t.Fatal("did not expect to requeue after")
360360
}
361361

362362
cfg, err := getKubeadmConfig(myclient, "control-plane-init-cfg")
@@ -414,10 +414,10 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
414414
cluster.Status.InfrastructureReady = true
415415
cluster.Status.ControlPlaneInitialized = true
416416
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")
419419

420-
controlPlaneJoinMachine := newControlPlaneMachine(cluster)
420+
controlPlaneJoinMachine := newControlPlaneMachine(cluster, "control-plane-join-machine")
421421
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPlaneJoinMachine, "control-plane-join-cfg")
422422
controlPlaneJoinConfig.Spec.JoinConfiguration.ControlPlane = nil // Makes controlPlaneJoinConfig invalid for a control plane machine
423423

@@ -453,8 +453,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
453453
cluster := newCluster("cluster")
454454
cluster.Status.InfrastructureReady = true
455455
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")
458458

459459
workerMachine := newWorkerMachine(cluster)
460460
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
@@ -482,10 +482,10 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
482482
}
483483
result, err := k.Reconcile(request)
484484
if err != nil {
485-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
485+
t.Fatalf("Failed to reconcile:\n %+v", err)
486486
}
487487
if result.Requeue == true {
488-
t.Fatal("did not expected to requeue")
488+
t.Fatal("did not expect to requeue")
489489
}
490490
if result.RequeueAfter != 10*time.Second {
491491
t.Fatal("expected to requeue after 10s")
@@ -497,20 +497,20 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
497497
cluster.Status.InfrastructureReady = true
498498
cluster.Status.ControlPlaneInitialized = true
499499
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")
502502

503503
workerMachine := newWorkerMachine(cluster)
504504
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
505505

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")
508508

509509
objects := []runtime.Object{
510510
cluster,
511511
workerMachine,
512512
workerJoinConfig,
513-
controlPlaneMachine,
513+
controlPlaneJoinMachine,
514514
controlPlaneJoinConfig,
515515
}
516516
objects = append(objects, createSecrets(t, cluster, initConfig)...)
@@ -530,18 +530,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
530530
}
531531
result, err := k.Reconcile(request)
532532
if err != nil {
533-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
533+
t.Fatalf("Failed to reconcile:\n %+v", err)
534534
}
535535
if result.Requeue == true {
536-
t.Fatal("did not expected to requeue")
536+
t.Fatal("did not expect to requeue")
537537
}
538538
if result.RequeueAfter != time.Duration(0) {
539-
t.Fatal("did not expected to requeue after")
539+
t.Fatal("did not expect to requeue after")
540540
}
541541

542542
cfg, err := getKubeadmConfig(myclient, "worker-join-cfg")
543543
if err != nil {
544-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
544+
t.Fatalf("Failed to reconcile:\n %+v", err)
545545
}
546546

547547
if cfg.Status.Ready != true {
@@ -560,18 +560,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
560560
}
561561
result, err = k.Reconcile(request)
562562
if err != nil {
563-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
563+
t.Fatalf("Failed to reconcile:\n %+v", err)
564564
}
565565
if result.Requeue == true {
566-
t.Fatal("did not expected to requeue")
566+
t.Fatal("did not expect to requeue")
567567
}
568568
if result.RequeueAfter != time.Duration(0) {
569-
t.Fatal("did not expected to requeue after")
569+
t.Fatal("did not expect to requeue after")
570570
}
571571

572572
cfg, err = getKubeadmConfig(myclient, "control-plane-join-cfg")
573573
if err != nil {
574-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
574+
t.Fatalf("Failed to reconcile:\n %+v", err)
575575
}
576576

577577
if cfg.Status.Ready != true {
@@ -585,11 +585,11 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
585585
myremoteclient, _ := k.SecretsClientFactory.NewSecretsClient(nil, nil)
586586
l, err := myremoteclient.List(metav1.ListOptions{})
587587
if err != nil {
588-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
588+
t.Fatalf("Failed to reconcile:\n %+v", err)
589589
}
590590

591591
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)
593593
}
594594
}
595595

@@ -898,7 +898,7 @@ func TestKubeadmConfigReconciler_Reconcile_AlwaysCheckCAVerificationUnlessReques
898898
Port: 6443,
899899
},
900900
}
901-
controlPlaneInitMachine := newControlPlaneMachine(cluster)
901+
controlPlaneInitMachine := newControlPlaneMachine(cluster, "my-control-plane-init-machine")
902902
initConfig := newControlPlaneInitKubeadmConfig(controlPlaneInitMachine, "my-control-plane-init-config")
903903

904904
controlPlaneMachineName := "my-machine"
@@ -1017,7 +1017,7 @@ func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist(t
10171017
cluster := newCluster("my-cluster")
10181018
cluster.Status.InfrastructureReady = true
10191019
cluster.Status.ControlPlaneInitialized = false
1020-
m := newControlPlaneMachine(cluster)
1020+
m := newControlPlaneMachine(cluster, "control-plane-machine")
10211021
configName := "my-config"
10221022
c := newControlPlaneInitKubeadmConfig(m, configName)
10231023
scrt := &corev1.Secret{
@@ -1044,6 +1044,67 @@ func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist(t
10441044
}
10451045
}
10461046

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+
10471108
// test utils
10481109

10491110
// newCluster return a CAPI cluster object
@@ -1092,8 +1153,8 @@ func newWorkerMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
10921153
return newMachine(cluster, "worker-machine") // machine by default is a worker node (not the bootstrapNode)
10931154
}
10941155

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)
10971158
m.Labels[clusterv1.MachineControlPlaneLabelName] = "true"
10981159
return m
10991160
}
@@ -1180,9 +1241,21 @@ func (f FakeSecretFactory) NewSecretsClient(client client.Client, cluster *clust
11801241
return f.client, nil
11811242
}
11821243

1183-
type myInitLocker struct{}
1244+
type myInitLocker struct {
1245+
locked bool
1246+
}
11841247

11851248
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+
}
11861260
return true
11871261
}
1188-
func (m *myInitLocker) Unlock(_ context.Context, _ *clusterv1.Cluster) bool { return true }

0 commit comments

Comments
 (0)