From 575575a78a87c7fc97ce540124509f90c5733e05 Mon Sep 17 00:00:00 2001 From: Joe Bowbeer Date: Fri, 26 Apr 2024 16:41:32 -0700 Subject: [PATCH] Fix post-delete finalizer in appset (#18003) Signed-off-by: Joe Bowbeer --- .../controllers/applicationset_controller.go | 12 ++++ .../applicationset_controller_test.go | 65 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/applicationset/controllers/applicationset_controller.go b/applicationset/controllers/applicationset_controller.go index 614b00ebc38fd..abd23746893e8 100644 --- a/applicationset/controllers/applicationset_controller.go +++ b/applicationset/controllers/applicationset_controller.go @@ -17,6 +17,7 @@ package controllers import ( "context" "fmt" + "strings" "time" "github.com/google/go-cmp/cmp" @@ -723,6 +724,17 @@ func (r *ApplicationSetReconciler) createOrUpdateInCluster(ctx context.Context, } } + // Preserve post-delete finalizers: + // https://github.com/argoproj/argo-cd/issues/17181 + for _, finalizer := range found.ObjectMeta.Finalizers { + if strings.HasPrefix(finalizer, argov1alpha1.PostDeleteFinalizerName) { + if generatedApp.Finalizers == nil { + generatedApp.Finalizers = []string{} + } + generatedApp.Finalizers = append(generatedApp.Finalizers, finalizer) + } + } + found.ObjectMeta.Annotations = generatedApp.Annotations found.ObjectMeta.Finalizers = generatedApp.Finalizers diff --git a/applicationset/controllers/applicationset_controller_test.go b/applicationset/controllers/applicationset_controller_test.go index 244b2797ac684..790682d98eead 100644 --- a/applicationset/controllers/applicationset_controller_test.go +++ b/applicationset/controllers/applicationset_controller_test.go @@ -1281,6 +1281,71 @@ func TestCreateOrUpdateInCluster(t *testing.T) { }, }, }, + }, { + name: "Ensure that argocd post-delete finalizers are preserved from an existing app", + appSet: v1alpha1.ApplicationSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.ApplicationSetSpec{ + Template: v1alpha1.ApplicationSetTemplate{ + Spec: v1alpha1.ApplicationSpec{ + Project: "project", + }, + }, + }, + }, + existingApps: []v1alpha1.Application{ + { + TypeMeta: metav1.TypeMeta{ + Kind: application.ApplicationKind, + APIVersion: "argoproj.io/v1alpha1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "app1", + Namespace: "namespace", + ResourceVersion: "2", + Finalizers: []string{ + v1alpha1.PostDeleteFinalizerName, + v1alpha1.PostDeleteFinalizerName + "/mystage", + }, + }, + Spec: v1alpha1.ApplicationSpec{ + Project: "project", + }, + }, + }, + desiredApps: []v1alpha1.Application{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "app1", + }, + Spec: v1alpha1.ApplicationSpec{ + Project: "project", + }, + }, + }, + expected: []v1alpha1.Application{ + { + TypeMeta: metav1.TypeMeta{ + Kind: application.ApplicationKind, + APIVersion: "argoproj.io/v1alpha1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "app1", + Namespace: "namespace", + ResourceVersion: "2", + Finalizers: []string{ + v1alpha1.PostDeleteFinalizerName, + v1alpha1.PostDeleteFinalizerName + "/mystage", + }, + }, + Spec: v1alpha1.ApplicationSpec{ + Project: "project", + }, + }, + }, }, } {