Skip to content

Commit db67880

Browse files
committed
fix: resolve finalizer removal conflict in HelmRepository deletion
- Add removeFinalizerWithRetry method to handle resource version conflicts - Use retry.RetryOnConflict to safely remove finalizers during deletion - Get latest resource version before attempting finalizer removal - Handle NotFound errors gracefully (resource already deleted) This fixes the StorageError with precondition failed UID mismatch that occurred when multiple operations tried to modify the same HelmRepository resource during deletion.
1 parent 31e3174 commit db67880

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

internal/controller/helmrepository_controller.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,8 @@ func (r *HelmRepositoryReconciler) reconcileDelete(ctx context.Context, repo *he
214214
// Don't block deletion, just log error
215215
}
216216

217-
// Remove Finalizer
218-
controllerutil.RemoveFinalizer(repo, utils.HelmRepositoryFinalizer)
219-
if err := r.Update(ctx, repo); err != nil {
217+
// Remove Finalizer with retry
218+
if err := r.removeFinalizerWithRetry(ctx, repo); err != nil {
220219
logger.Error(err, "Failed to remove finalizer")
221220
return ctrl.Result{}, err
222221
}
@@ -623,6 +622,27 @@ func sanitizeKubernetesName(name string) string {
623622
return result
624623
}
625624

625+
// removeFinalizerWithRetry removes finalizer with retry mechanism to handle conflicts
626+
func (r *HelmRepositoryReconciler) removeFinalizerWithRetry(ctx context.Context, repo *helmoperatorv1alpha1.HelmRepository) error {
627+
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
628+
// Get the latest version of the resource
629+
latest := &helmoperatorv1alpha1.HelmRepository{}
630+
if err := r.Get(ctx, client.ObjectKeyFromObject(repo), latest); err != nil {
631+
if apierrors.IsNotFound(err) {
632+
// Resource already deleted, consider success
633+
return nil
634+
}
635+
return err
636+
}
637+
638+
// Remove finalizer from the latest version
639+
controllerutil.RemoveFinalizer(latest, utils.HelmRepositoryFinalizer)
640+
641+
// Update the resource
642+
return r.Update(ctx, latest)
643+
})
644+
}
645+
626646
// SetupWithManager sets up the controller with the Manager.
627647
func (r *HelmRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
628648
return ctrl.NewControllerManagedBy(mgr).

internal/helm/client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@ type UninstallRequest struct {
183183

184184
// ReleaseInfo contains information about a release
185185
type ReleaseInfo struct {
186-
Name string
187-
Namespace string
188-
Revision int
189-
Status string
190-
Chart string
191-
AppVersion string
192-
Updated time.Time
193-
Description string
194-
FirstDeployed *time.Time
195-
LastDeployed *time.Time
186+
Name string
187+
Namespace string
188+
Revision int
189+
Status string
190+
Chart string
191+
AppVersion string
192+
Updated time.Time
193+
Description string
194+
FirstDeployed *time.Time
195+
LastDeployed *time.Time
196196
Notes string
197197
Values string
198198
OriginalValues string // Default values from the chart

internal/helm/repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ func (c *helmClient) RemoveRepository(ctx context.Context, name string) error {
148148
}
149149

150150
if !f.Has(name) {
151-
return fmt.Errorf("repository %s not found", name)
151+
// return fmt.Errorf("repository %s not found", name)
152+
return nil // If repository doesn't exist, just return nil to avoid error
152153
}
153154

154155
// Remove repository from file

0 commit comments

Comments
 (0)