Skip to content

Commit 484b4cf

Browse files
Fix unit test coverage in KubeadmConfig
Signed-off-by: killianmuldoon <[email protected]>
1 parent e274f44 commit 484b4cf

File tree

1 file changed

+66
-37
lines changed

1 file changed

+66
-37
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,17 @@ func TestKubeadmConfigReconciler_MachineToBootstrapMapFuncReturn(t *testing.T) {
9292
// Reconcile returns early if the kubeadm config is ready because it should never re-generate bootstrap data.
9393
func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *testing.T) {
9494
g := NewWithT(t)
95-
95+
cluster := builder.Cluster(metav1.NamespaceDefault, "cluster1").Build()
96+
cluster.Status.InfrastructureReady = true
97+
machine := builder.Machine(metav1.NamespaceDefault, "m1").WithClusterName("cluster1").Build()
9698
config := newKubeadmConfig(metav1.NamespaceDefault, "cfg")
99+
addKubeadmConfigToMachine(config, machine)
100+
97101
config.Status.Ready = true
98102

99103
objects := []client.Object{
104+
cluster,
105+
machine,
100106
config,
101107
}
102108
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
@@ -117,7 +123,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
117123
g.Expect(result.RequeueAfter).To(Equal(time.Duration(0)))
118124
}
119125

120-
// Reconcile returns early if the kubeadm config is ready because it should never re-generate bootstrap data.
126+
// Reconcile should update owner references on bootstrap secrets on creation and update.
121127
func TestKubeadmConfigReconciler_TestSecretOwnerReferenceReconciliation(t *testing.T) {
122128
g := NewWithT(t)
123129

@@ -249,14 +255,20 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfReferencedMachineIsNotFoun
249255
// If the machine has bootstrap data secret reference, there is no need to generate more bootstrap data.
250256
func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasDataSecretName(t *testing.T) {
251257
g := NewWithT(t)
258+
cluster := builder.Cluster(metav1.NamespaceDefault, "cluster1").Build()
259+
cluster.Status.InfrastructureReady = true
260+
252261
machine := builder.Machine(metav1.NamespaceDefault, "machine").
253262
WithVersion("v1.19.1").
263+
WithClusterName("cluster1").
254264
WithBootstrapTemplate(bootstrapbuilder.KubeadmConfig(metav1.NamespaceDefault, "cfg").Unstructured()).
255265
Build()
256266
machine.Spec.Bootstrap.DataSecretName = pointer.String("something")
257267

258268
config := newKubeadmConfig(metav1.NamespaceDefault, "cfg")
269+
addKubeadmConfigToMachine(config, machine)
259270
objects := []client.Object{
271+
cluster,
260272
machine,
261273
config,
262274
}
@@ -273,9 +285,12 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasDataSecretName
273285
},
274286
}
275287
result, err := k.Reconcile(ctx, request)
288+
actual := &bootstrapv1.KubeadmConfig{}
289+
g.Expect(myclient.Get(ctx, client.ObjectKey{Namespace: config.Namespace, Name: config.Name}, actual)).To(Succeed())
276290
g.Expect(err).NotTo(HaveOccurred())
277291
g.Expect(result.Requeue).To(BeFalse())
278292
g.Expect(result.RequeueAfter).To(Equal(time.Duration(0)))
293+
assertHasTrueCondition(g, myclient, request, bootstrapv1.DataSecretAvailableCondition)
279294
}
280295

281296
func TestKubeadmConfigReconciler_ReturnEarlyIfClusterInfraNotReady(t *testing.T) {
@@ -328,35 +343,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasNoCluster(t *t
328343
WithBootstrapTemplate(bootstrapbuilder.KubeadmConfig(metav1.NamespaceDefault, "cfg").Unstructured()).
329344
Build()
330345
config := newKubeadmConfig(metav1.NamespaceDefault, "cfg")
331-
332-
objects := []client.Object{
333-
machine,
334-
config,
335-
}
336-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
337-
338-
k := &KubeadmConfigReconciler{
339-
Client: myclient,
340-
}
341-
342-
request := ctrl.Request{
343-
NamespacedName: client.ObjectKey{
344-
Namespace: metav1.NamespaceDefault,
345-
Name: "cfg",
346-
},
347-
}
348-
_, err := k.Reconcile(ctx, request)
349-
g.Expect(err).NotTo(HaveOccurred())
350-
}
351-
352-
// This does not expect an error, hoping the machine gets updated with a cluster.
353-
func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfMachineDoesNotHaveAssociatedCluster(t *testing.T) {
354-
g := NewWithT(t)
355-
machine := builder.Machine(metav1.NamespaceDefault, "machine").
356-
WithVersion("v1.19.1").
357-
WithBootstrapTemplate(bootstrapbuilder.KubeadmConfig(metav1.NamespaceDefault, "cfg").Unstructured()).
358-
Build()
359-
config := newKubeadmConfig(metav1.NamespaceDefault, "cfg")
346+
addKubeadmConfigToMachine(config, machine)
360347

361348
objects := []client.Object{
362349
machine,
@@ -390,6 +377,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfAssociatedClusterIsNotFoun
390377
Build()
391378
config := newKubeadmConfig(metav1.NamespaceDefault, "cfg")
392379

380+
addKubeadmConfigToMachine(config, machine)
393381
objects := []client.Object{
394382
// intentionally omitting cluster
395383
machine,
@@ -430,7 +418,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
430418
objects []client.Object
431419
}{
432420
{
433-
name: "requeue worker when control plane is not yet initialiezd",
421+
name: "requeue worker when control plane is not yet initialized",
434422
request: ctrl.Request{
435423
NamespacedName: client.ObjectKey{
436424
Namespace: workerJoinConfig.Namespace,
@@ -482,11 +470,19 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
482470
func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T) {
483471
g := NewWithT(t)
484472

473+
configName := "control-plane-init-cfg"
485474
cluster := builder.Cluster(metav1.NamespaceDefault, "cluster").Build()
475+
cluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{Host: "validhost", Port: 6443}
486476
cluster.Status.InfrastructureReady = true
477+
conditions.MarkTrue(cluster, clusterv1.ControlPlaneInitializedCondition)
487478

488479
controlPlaneInitMachine := newControlPlaneMachine(cluster, "control-plane-init-machine")
489-
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig(controlPlaneInitMachine.Namespace, "control-plane-init-cfg")
480+
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig(controlPlaneInitMachine.Namespace, configName)
481+
controlPlaneInitConfig.Spec.JoinConfiguration = &bootstrapv1.JoinConfiguration{}
482+
controlPlaneInitConfig.Spec.JoinConfiguration.Discovery.BootstrapToken = &bootstrapv1.BootstrapTokenDiscovery{
483+
CACertHashes: []string{"...."},
484+
}
485+
490486
addKubeadmConfigToMachine(controlPlaneInitConfig, controlPlaneInitMachine)
491487

492488
objects := []client.Object{
@@ -499,8 +495,9 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
499495
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
500496

501497
k := &KubeadmConfigReconciler{
502-
Client: myclient,
503-
KubeadmInitLock: &myInitLocker{},
498+
Client: myclient,
499+
KubeadmInitLock: &myInitLocker{},
500+
remoteClientGetter: fakeremote.NewClusterClient,
504501
}
505502

506503
request := ctrl.Request{
@@ -509,6 +506,9 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
509506
Name: "control-plane-init-cfg",
510507
},
511508
}
509+
s := &corev1.Secret{}
510+
g.Expect(myclient.Get(ctx, client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: configName}, s)).ToNot(Succeed())
511+
512512
result, err := k.Reconcile(ctx, request)
513513
g.Expect(err).NotTo(HaveOccurred())
514514
g.Expect(result.Requeue).To(BeFalse())
@@ -522,6 +522,9 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
522522
assertHasTrueCondition(g, myclient, request, bootstrapv1.CertificatesAvailableCondition)
523523
assertHasTrueCondition(g, myclient, request, bootstrapv1.DataSecretAvailableCondition)
524524

525+
// Expect the Secret to exist, and for it to contain some data under the "value" key.
526+
g.Expect(myclient.Get(ctx, client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: configName}, s)).To(Succeed())
527+
g.Expect(len(s.Data["value"])).To(BeNumerically(">", 0))
525528
// Ensure that we don't fail trying to refresh any bootstrap tokens
526529
_, err = k.Reconcile(ctx, request)
527530
g.Expect(err).NotTo(HaveOccurred())
@@ -561,11 +564,15 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
561564
request := ctrl.Request{
562565
NamespacedName: client.ObjectKey{
563566
Namespace: metav1.NamespaceDefault,
564-
Name: "control-plane-join-cfg",
567+
Name: controlPlaneJoinConfig.Name,
565568
},
566569
}
567570
_, err := k.Reconcile(ctx, request)
568571
g.Expect(err).NotTo(HaveOccurred())
572+
actualConfig := &bootstrapv1.KubeadmConfig{}
573+
g.Expect(myclient.Get(ctx, client.ObjectKey{Namespace: controlPlaneJoinConfig.Namespace, Name: controlPlaneJoinConfig.Name}, actualConfig)).To(Succeed())
574+
assertHasTrueCondition(g, myclient, request, bootstrapv1.DataSecretAvailableCondition)
575+
assertHasTrueCondition(g, myclient, request, bootstrapv1.CertificatesAvailableCondition)
569576
}
570577

571578
// If there is no APIEndpoint but everything is ready then requeue in hopes of a new APIEndpoint showing up eventually.
@@ -607,9 +614,16 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
607614
g.Expect(err).NotTo(HaveOccurred())
608615
g.Expect(result.Requeue).To(BeFalse())
609616
g.Expect(result.RequeueAfter).To(Equal(10 * time.Second))
617+
618+
actualConfig := &bootstrapv1.KubeadmConfig{}
619+
g.Expect(myclient.Get(ctx, client.ObjectKey{Namespace: workerJoinConfig.Namespace, Name: workerJoinConfig.Name}, actualConfig)).To(Succeed())
620+
621+
// At this point the DataSecretAvailableCondition should not be set. CertificatesAvailableCondition should be true.
622+
g.Expect(conditions.Get(actualConfig, bootstrapv1.DataSecretAvailableCondition)).To(BeNil())
623+
assertHasTrueCondition(g, myclient, request, bootstrapv1.CertificatesAvailableCondition)
610624
}
611625

612-
func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
626+
func TestReconcileIfJoinCertificatesAvailableConditioninNodesAndControlPlaneIsReady(t *testing.T) {
613627
cluster := builder.Cluster(metav1.NamespaceDefault, "cluster").Build()
614628
cluster.Status.InfrastructureReady = true
615629
conditions.MarkTrue(cluster, clusterv1.ControlPlaneInitializedCondition)
@@ -1848,6 +1862,17 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
18481862
g.Expect(err).NotTo(HaveOccurred())
18491863
g.Expect(result.Requeue).To(BeFalse())
18501864
g.Expect(result.RequeueAfter).To(Equal(30 * time.Second))
1865+
confList := &bootstrapv1.KubeadmConfigList{}
1866+
g.Expect(myclient.List(ctx, confList)).To(Succeed())
1867+
for _, c := range confList.Items {
1868+
// Ensure the DataSecretName is only set for controlPlaneInitConfigFirst.
1869+
if c.Name == controlPlaneInitConfigFirst.Name {
1870+
g.Expect(*c.Status.DataSecretName).To(Not(BeEmpty()))
1871+
}
1872+
if c.Name == controlPlaneInitConfigSecond.Name {
1873+
g.Expect(c.Status.DataSecretName).To(BeNil())
1874+
}
1875+
}
18511876
}
18521877

18531878
// Patch should be applied if there is an error in reconcile.
@@ -2252,6 +2277,10 @@ func addKubeadmConfigToMachine(config *bootstrapv1.KubeadmConfig, machine *clust
22522277
},
22532278
}
22542279

2280+
if machine.Spec.Bootstrap.ConfigRef == nil {
2281+
machine.Spec.Bootstrap.ConfigRef = &corev1.ObjectReference{}
2282+
}
2283+
22552284
machine.Spec.Bootstrap.ConfigRef.Name = config.Name
22562285
machine.Spec.Bootstrap.ConfigRef.Namespace = config.Namespace
22572286
}

0 commit comments

Comments
 (0)