@@ -157,7 +157,7 @@ func (r *serviceAccountReconciler) provisionImagePullSecretForPrincipal(
157157) (expiresAt time.Time , _ error ) {
158158 logger := log .FromContext (ctx ).WithValues ("secret" , secretName , "principal" , principal )
159159
160- should , exp , err := r .shouldCreateOrRefreshImagePullSecret (ctx , logger , sa , secretName )
160+ should , exp , err := r .shouldCreateOrRefreshImagePullSecret (ctx , logger , sa , secretName , principal )
161161 if err != nil {
162162 return time.Time {}, fmt .Errorf ("failed to determine if an image pull secret should be created or refreshed: %w" , err )
163163 }
@@ -183,10 +183,10 @@ func (r *serviceAccountReconciler) provisionImagePullSecretForPrincipal(
183183}
184184
185185func (r * serviceAccountReconciler ) shouldCreateOrRefreshImagePullSecret (
186- ctx context.Context , logger logr.Logger , sa * corev1.ServiceAccount , name string ,
186+ ctx context.Context , logger logr.Logger , sa * corev1.ServiceAccount , secretName string , principal string ,
187187) (should bool , expiresAt time.Time , _ error ) {
188188 // Check if the image pull secret exists.
189- secretKey := client.ObjectKey {Namespace : sa .GetNamespace (), Name : name }
189+ secretKey := client.ObjectKey {Namespace : sa .GetNamespace (), Name : secretName }
190190
191191 secret := & corev1.Secret {}
192192 if err := r .Get (ctx , secretKey , secret ); err != nil {
@@ -198,6 +198,14 @@ func (r *serviceAccountReconciler) shouldCreateOrRefreshImagePullSecret(
198198 return false , time.Time {}, fmt .Errorf ("failed to check the existing of an image pull secret: %w" , err )
199199 }
200200
201+ // Check if the image pull secret was provisioned for the principal.
202+ if secret .Annotations [annotationKeyPrincipal ] != principal {
203+ logger .Info (
204+ "Image pull secret was provisioned for a different principal. Should be provisioned for the current one." ,
205+ )
206+ return true , time.Time {}, nil
207+ }
208+
201209 // Check if the image pull secret is attached to the ServiceAccount.
202210 if ! r .imagePullSecretAttached (sa , secret .GetName ()) {
203211 logger .Info ("Image pull secret is not attached to the ServiceAccount. Should be attached." )
@@ -248,7 +256,7 @@ func (r *serviceAccountReconciler) createOrRefreshImagePullSecret(
248256
249257 // Ensure an image pull secret from the access token.
250258 secret , err := buildImagePullSecret (
251- sa , name , sa .Annotations [annotationKeyRegistry ], username , token , expiresAt ,
259+ sa , name , sa .Annotations [annotationKeyRegistry ], username , token , principal , expiresAt ,
252260 )
253261 if err != nil {
254262 return nil , time.Time {}, fmt .Errorf ("failed to build image pull secret definition: %w" , err )
0 commit comments