diff --git a/controllers/helmchart_controller.go b/controllers/helmchart_controller.go index a294c8cba..a43447465 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -527,7 +527,14 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj * if file != "" { defer func() { - os.Remove(file) + if err := os.Remove(file); err != nil { + r.eventLogf(ctx, + obj, + corev1.EventTypeWarning, + meta.FailedReason, + "failed to delete temporary credentials file: %s", + err) + } }() } diff --git a/controllers/helmrepository_controller_oci.go b/controllers/helmrepository_controller_oci.go index ba2d356d6..ac28f70fc 100644 --- a/controllers/helmrepository_controller_oci.go +++ b/controllers/helmrepository_controller_oci.go @@ -171,7 +171,7 @@ func (r *HelmRepositoryOCIReconciler) Reconcile(ctx context.Context, req ctrl.Re r.Metrics.RecordDuration(ctx, obj, start) }() - // Add finalizer first if not exist to avoid the race condition + // Add finalizer first if it doesn't exist to avoid the race condition // between init and delete if !controllerutil.ContainsFinalizer(obj, sourcev1.SourceFinalizer) { controllerutil.AddFinalizer(obj, sourcev1.SourceFinalizer) @@ -222,6 +222,7 @@ func (r *HelmRepositoryOCIReconciler) notify(oldObj, newObj *sourcev1.HelmReposi } func (r *HelmRepositoryOCIReconciler) reconcile(ctx context.Context, obj *sourcev1.HelmRepository, reconcilers []helmRepositoryOCIReconcileFunc) (sreconcile.Result, error) { + oldObj := obj.DeepCopy() // Mark as reconciling if generation differs. @@ -303,7 +304,7 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s registryClient, file, err := r.RegistryClientGenerator(logOpts != nil) if err != nil { e := &serror.Stalling{ - Err: fmt.Errorf("failed to create registry client:: %w", err), + Err: fmt.Errorf("failed to create registry client: %w", err), Reason: meta.FailedReason, } conditions.MarkFalse(obj, meta.ReadyCondition, e.Reason, e.Err.Error()) @@ -312,7 +313,17 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s if file != "" { defer func() { - os.Remove(file) + if err := os.Remove(file); err != nil { + log := ctrl.LoggerFrom(ctx) + log.Error(err, "failed to delete temporary credentials file") + r.Eventf( + obj, + corev1.EventTypeWarning, + meta.FailedReason, + "failed to delete temporary credentials file: %s", + err, + ) + } }() }