@@ -227,7 +227,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
227
227
return ctrl.Result {}, err
228
228
}
229
229
230
- certificates := internalcluster .NewCertificatesForControlPlane (config .Spec .ClusterConfiguration )
230
+ certificates := internalcluster .NewCertificatesForInitialControlPlane (config .Spec .ClusterConfiguration )
231
231
if err := certificates .LookupOrGenerate (ctx , r .Client , cluster , config ); err != nil {
232
232
log .Error (err , "unable to lookup or create cluster certificates" )
233
233
return ctrl.Result {}, err
@@ -266,56 +266,41 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
266
266
if config .Spec .JoinConfiguration == nil {
267
267
log .Info ("Creating default JoinConfiguration" )
268
268
config .Spec .JoinConfiguration = & kubeadmv1beta1.JoinConfiguration {}
269
- if util .IsControlPlaneMachine (machine ) {
270
- config .Spec .JoinConfiguration .ControlPlane = & kubeadmv1beta1.JoinControlPlane {}
271
- }
272
- }
273
-
274
- certificates := internalcluster .NewCertificatesForWorker (config .Spec .JoinConfiguration .CACertPath )
275
- if err := certificates .Lookup (ctx , r .Client , cluster ); err != nil {
276
- log .Error (err , "unable to lookup cluster certificates" )
277
- return ctrl.Result {}, err
278
- }
279
- if err := certificates .EnsureAllExist (); err != nil {
280
- return ctrl.Result {}, err
281
269
}
282
270
283
- hashes , err := certificates .GetByPurpose (secret .ClusterCA ).Hashes ()
284
- if err != nil {
285
- log .Error (err , "Unable to generate Cluster CA certificate hashes" )
286
- return ctrl.Result {}, err
287
- }
288
- // TODO: move this into reconcile.Discovery so that defaults for the Discovery are all in the same place
289
- if config .Spec .JoinConfiguration .Discovery .BootstrapToken == nil {
290
- config .Spec .JoinConfiguration .Discovery .BootstrapToken = & kubeadmv1beta1.BootstrapTokenDiscovery {}
291
- }
292
- config .Spec .JoinConfiguration .Discovery .BootstrapToken .CACertHashes = hashes
271
+ // it's a control plane join
272
+ if util .IsControlPlaneMachine (machine ) {
273
+ if config .Spec .JoinConfiguration .ControlPlane == nil {
274
+ config .Spec .JoinConfiguration .ControlPlane = & kubeadmv1beta1.JoinControlPlane {}
275
+ }
293
276
294
- // ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
295
- if err := r .reconcileDiscovery (cluster , config ); err != nil {
296
- if requeueErr , ok := errors .Cause (err ).(capierrors.HasRequeueAfterError ); ok {
297
- log .Info (err .Error ())
298
- return ctrl.Result {RequeueAfter : requeueErr .GetRequeueAfter ()}, nil
277
+ certificates := internalcluster .NewCertificatesForJoiningControlPlane ()
278
+ if err := certificates .Lookup (ctx , r .Client , cluster ); err != nil {
279
+ log .Error (err , "unable to lookup cluster certificates" )
280
+ return ctrl.Result {}, err
281
+ }
282
+ if err := certificates .EnsureAllExist (); err != nil {
283
+ return ctrl.Result {}, err
299
284
}
300
- return ctrl.Result {}, err
301
- }
302
285
303
- joindata , err := kubeadmv1beta1 .ConfigurationToYAML (config .Spec .JoinConfiguration )
304
- if err != nil {
305
- log .Error (err , "failed to marshal join configuration" )
306
- return ctrl.Result {}, err
307
- }
286
+ // ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
287
+ if err := r .reconcileDiscovery (cluster , config , certificates ); err != nil {
288
+ if requeueErr , ok := errors .Cause (err ).(capierrors.HasRequeueAfterError ); ok {
289
+ log .Info (err .Error ())
290
+ return ctrl.Result {RequeueAfter : requeueErr .GetRequeueAfter ()}, nil
291
+ }
292
+ return ctrl.Result {}, err
293
+ }
308
294
309
- // it's a control plane join
310
- if util . IsControlPlaneMachine ( machine ) {
311
- if config . Spec . JoinConfiguration . ControlPlane == nil {
312
- return ctrl.Result {}, errors . New ( "Machine is a ControlPlane, but JoinConfiguration.ControlPlane is not set in the KubeadmConfig object" )
295
+ joinData , err := kubeadmv1beta1 . ConfigurationToYAML ( config . Spec . JoinConfiguration )
296
+ if err != nil {
297
+ log . Error ( err , "failed to marshal join configuration" )
298
+ return ctrl.Result {}, err
313
299
}
314
300
315
301
log .Info ("Creating BootstrapData for the join control plane" )
316
-
317
302
cloudJoinData , err := cloudinit .NewJoinControlPlane (& cloudinit.ControlPlaneJoinInput {
318
- JoinConfiguration : joindata ,
303
+ JoinConfiguration : joinData ,
319
304
Certificates : certificates ,
320
305
BaseUserData : cloudinit.BaseUserData {
321
306
AdditionalFiles : config .Spec .Files ,
@@ -335,7 +320,32 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
335
320
return ctrl.Result {}, nil
336
321
}
337
322
338
- // otherwise it is a node
323
+ // It's a worker join
324
+ certificates := internalcluster .NewCertificatesForWorker (config .Spec .JoinConfiguration .CACertPath )
325
+ if err := certificates .Lookup (ctx , r .Client , cluster ); err != nil {
326
+ log .Error (err , "unable to lookup cluster certificates" )
327
+ return ctrl.Result {}, err
328
+ }
329
+ if err := certificates .EnsureAllExist (); err != nil {
330
+ log .Error (err , "Missing certificates" )
331
+ return ctrl.Result {}, err
332
+ }
333
+
334
+ // ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
335
+ if err := r .reconcileDiscovery (cluster , config , certificates ); err != nil {
336
+ if requeueErr , ok := errors .Cause (err ).(capierrors.HasRequeueAfterError ); ok {
337
+ log .Info (err .Error ())
338
+ return ctrl.Result {RequeueAfter : requeueErr .GetRequeueAfter ()}, nil
339
+ }
340
+ return ctrl.Result {}, err
341
+ }
342
+
343
+ joinData , err := kubeadmv1beta1 .ConfigurationToYAML (config .Spec .JoinConfiguration )
344
+ if err != nil {
345
+ log .Error (err , "failed to marshal join configuration" )
346
+ return ctrl.Result {}, err
347
+ }
348
+
339
349
if config .Spec .JoinConfiguration .ControlPlane != nil {
340
350
return ctrl.Result {}, errors .New ("Machine is a Worker, but JoinConfiguration.ControlPlane is set in the KubeadmConfig object" )
341
351
}
@@ -350,7 +360,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
350
360
PostKubeadmCommands : config .Spec .PostKubeadmCommands ,
351
361
Users : config .Spec .Users ,
352
362
},
353
- JoinConfiguration : joindata ,
363
+ JoinConfiguration : joinData ,
354
364
})
355
365
if err != nil {
356
366
log .Error (err , "failed to create a worker join configuration" )
@@ -416,7 +426,7 @@ func (r *KubeadmConfigReconciler) MachineToBootstrapMapFunc(o handler.MapObject)
416
426
// The implementation func respect user provided discovery configurations, but in case some of them are missing, a valid BootstrapToken object
417
427
// is automatically injected into config.JoinConfiguration.Discovery.
418
428
// This allows to simplify configuration UX, by providing the option to delegate to CABPK the configuration of kubeadm join discovery.
419
- func (r * KubeadmConfigReconciler ) reconcileDiscovery (cluster * clusterv1.Cluster , config * bootstrapv1.KubeadmConfig ) error {
429
+ func (r * KubeadmConfigReconciler ) reconcileDiscovery (cluster * clusterv1.Cluster , config * bootstrapv1.KubeadmConfig , certificates internalcluster. Certificates ) error {
420
430
log := r .Log .WithValues ("kubeadmconfig" , fmt .Sprintf ("%s/%s" , config .Namespace , config .Name ))
421
431
422
432
// if config already contains a file discovery configuration, respect it without further validations
@@ -429,6 +439,16 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(cluster *clusterv1.Cluster,
429
439
config .Spec .JoinConfiguration .Discovery .BootstrapToken = & kubeadmv1beta1.BootstrapTokenDiscovery {}
430
440
}
431
441
442
+ // calculate the ca cert hashes if they are not already set
443
+ if len (config .Spec .JoinConfiguration .Discovery .BootstrapToken .CACertHashes ) == 0 {
444
+ hashes , err := certificates .GetByPurpose (secret .ClusterCA ).Hashes ()
445
+ if err != nil {
446
+ log .Error (err , "Unable to generate Cluster CA certificate hashes" )
447
+ return err
448
+ }
449
+ config .Spec .JoinConfiguration .Discovery .BootstrapToken .CACertHashes = hashes
450
+ }
451
+
432
452
// if BootstrapToken already contains an APIServerEndpoint, respect it; otherwise inject the APIServerEndpoint endpoint defined in cluster status
433
453
apiServerEndpoint := config .Spec .JoinConfiguration .Discovery .BootstrapToken .APIServerEndpoint
434
454
if apiServerEndpoint == "" {
0 commit comments