@@ -223,13 +223,13 @@ func (r *KThreesConfigReconciler) joinControlplane(ctx context.Context, scope *S
223223
224224 serverURL := fmt .Sprintf ("https://%s" , scope .Cluster .Spec .ControlPlaneEndpoint .String ())
225225
226- tokn , err := r . retrieveToken (ctx , scope )
226+ tokn , err := token . Lookup (ctx , r . Client , client . ObjectKeyFromObject ( scope . Cluster ) )
227227 if err != nil {
228228 conditions .MarkFalse (scope .Config , bootstrapv1 .DataSecretAvailableCondition , bootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
229229 return err
230230 }
231231
232- configStruct := k3s .GenerateJoinControlPlaneConfig (serverURL , tokn ,
232+ configStruct := k3s .GenerateJoinControlPlaneConfig (serverURL , * tokn ,
233233 scope .Cluster .Spec .ControlPlaneEndpoint .Host ,
234234 scope .Config .Spec .ServerConfig ,
235235 scope .Config .Spec .AgentConfig )
@@ -284,13 +284,13 @@ func (r *KThreesConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
284284
285285 serverURL := fmt .Sprintf ("https://%s" , scope .Cluster .Spec .ControlPlaneEndpoint .String ())
286286
287- tokn , err := r . retrieveToken (ctx , scope )
287+ tokn , err := token . Lookup (ctx , r . Client , client . ObjectKeyFromObject ( scope . Cluster ) )
288288 if err != nil {
289289 conditions .MarkFalse (scope .Config , bootstrapv1 .DataSecretAvailableCondition , bootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
290290 return err
291291 }
292292
293- configStruct := k3s .GenerateWorkerConfig (serverURL , tokn , scope .Config .Spec .ServerConfig , scope .Config .Spec .AgentConfig )
293+ configStruct := k3s .GenerateWorkerConfig (serverURL , * tokn , scope .Config .Spec .ServerConfig , scope .Config .Spec .AgentConfig )
294294
295295 b , err := kubeyaml .Marshal (configStruct )
296296 if err != nil {
@@ -424,7 +424,7 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
424424 }
425425 conditions .MarkTrue (scope .Config , bootstrapv1 .CertificatesAvailableCondition )
426426
427- token , err := r . generateAndStoreToken (ctx , scope )
427+ token , err := token . Lookup (ctx , r . Client , client . ObjectKeyFromObject ( scope . Cluster ) )
428428 if err != nil {
429429 return ctrl.Result {}, err
430430 }
@@ -433,7 +433,7 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
433433 // For now just use the etcd option
434434 configStruct := k3s .GenerateInitControlPlaneConfig (
435435 scope .Cluster .Spec .ControlPlaneEndpoint .Host ,
436- token ,
436+ * token ,
437437 scope .Config .Spec .ServerConfig ,
438438 scope .Config .Spec .AgentConfig )
439439
@@ -480,64 +480,6 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
480480 return r .reconcileKubeconfig (ctx , scope )
481481}
482482
483- func (r * KThreesConfigReconciler ) generateAndStoreToken (ctx context.Context , scope * Scope ) (string , error ) {
484- tokn , err := token .Random (16 )
485- if err != nil {
486- return "" , err
487- }
488-
489- secret := & corev1.Secret {
490- ObjectMeta : metav1.ObjectMeta {
491- Name : token .Name (scope .Cluster .Name ),
492- Namespace : scope .Config .Namespace ,
493- Labels : map [string ]string {
494- clusterv1 .ClusterNameLabel : scope .Cluster .Name ,
495- },
496- OwnerReferences : []metav1.OwnerReference {
497- {
498- APIVersion : clusterv1 .GroupVersion .String (),
499- Kind : "Cluster" ,
500- Name : scope .Cluster .Name ,
501- UID : scope .Cluster .UID ,
502- Controller : pointer .Bool (true ),
503- },
504- },
505- },
506- Data : map [string ][]byte {
507- "value" : []byte (tokn ),
508- },
509- Type : clusterv1 .ClusterSecretType ,
510- }
511-
512- // as secret creation and scope.Config status patch are not atomic operations
513- // it is possible that secret creation happens but the config.Status patches are not applied
514- if err := r .Client .Create (ctx , secret ); err != nil {
515- if ! apierrors .IsAlreadyExists (err ) {
516- return "" , fmt .Errorf ("failed to create token for KThreesConfig %s/%s: %w" , scope .Config .Namespace , scope .Config .Name , err )
517- }
518- // r.Log.Info("bootstrap data secret for KThreesConfig already exists, updating", "secret", secret.Name, "KThreesConfig", scope.Config.Name)
519- if err := r .Client .Update (ctx , secret ); err != nil {
520- return "" , fmt .Errorf ("failed to update bootstrap token secret for KThreesConfig %s/%s: %w" , scope .Config .Namespace , scope .Config .Name , err )
521- }
522- }
523-
524- return tokn , nil
525- }
526-
527- func (r * KThreesConfigReconciler ) retrieveToken (ctx context.Context , scope * Scope ) (string , error ) {
528- secret := & corev1.Secret {}
529- obj := client.ObjectKey {
530- Namespace : scope .Config .Namespace ,
531- Name : token .Name (scope .Cluster .Name ),
532- }
533-
534- if err := r .Client .Get (ctx , obj , secret ); err != nil {
535- return "" , fmt .Errorf ("failed to get token for KThreesConfig %s/%s: %w" , scope .Config .Namespace , scope .Config .Name , err )
536- }
537-
538- return string (secret .Data ["value" ]), nil
539- }
540-
541483func (r * KThreesConfigReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
542484 if r .KThreesInitLock == nil {
543485 r .KThreesInitLock = locking .NewControlPlaneInitMutex (ctrl .Log .WithName ("init-locker" ), mgr .GetClient ())
0 commit comments