@@ -117,8 +117,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
117
117
return ctrl.Result {}, err
118
118
}
119
119
120
- // Check for infrastructure ready. If it's not ready then we will requeue the machine until it is.
121
- // The cluster-api machine controller set this value.
122
120
if ! cluster .Status .InfrastructureReady {
123
121
log .Info ("Infrastructure is not ready, waiting until ready." )
124
122
return ctrl.Result {}, nil
@@ -207,14 +205,13 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
207
205
208
206
certificates , err := r .getClusterCertificates (ctx , cluster .GetName (), config .GetNamespace ())
209
207
if err != nil {
210
- if apierrors .IsNotFound (err ) {
211
- certificates , err = r .createClusterCertificates (ctx , cluster .GetName (), config )
212
- if err != nil {
213
- log .Error (err , "unable to create cluster certificates" )
214
- return ctrl.Result {}, err
215
- }
216
- } else {
217
- log .Error (err , "unable to lookup cluster certificates" )
208
+ log .Error (err , "unable to lookup cluster certificates" )
209
+ return ctrl.Result {}, err
210
+ }
211
+ if certificates == nil {
212
+ certificates , err = r .createClusterCertificates (ctx , cluster .GetName (), config )
213
+ if err != nil {
214
+ log .Error (err , "unable to create cluster certificates" )
218
215
return ctrl.Result {}, err
219
216
}
220
217
}
@@ -260,6 +257,21 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
260
257
return ctrl.Result {}, errors .New ("Control plane already exists for the cluster, only KubeadmConfig objects with JoinConfiguration are allowed" )
261
258
}
262
259
260
+ // Get certificates to improve security of discovery
261
+ certificates , err := r .getClusterCertificates (ctx , cluster .GetName (), config .GetNamespace ())
262
+ if err != nil {
263
+ log .Error (err , "unable to lookup cluster certificates" )
264
+ return ctrl.Result {}, err
265
+ }
266
+ if certificates != nil {
267
+ hashes , err := certs .CertificateHashes (certificates .ClusterCA .Cert )
268
+ if err == nil {
269
+ config .Spec .JoinConfiguration .Discovery .BootstrapToken = & kubeadmv1beta1.BootstrapTokenDiscovery {
270
+ CACertHashes : hashes ,
271
+ }
272
+ }
273
+ }
274
+
263
275
// ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
264
276
if err := r .reconcileDiscovery (cluster , config ); err != nil {
265
277
if requeueErr , ok := errors .Cause (err ).(capierrors.HasRequeueAfterError ); ok {
@@ -286,6 +298,10 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
286
298
log .Error (err , "unable to locate cluster certificates" )
287
299
return ctrl.Result {}, err
288
300
}
301
+ if certificates == nil {
302
+ log .Info ("Cluster CAs have not been created; requeuing to try again" )
303
+ return ctrl.Result {RequeueAfter : 10 * time .Second }, nil
304
+ }
289
305
290
306
cloudJoinData , err := cloudinit .NewJoinControlPlane (& cloudinit.ControlPlaneJoinInput {
291
307
JoinConfiguration : joindata ,
@@ -429,8 +445,8 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(cluster *clusterv1.Cluster,
429
445
// if BootstrapToken already contains a CACertHashes or UnsafeSkipCAVerification, respect it; otherwise set for UnsafeSkipCAVerification
430
446
// TODO: set CACertHashes instead of UnsafeSkipCAVerification
431
447
if len (config .Spec .JoinConfiguration .Discovery .BootstrapToken .CACertHashes ) == 0 && ! config .Spec .JoinConfiguration .Discovery .BootstrapToken .UnsafeSkipCAVerification {
448
+ log .Info ("No CAs were provided. Falling back to insecure discover method by skipping CA Cert validation" )
432
449
config .Spec .JoinConfiguration .Discovery .BootstrapToken .UnsafeSkipCAVerification = true
433
- log .Info ("Altering JoinConfiguration.Discovery.BootstrapToken" , "UnsafeSkipCAVerification" , true )
434
450
}
435
451
436
452
return nil
@@ -482,11 +498,14 @@ func (r *KubeadmConfigReconciler) getClusterCertificates(ctx context.Context, cl
482
498
secret := & corev1.Secret {}
483
499
484
500
err := r .Get (ctx , types.NamespacedName {Name : ClusterCertificatesSecretName (clusterName ), Namespace : namespace }, secret )
485
- if err != nil {
501
+ switch {
502
+ case apierrors .IsNotFound (err ):
503
+ return nil , nil
504
+ case err != nil :
486
505
return nil , err
506
+ default :
507
+ return certs .NewCertificatesFromMap (secret .Data ), nil
487
508
}
488
-
489
- return certs .NewCertificatesFromMap (secret .Data ), nil
490
509
}
491
510
492
511
func (r * KubeadmConfigReconciler ) createClusterCertificates (ctx context.Context , clusterName string , config * bootstrapv1.KubeadmConfig ) (* certs.Certificates , error ) {
0 commit comments