@@ -922,13 +922,12 @@ func (r *KustomizationReconciler) finalize(ctx context.Context, kustomization ku
922922 objects , _ := ListObjectsInInventory (kustomization .Status .Inventory )
923923
924924 impersonation := NewKustomizeImpersonation (kustomization , r .Client , r .StatusPoller , r .DefaultServiceAccount )
925- kubeClient , _ , err := impersonation .GetClient (ctx )
926- if err != nil {
927- // when impersonation fails, log the stale objects and continue with the finalization
928- msg := fmt .Sprintf ("unable to prune objects: \n %s" , ssa .FmtUnstructuredList (objects ))
929- log .Error (fmt .Errorf ("failed to build kube client: %w" , err ), msg )
930- r .event (ctx , kustomization , kustomization .Status .LastAppliedRevision , events .EventSeverityError , msg , nil )
931- } else {
925+ if impersonation .CanFinalize (ctx ) {
926+ kubeClient , _ , err := impersonation .GetClient (ctx )
927+ if err != nil {
928+ return ctrl.Result {}, err
929+ }
930+
932931 resourceManager := ssa .NewResourceManager (kubeClient , nil , ssa.Owner {
933932 Field : r .ControllerName ,
934933 Group : kustomizev1 .GroupVersion .Group ,
@@ -953,6 +952,11 @@ func (r *KustomizationReconciler) finalize(ctx context.Context, kustomization ku
953952 if changeSet != nil && len (changeSet .Entries ) > 0 {
954953 r .event (ctx , kustomization , kustomization .Status .LastAppliedRevision , events .EventSeverityInfo , changeSet .String (), nil )
955954 }
955+ } else {
956+ // when the account to impersonate is gone, log the stale objects and continue with the finalization
957+ msg := fmt .Sprintf ("unable to prune objects: \n %s" , ssa .FmtUnstructuredList (objects ))
958+ log .Error (fmt .Errorf ("skiping pruning, failed to find account to impersonate" ), msg )
959+ r .event (ctx , kustomization , kustomization .Status .LastAppliedRevision , events .EventSeverityError , msg , nil )
956960 }
957961 }
958962
0 commit comments