diff --git a/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go b/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go index af698cad812f..a937d5cd83b2 100644 --- a/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go +++ b/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go @@ -277,7 +277,7 @@ func (r *KubeadmConfigReconciler) reconcile(ctx context.Context, scope *Scope, c // If the BootstrapToken has been generated for a join but the config owner has no nodeRefs, // this indicates that the node has not yet joined and the token in the join config has not // been consumed and it may need a refresh. - return r.refreshBootstrapTokenIfNeeded(ctx, config, cluster) + return r.refreshBootstrapTokenIfNeeded(ctx, config, cluster, scope) } if configOwner.IsMachinePool() { // If the BootstrapToken has been generated and infrastructure is ready but the configOwner is a MachinePool, @@ -315,8 +315,7 @@ func (r *KubeadmConfigReconciler) reconcile(ctx context.Context, scope *Scope, c return r.joinWorker(ctx, scope) } -func (r *KubeadmConfigReconciler) refreshBootstrapTokenIfNeeded(ctx context.Context, config *bootstrapv1.KubeadmConfig, cluster *clusterv1.Cluster) (ctrl.Result, error) { - log := ctrl.LoggerFrom(ctx) +func (r *KubeadmConfigReconciler) refreshBootstrapTokenIfNeeded(ctx context.Context, config *bootstrapv1.KubeadmConfig, cluster *clusterv1.Cluster, scope *Scope) (ctrl.Result, error) { log := ctrl.LoggerFrom(ctx) token := config.Spec.JoinConfiguration.Discovery.BootstrapToken.Token remoteClient, err := r.Tracker.GetClient(ctx, util.ObjectKey(cluster)) @@ -326,6 +325,20 @@ func (r *KubeadmConfigReconciler) refreshBootstrapTokenIfNeeded(ctx context.Cont secret, err := getToken(ctx, remoteClient, token) if err != nil { + if apierrors.IsNotFound(err) { + log.Info("Bootstrap token not found, creating new bootstrap token") + token, err := createToken(ctx, remoteClient, r.TokenTTL) + if err != nil { + return ctrl.Result{}, errors.Wrapf(err, "failed to create new bootstrap token") + } + + config.Spec.JoinConfiguration.Discovery.BootstrapToken.Token = token + log.V(3).Info("Altering JoinConfiguration.Discovery.BootstrapToken.Token") + + // update the bootstrap data + return r.joinWorker(ctx, scope) + } + return ctrl.Result{}, errors.Wrapf(err, "failed to get bootstrap token secret in order to refresh it") } log = log.WithValues("Secret", klog.KObj(secret))