From a4f258b84f7e65e4f4fea93dc2bc5b6f6cc11f49 Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Wed, 15 Nov 2023 15:57:22 +0100 Subject: [PATCH] fix: delete sa token if already exists --- .../resources/pods/translate/sa_token_secret.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/controllers/resources/pods/translate/sa_token_secret.go b/pkg/controllers/resources/pods/translate/sa_token_secret.go index eda02592e6..95bf109f90 100644 --- a/pkg/controllers/resources/pods/translate/sa_token_secret.go +++ b/pkg/controllers/resources/pods/translate/sa_token_secret.go @@ -45,7 +45,20 @@ func SATokenSecret(ctx context.Context, pClient client.Client, vPod *corev1.Pod, existingSecret, err := GetSecretIfExists(ctx, pClient, vPod.Name, vPod.Namespace) if err != nil { return err - } else if existingSecret == nil { + } + + // check if we need to delete the secret + if existingSecret != nil { + err = pClient.Delete(ctx, existingSecret) + if err != nil && !kerrors.IsNotFound(err) { + return err + } + + existingSecret = nil + } + + // secret does not exist we need to create it + if existingSecret == nil { // create to secret with the given token secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -84,10 +97,8 @@ func SetPodAsOwner(ctx context.Context, pPod *corev1.Pod, pClient client.Client, } owners := secret.GetOwnerReferences() - if translate.Owner != nil { // check if the current owner is the vcluster service - for i, owner := range owners { if owner.UID == translate.Owner.GetUID() { // path this with current pod as owner instead