@@ -223,13 +223,13 @@ func (r *KThreesConfigReconciler) joinControlplane(ctx context.Context, scope *S
223
223
224
224
serverURL := fmt .Sprintf ("https://%s" , scope .Cluster .Spec .ControlPlaneEndpoint .String ())
225
225
226
- tokn , err := r . retrieveToken (ctx , scope )
226
+ tokn , err := token . Lookup (ctx , r . Client , client . ObjectKeyFromObject ( scope . Cluster ) )
227
227
if err != nil {
228
228
conditions .MarkFalse (scope .Config , bootstrapv1 .DataSecretAvailableCondition , bootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
229
229
return err
230
230
}
231
231
232
- configStruct := k3s .GenerateJoinControlPlaneConfig (serverURL , tokn ,
232
+ configStruct := k3s .GenerateJoinControlPlaneConfig (serverURL , * tokn ,
233
233
scope .Cluster .Spec .ControlPlaneEndpoint .Host ,
234
234
scope .Config .Spec .ServerConfig ,
235
235
scope .Config .Spec .AgentConfig )
@@ -284,13 +284,13 @@ func (r *KThreesConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
284
284
285
285
serverURL := fmt .Sprintf ("https://%s" , scope .Cluster .Spec .ControlPlaneEndpoint .String ())
286
286
287
- tokn , err := r . retrieveToken (ctx , scope )
287
+ tokn , err := token . Lookup (ctx , r . Client , client . ObjectKeyFromObject ( scope . Cluster ) )
288
288
if err != nil {
289
289
conditions .MarkFalse (scope .Config , bootstrapv1 .DataSecretAvailableCondition , bootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
290
290
return err
291
291
}
292
292
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 )
294
294
295
295
b , err := kubeyaml .Marshal (configStruct )
296
296
if err != nil {
@@ -424,7 +424,7 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
424
424
}
425
425
conditions .MarkTrue (scope .Config , bootstrapv1 .CertificatesAvailableCondition )
426
426
427
- token , err := r . generateAndStoreToken (ctx , scope )
427
+ token , err := token . Lookup (ctx , r . Client , client . ObjectKeyFromObject ( scope . Cluster ) )
428
428
if err != nil {
429
429
return ctrl.Result {}, err
430
430
}
@@ -433,7 +433,7 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
433
433
// For now just use the etcd option
434
434
configStruct := k3s .GenerateInitControlPlaneConfig (
435
435
scope .Cluster .Spec .ControlPlaneEndpoint .Host ,
436
- token ,
436
+ * token ,
437
437
scope .Config .Spec .ServerConfig ,
438
438
scope .Config .Spec .AgentConfig )
439
439
@@ -480,64 +480,6 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
480
480
return r .reconcileKubeconfig (ctx , scope )
481
481
}
482
482
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
-
541
483
func (r * KThreesConfigReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
542
484
if r .KThreesInitLock == nil {
543
485
r .KThreesInitLock = locking .NewControlPlaneInitMutex (ctrl .Log .WithName ("init-locker" ), mgr .GetClient ())
0 commit comments