Skip to content

Commit

Permalink
Preserve ApplicationSet template finalizers
Browse files Browse the repository at this point in the history
This allows ApplicationSet admins to customize the finalizers while ensuring
`resources-finalizer.argocd.argoproj.io` is always applied for proper cleanup.

Signed-off-by: Alex Misstear <[email protected]>
  • Loading branch information
amisstea committed Apr 24, 2024
1 parent 8b6fd6a commit 7ce2e93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 8 additions & 3 deletions api/v1alpha1/clustertemplateinstance_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"context"
"encoding/json"
"slices"

argo "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/kubernetes-client/go-base/config/api"
Expand Down Expand Up @@ -183,9 +184,6 @@ func (i *ClusterTemplateInstance) UpdateApplicationSet(
Template: argo.ApplicationSetTemplate{
ApplicationSetTemplateMeta: argo.ApplicationSetTemplateMeta{
Name: name,
Finalizers: []string{
argo.ResourcesFinalizerName,
},
Labels: map[string]string{
CTINameLabel: i.Name,
CTINamespaceLabel: i.Namespace,
Expand All @@ -195,6 +193,13 @@ func (i *ClusterTemplateInstance) UpdateApplicationSet(
},
}

// To guarantee App cleanup, add the resource finalizer to the generator's template if it's not already defined in the AppSet's template.
// Preserve any other finalizers the AppSet author may have included.
found := slices.Contains(appSet.Spec.Template.Finalizers, argo.ResourcesFinalizerName)
if !found {
gen.List.Template.ApplicationSetTemplateMeta.Finalizers = append(appSet.Spec.Template.Finalizers, argo.ResourcesFinalizerName)
}

if appSet.Spec.Template.Spec.Source.Chart != "" {
params, err := i.GetHelmParameters(appSet, isDay2)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion api/v1alpha1/clustertemplateinstance_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,13 @@ var _ = Describe("ClusterTemplateInstance utils", func() {
Name: "foo",
Namespace: "cluster-aas-operator",
},
Spec: argo.ApplicationSetSpec{},
Spec: argo.ApplicationSetSpec{
Template: argo.ApplicationSetTemplate{
ApplicationSetTemplateMeta: argo.ApplicationSetTemplateMeta{
Finalizers: []string{"fooFinalizer"},
},
},
},
}

client := fake.NewFakeClientWithScheme(scheme.Scheme, appset)
Expand All @@ -367,6 +373,8 @@ var _ = Describe("ClusterTemplateInstance utils", func() {

Expect(len(a.Items[0].Spec.Generators)).To(Equal(1))
Expect(len(a.Items[0].Spec.Generators[0].List.Elements)).To(Equal(1))
Expect(len(a.Items[0].Spec.Generators[0].List.Template.Finalizers)).To(Equal(2))
Expect(a.Items[0].Spec.Generators[0].List.Template.Finalizers).To(Equal([]string{"fooFinalizer", argo.ResourcesFinalizerName}))

s, err := a.Items[0].Spec.Generators[0].List.Elements[0].MarshalJSON()
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -418,6 +426,9 @@ var _ = Describe("ClusterTemplateInstance utils", func() {
Spec: argo.ApplicationSetSpec{
Generators: []argo.ApplicationSetGenerator{{}},
Template: argo.ApplicationSetTemplate{
ApplicationSetTemplateMeta: argo.ApplicationSetTemplateMeta{
Finalizers: []string{argo.ResourcesFinalizerName},
},
Spec: argo.ApplicationSpec{
Source: argo.ApplicationSource{},
},
Expand All @@ -435,6 +446,7 @@ var _ = Describe("ClusterTemplateInstance utils", func() {
//Expect(appsets.Items[0].Spec.Generators[0].String()).To(Equal(""))
Expect(len(appsets.Items[0].Spec.Generators)).To(Equal(2))
Expect(len(appsets.Items[0].Spec.Generators[1].List.Elements)).To(Equal(1))
Expect(len(appsets.Items[0].Spec.Generators[1].List.Template.Finalizers)).To(Equal(0))

data, err = appsets.Items[0].Spec.Generators[1].List.Elements[0].MarshalJSON()
Expect(err).ShouldNot(HaveOccurred())
Expand Down

0 comments on commit 7ce2e93

Please sign in to comment.