Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix finalize logging #417

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions controllers/application_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/retry"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -115,20 +116,23 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if containsString(application.GetFinalizers(), appFinalizerName) {
metrics.ApplicationDeletionTotalReqs.Inc()
// A finalizer is present for the Application CR, so make sure we do the necessary cleanup steps
if err := r.Finalize(ctx, &application, ghClient); err != nil {
if finalizeErr := r.Finalize(ctx, &application, ghClient); finalizeErr != nil {
log.Error(finalizeErr, "Unable to delete GitOps repository for application")
finalizeCounter, err := getCounterAnnotation(finalizeCount, &application)
if err == nil && finalizeCounter < 5 {
// The Finalize function failed, so increment the finalize count and return
setCounterAnnotation(finalizeCount, &application, finalizeCounter+1)
err := r.Update(ctx, &application)
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
err := r.Update(ctx, &application)
return err
})
if err != nil {
log.Error(err, "Error incrementing finalizer count on resource")
}
return ctrl.Result{}, nil
} else {
// if fail to delete the external dependency here, log the error, but don't return error
// Don't want to get stuck in a cycle of repeatedly trying to delete the repository and failing
michael-valdron marked this conversation as resolved.
Show resolved Hide resolved
log.Error(err, "Unable to delete GitOps repository for application %v in namespace %v", application.GetName(), application.GetNamespace())

// Increment the Application deletion failed metric as the application delete did not fully succeed
metrics.ApplicationDeletionFailed.Inc()
Expand All @@ -138,7 +142,11 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// remove the finalizer from the list and update it.
controllerutil.RemoveFinalizer(&application, appFinalizerName)
if err := r.Update(ctx, &application); err != nil {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
err := r.Update(ctx, &application)
return err
})
if err != nil {
return ctrl.Result{}, err
} else {
metrics.ApplicationDeletionSucceeded.Inc()
Expand Down
Loading