@@ -92,11 +92,17 @@ func TestKubeadmConfigReconciler_MachineToBootstrapMapFuncReturn(t *testing.T) {
92
92
// Reconcile returns early if the kubeadm config is ready because it should never re-generate bootstrap data.
93
93
func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady (t * testing.T ) {
94
94
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 ()
96
98
config := newKubeadmConfig (metav1 .NamespaceDefault , "cfg" )
99
+ addKubeadmConfigToMachine (config , machine )
100
+
97
101
config .Status .Ready = true
98
102
99
103
objects := []client.Object {
104
+ cluster ,
105
+ machine ,
100
106
config ,
101
107
}
102
108
myclient := fake .NewClientBuilder ().WithObjects (objects ... ).Build ()
@@ -117,7 +123,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
117
123
g .Expect (result .RequeueAfter ).To (Equal (time .Duration (0 )))
118
124
}
119
125
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 .
121
127
func TestKubeadmConfigReconciler_TestSecretOwnerReferenceReconciliation (t * testing.T ) {
122
128
g := NewWithT (t )
123
129
@@ -249,14 +255,20 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfReferencedMachineIsNotFoun
249
255
// If the machine has bootstrap data secret reference, there is no need to generate more bootstrap data.
250
256
func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasDataSecretName (t * testing.T ) {
251
257
g := NewWithT (t )
258
+ cluster := builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).Build ()
259
+ cluster .Status .InfrastructureReady = true
260
+
252
261
machine := builder .Machine (metav1 .NamespaceDefault , "machine" ).
253
262
WithVersion ("v1.19.1" ).
263
+ WithClusterName ("cluster1" ).
254
264
WithBootstrapTemplate (bootstrapbuilder .KubeadmConfig (metav1 .NamespaceDefault , "cfg" ).Unstructured ()).
255
265
Build ()
256
266
machine .Spec .Bootstrap .DataSecretName = pointer .String ("something" )
257
267
258
268
config := newKubeadmConfig (metav1 .NamespaceDefault , "cfg" )
269
+ addKubeadmConfigToMachine (config , machine )
259
270
objects := []client.Object {
271
+ cluster ,
260
272
machine ,
261
273
config ,
262
274
}
@@ -273,9 +285,12 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasDataSecretName
273
285
},
274
286
}
275
287
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 ())
276
290
g .Expect (err ).NotTo (HaveOccurred ())
277
291
g .Expect (result .Requeue ).To (BeFalse ())
278
292
g .Expect (result .RequeueAfter ).To (Equal (time .Duration (0 )))
293
+ assertHasTrueCondition (g , myclient , request , bootstrapv1 .DataSecretAvailableCondition )
279
294
}
280
295
281
296
func TestKubeadmConfigReconciler_ReturnEarlyIfClusterInfraNotReady (t * testing.T ) {
@@ -328,35 +343,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasNoCluster(t *t
328
343
WithBootstrapTemplate (bootstrapbuilder .KubeadmConfig (metav1 .NamespaceDefault , "cfg" ).Unstructured ()).
329
344
Build ()
330
345
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 )
360
347
361
348
objects := []client.Object {
362
349
machine ,
@@ -390,6 +377,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfAssociatedClusterIsNotFoun
390
377
Build ()
391
378
config := newKubeadmConfig (metav1 .NamespaceDefault , "cfg" )
392
379
380
+ addKubeadmConfigToMachine (config , machine )
393
381
objects := []client.Object {
394
382
// intentionally omitting cluster
395
383
machine ,
@@ -430,7 +418,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
430
418
objects []client.Object
431
419
}{
432
420
{
433
- name : "requeue worker when control plane is not yet initialiezd " ,
421
+ name : "requeue worker when control plane is not yet initialized " ,
434
422
request : ctrl.Request {
435
423
NamespacedName : client.ObjectKey {
436
424
Namespace : workerJoinConfig .Namespace ,
@@ -482,11 +470,19 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
482
470
func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData (t * testing.T ) {
483
471
g := NewWithT (t )
484
472
473
+ configName := "control-plane-init-cfg"
485
474
cluster := builder .Cluster (metav1 .NamespaceDefault , "cluster" ).Build ()
475
+ cluster .Spec .ControlPlaneEndpoint = clusterv1.APIEndpoint {Host : "validhost" , Port : 6443 }
486
476
cluster .Status .InfrastructureReady = true
477
+ conditions .MarkTrue (cluster , clusterv1 .ControlPlaneInitializedCondition )
487
478
488
479
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
+
490
486
addKubeadmConfigToMachine (controlPlaneInitConfig , controlPlaneInitMachine )
491
487
492
488
objects := []client.Object {
@@ -499,8 +495,9 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
499
495
myclient := fake .NewClientBuilder ().WithObjects (objects ... ).Build ()
500
496
501
497
k := & KubeadmConfigReconciler {
502
- Client : myclient ,
503
- KubeadmInitLock : & myInitLocker {},
498
+ Client : myclient ,
499
+ KubeadmInitLock : & myInitLocker {},
500
+ remoteClientGetter : fakeremote .NewClusterClient ,
504
501
}
505
502
506
503
request := ctrl.Request {
@@ -509,6 +506,9 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
509
506
Name : "control-plane-init-cfg" ,
510
507
},
511
508
}
509
+ s := & corev1.Secret {}
510
+ g .Expect (myclient .Get (ctx , client.ObjectKey {Namespace : metav1 .NamespaceDefault , Name : configName }, s )).ToNot (Succeed ())
511
+
512
512
result , err := k .Reconcile (ctx , request )
513
513
g .Expect (err ).NotTo (HaveOccurred ())
514
514
g .Expect (result .Requeue ).To (BeFalse ())
@@ -522,6 +522,9 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
522
522
assertHasTrueCondition (g , myclient , request , bootstrapv1 .CertificatesAvailableCondition )
523
523
assertHasTrueCondition (g , myclient , request , bootstrapv1 .DataSecretAvailableCondition )
524
524
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 ))
525
528
// Ensure that we don't fail trying to refresh any bootstrap tokens
526
529
_ , err = k .Reconcile (ctx , request )
527
530
g .Expect (err ).NotTo (HaveOccurred ())
@@ -561,11 +564,15 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
561
564
request := ctrl.Request {
562
565
NamespacedName : client.ObjectKey {
563
566
Namespace : metav1 .NamespaceDefault ,
564
- Name : "control-plane-join-cfg" ,
567
+ Name : controlPlaneJoinConfig . Name ,
565
568
},
566
569
}
567
570
_ , err := k .Reconcile (ctx , request )
568
571
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 )
569
576
}
570
577
571
578
// 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
607
614
g .Expect (err ).NotTo (HaveOccurred ())
608
615
g .Expect (result .Requeue ).To (BeFalse ())
609
616
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 )
610
624
}
611
625
612
- func TestReconcileIfJoinNodesAndControlPlaneIsReady (t * testing.T ) {
626
+ func TestReconcileIfJoinCertificatesAvailableConditioninNodesAndControlPlaneIsReady (t * testing.T ) {
613
627
cluster := builder .Cluster (metav1 .NamespaceDefault , "cluster" ).Build ()
614
628
cluster .Status .InfrastructureReady = true
615
629
conditions .MarkTrue (cluster , clusterv1 .ControlPlaneInitializedCondition )
@@ -1848,6 +1862,17 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
1848
1862
g .Expect (err ).NotTo (HaveOccurred ())
1849
1863
g .Expect (result .Requeue ).To (BeFalse ())
1850
1864
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
+ }
1851
1876
}
1852
1877
1853
1878
// Patch should be applied if there is an error in reconcile.
@@ -2252,6 +2277,10 @@ func addKubeadmConfigToMachine(config *bootstrapv1.KubeadmConfig, machine *clust
2252
2277
},
2253
2278
}
2254
2279
2280
+ if machine .Spec .Bootstrap .ConfigRef == nil {
2281
+ machine .Spec .Bootstrap .ConfigRef = & corev1.ObjectReference {}
2282
+ }
2283
+
2255
2284
machine .Spec .Bootstrap .ConfigRef .Name = config .Name
2256
2285
machine .Spec .Bootstrap .ConfigRef .Namespace = config .Namespace
2257
2286
}
0 commit comments