Skip to content

Commit 832c241

Browse files
committed
fix: cascaded delete on Application should clean up all managed-agent app resources
Signed-off-by: Jonathan West <jonwest@redhat.com>
1 parent 48057ca commit 832c241

File tree

8 files changed

+243
-27
lines changed

8 files changed

+243
-27
lines changed

agent/outbound.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ func (a *Agent) addAppDeletionToQueue(app *v1alpha1.Application) {
126126
return
127127
}
128128

129-
// Only send the deletion event when we're in autonomous mode
130-
if !a.mode.IsAutonomous() {
131-
return
132-
}
133-
134129
q.Add(a.emitter.ApplicationEvent(event.Delete, app))
135130
logCtx.WithField("sendq_len", q.Len()).Debugf("Added app delete event to send queue")
136131
}

agent/outbound_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ func Test_addAppDeletionToQueue(t *testing.T) {
138138
defer func() {
139139
a.mode = types.AgentModeAutonomous
140140
}()
141-
// App must be already managed for event to be generated
141+
// Delete events will be sent for managed applications on managed agent, whether or not the application is managed.
142142
_ = a.appManager.Manage("agent/guestbook")
143-
a.addAppDeletionToQueue(app)
144143
require.Equal(t, 0, a.queues.SendQ(defaultQueueName).Len())
144+
a.addAppDeletionToQueue(app)
145+
require.Equal(t, 1, a.queues.SendQ(defaultQueueName).Len())
145146
require.False(t, a.appManager.IsManaged("agent/guestbook"))
146147
})
147148
t.Run("Deletion event for unmanaged application", func(t *testing.T) {

internal/manager/application/application.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/apimachinery/pkg/api/errors"
2626
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/client-go/util/retry"
28+
"k8s.io/utils/ptr"
2829

2930
"github.com/argoproj-labs/argocd-agent/internal/backend"
3031
appCache "github.com/argoproj-labs/argocd-agent/internal/cache"
@@ -185,6 +186,8 @@ func (m *ApplicationManager) UpdateManagedApp(ctx context.Context, incoming *v1a
185186
return nil, fmt.Errorf("updatedManagedApp should be called on a managed agent, only")
186187
}
187188

189+
deletionTimestampChanged := false
190+
188191
updated, err = m.update(ctx, m.allowUpsert, incoming, func(existing, incoming *v1alpha1.Application) {
189192
if v, ok := existing.Annotations[manager.SourceUIDAnnotation]; ok {
190193
if incoming.Annotations == nil {
@@ -198,6 +201,11 @@ func (m *ApplicationManager) UpdateManagedApp(ctx context.Context, incoming *v1a
198201
existing.Spec = *incoming.Spec.DeepCopy()
199202
existing.Operation = incoming.Operation.DeepCopy()
200203
existing.Status = *incoming.Status.DeepCopy()
204+
205+
if incoming.DeletionTimestamp != nil && existing.DeletionTimestamp == nil {
206+
deletionTimestampChanged = true
207+
}
208+
201209
}, func(existing, incoming *v1alpha1.Application) (jsondiff.Patch, error) {
202210
// We need to keep the refresh label if it is set on the existing app
203211
if v, ok := existing.Annotations["argocd.argoproj.io/refresh"]; ok {
@@ -214,6 +222,10 @@ func (m *ApplicationManager) UpdateManagedApp(ctx context.Context, incoming *v1a
214222
incoming.Annotations[manager.SourceUIDAnnotation] = v
215223
}
216224

225+
if incoming.DeletionTimestamp != nil && existing.DeletionTimestamp == nil {
226+
deletionTimestampChanged = true
227+
}
228+
217229
target := &v1alpha1.Application{
218230
ObjectMeta: v1.ObjectMeta{
219231
Annotations: incoming.Annotations,
@@ -247,6 +259,14 @@ func (m *ApplicationManager) UpdateManagedApp(ctx context.Context, incoming *v1a
247259
logCtx.Warnf("Couldn't unignore change %s for app %s: %v", updated.ResourceVersion, updated.QualifiedName(), err)
248260
}
249261
}
262+
263+
if deletionTimestampChanged {
264+
logCtx.Infof("deletionTimestamp of managed agent changed from nil to non-nil, so deleting Application")
265+
if err := m.applicationBackend.Delete(ctx, incoming.Name, incoming.Namespace, ptr.To(backend.DeletePropagationForeground)); err != nil {
266+
return nil, err
267+
}
268+
}
269+
250270
return updated, err
251271
}
252272

internal/manager/application/application_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func Test_ManagerUpdateManaged(t *testing.T) {
143143
"bar": "foo",
144144
manager.SourceUIDAnnotation: "random_uid",
145145
},
146+
Finalizers: []string{"test-finalizer"},
146147
},
147148
Spec: v1alpha1.ApplicationSpec{
148149
Source: &v1alpha1.ApplicationSource{
@@ -223,6 +224,10 @@ func Test_ManagerUpdateManaged(t *testing.T) {
223224

224225
// Labels and annotations must be in sync with incoming
225226
require.Equal(t, incoming.Labels, updated.Labels)
227+
228+
// Finalizers must be in sync
229+
require.Equal(t, incoming.Finalizers, updated.Finalizers)
230+
226231
// Non-refresh annotations must be in sync with incoming
227232
require.Equal(t, incoming.Annotations["bar"], updated.Annotations["bar"])
228233
// Refresh annotation must not be removed
@@ -232,6 +237,7 @@ func Test_ManagerUpdateManaged(t *testing.T) {
232237
// Spec must be in sync with incoming
233238
require.Equal(t, incoming.Spec, updated.Spec)
234239
})
240+
235241
}
236242

237243
func Test_ManagerUpdateStatus(t *testing.T) {

principal/callbacks.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ func (s *Server) updateAppCallback(old *v1alpha1.Application, new *v1alpha1.Appl
6969
"event": "application_update",
7070
"application_name": old.Name,
7171
})
72-
if len(new.Finalizers) > 0 && len(new.Finalizers) != len(old.Finalizers) {
73-
tmp, err := s.appManager.RemoveFinalizers(s.ctx, new)
74-
if err != nil {
75-
logCtx.WithError(err).Warnf("Could not remove finalizer")
76-
} else {
77-
logCtx.Debug("Removed finalizer")
78-
new = tmp
79-
}
80-
}
8172
if s.appManager.IsChangeIgnored(new.QualifiedName(), new.ResourceVersion) {
8273
logCtx.WithField("resource_version", new.ResourceVersion).Debugf("Resource version has already been seen")
8374
return

principal/event.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/sirupsen/logrus"
3434
"golang.org/x/sync/semaphore"
3535
corev1 "k8s.io/api/core/v1"
36+
"k8s.io/apimachinery/pkg/api/errors"
3637
kerrors "k8s.io/apimachinery/pkg/api/errors"
3738
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3839
"k8s.io/apimachinery/pkg/util/validation"
@@ -213,20 +214,59 @@ func (s *Server) processApplicationEvent(ctx context.Context, agentName string,
213214
logCtx.Infof("Updated application spec %s", incoming.QualifiedName())
214215
// App deletion
215216
case event.Delete.String():
216-
if !agentMode.IsAutonomous() {
217-
logCtx.Debug("Discarding event, because agent is not in autonomous mode")
218-
return event.NewEventNotAllowedErr("event type not allowed when mode is not autonomous")
219-
}
217+
if agentMode.IsAutonomous() {
220218

221-
deletionPropagation := backend.DeletePropagationForeground
222-
err := s.appManager.Delete(ctx, agentName, incoming, &deletionPropagation)
223-
if err != nil {
224-
if kerrors.IsNotFound(err) {
219+
deletionPropagation := backend.DeletePropagationForeground
220+
err = s.appManager.Delete(ctx, agentName, incoming, &deletionPropagation)
221+
if err != nil {
222+
if kerrors.IsNotFound(err) {
223+
return nil
224+
}
225+
return fmt.Errorf("could not delete application %s: %w", incoming.QualifiedName(), err)
226+
}
227+
logCtx.Infof("Deleted application %s", incoming.QualifiedName())
228+
229+
} else if agentMode.IsManaged() {
230+
231+
app, err := s.appManager.Get(ctx, incoming.Name, incoming.Namespace)
232+
if err != nil {
233+
if errors.IsNotFound(err) {
234+
return nil // ignore not found error: no need to delete app if it doesn't exist
235+
}
236+
logCtx.WithError(err).Error("unexpected error when attempting to retrieve deleted Application")
237+
return err
238+
}
239+
240+
// When we receive an 'application deleted' event from a managed agent, it should only cause the principal application to be deleted IF the principal Application is ALSO in the deletion state (deletionTimestamp is non-nil)
241+
if app.DeletionTimestamp == nil {
242+
logCtx.Warn("application was detected as deleted from managed agent, even though principal application is not in deletion state")
225243
return nil
226244
}
227-
return fmt.Errorf("could not delete application %s: %w", incoming.QualifiedName(), err)
245+
246+
// Remove the finalizers from the Application when it has been deleted from managed-agent
247+
if len(app.Finalizers) > 0 {
248+
_, err := s.appManager.RemoveFinalizers(ctx, app)
249+
// app.Finalizers = nil
250+
// _, err := s.appManager.UpdateManagedApp(ctx, app)
251+
if err != nil {
252+
logCtx.WithError(err).Error("unexpected error when attempting to update finalizers of deleted Application")
253+
return err
254+
}
255+
}
256+
257+
deletionPropagation := backend.DeletePropagationForeground
258+
err = s.appManager.Delete(ctx, agentName, incoming, &deletionPropagation)
259+
if err != nil {
260+
if kerrors.IsNotFound(err) {
261+
return nil
262+
}
263+
return fmt.Errorf("could not delete application %s: %w", incoming.QualifiedName(), err)
264+
}
265+
logCtx.Infof("Deleted application %s", incoming.QualifiedName())
266+
267+
} else {
268+
return fmt.Errorf("unexpected agent mode")
228269
}
229-
logCtx.Infof("Deleted application %s", incoming.QualifiedName())
230270
default:
231271
return fmt.Errorf("unable to process event of type %s", ev.Type())
232272
}

test/e2e2/delete_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright 2025 The argocd-agent Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package e2e2
16+
17+
import (
18+
"testing"
19+
"time"
20+
21+
"github.com/argoproj-labs/argocd-agent/test/e2e2/fixture"
22+
23+
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
24+
argoapp "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
25+
"github.com/stretchr/testify/suite"
26+
appsv1 "k8s.io/api/apps/v1"
27+
"k8s.io/apimachinery/pkg/api/errors"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
)
30+
31+
type DeleteTestSuite struct {
32+
fixture.BaseSuite
33+
}
34+
35+
func (suite *DeleteTestSuite) Test_CascadeDeleteOnManagedAgent() {
36+
requires := suite.Require()
37+
38+
t := suite.T()
39+
40+
t.Log("Create a managed application in the principal's cluster")
41+
appOnPrincipal := v1alpha1.Application{
42+
ObjectMeta: metav1.ObjectMeta{
43+
Name: "my-app",
44+
Namespace: "agent-managed",
45+
},
46+
Spec: v1alpha1.ApplicationSpec{
47+
Project: "default",
48+
Source: &v1alpha1.ApplicationSource{
49+
RepoURL: "https://github.com/argoproj/argocd-example-apps",
50+
TargetRevision: "HEAD",
51+
Path: "kustomize-guestbook",
52+
},
53+
Destination: v1alpha1.ApplicationDestination{
54+
Name: "agent-managed",
55+
Namespace: "guestbook",
56+
},
57+
SyncPolicy: &v1alpha1.SyncPolicy{
58+
Automated: &v1alpha1.SyncPolicyAutomated{},
59+
SyncOptions: v1alpha1.SyncOptions{
60+
"CreateNamespace=true",
61+
},
62+
},
63+
},
64+
}
65+
66+
err := ensureAppExistsAndIsSyncedAndHealthy(&appOnPrincipal, suite.PrincipalClient, &suite.BaseSuite)
67+
requires.NoError(err)
68+
69+
t.Log("'kustomize-guestbook-ui' Deployment should exist on managed-agent")
70+
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "kustomize-guestbook-ui", Namespace: "guestbook"}}
71+
requires.Eventually(func() bool {
72+
err := suite.ManagedAgentClient.Get(suite.Ctx, fixture.ToNamespacedName(depl), depl, metav1.GetOptions{})
73+
return err == nil
74+
}, 60*time.Second, 1*time.Second)
75+
76+
t.Log("Add a finalizer to the Deployment to keep it from being deleted")
77+
depl.Finalizers = append(depl.Finalizers, "test-e2e/my-test-finalizer")
78+
err = suite.ManagedAgentClient.Update(suite.Ctx, depl, metav1.UpdateOptions{})
79+
requires.NoError(err)
80+
81+
// Ensure that finalizer is removed from Deployment if the test ends prematurely
82+
defer func() {
83+
err = suite.ManagedAgentClient.Get(suite.Ctx, fixture.ToNamespacedName(depl), depl, metav1.GetOptions{})
84+
if err == nil { // If the Deployment still exists
85+
depl.Finalizers = nil // Remove the finalizer
86+
err = suite.ManagedAgentClient.Update(suite.Ctx, depl, metav1.UpdateOptions{})
87+
requires.NoError(err)
88+
}
89+
}()
90+
91+
t.Log("Simulate cascade deletion from Argo CD: Add a resources-finalizer to principal, then delete it (which sets deletion timestamp)")
92+
err = suite.PrincipalClient.EnsureApplicationUpdate(suite.Ctx, fixture.ToNamespacedName(&appOnPrincipal), func(a *v1alpha1.Application) error {
93+
a.Finalizers = append(appOnPrincipal.Finalizers, "resources-finalizer.argocd.argoproj.io")
94+
return nil
95+
}, metav1.UpdateOptions{})
96+
requires.NoError(err)
97+
98+
err = suite.PrincipalClient.Delete(suite.Ctx, &appOnPrincipal, metav1.DeleteOptions{})
99+
requires.NoError(err)
100+
101+
t.Log("Ensure that finalizers and deletion timestamp are synced to managed-agent")
102+
requires.Eventually(func() bool {
103+
app := argoapp.Application{}
104+
err := suite.ManagedAgentClient.Get(suite.Ctx, fixture.ToNamespacedName(&appOnPrincipal), &app, metav1.GetOptions{})
105+
return err == nil && app.DeletionTimestamp != nil && len(app.Finalizers) > 0
106+
}, 60*time.Second, 1*time.Second)
107+
108+
t.Log("Remove finalizer from the Deployment, so that it may proceed with being deleted")
109+
err = suite.ManagedAgentClient.Get(suite.Ctx, fixture.ToNamespacedName(depl), depl, metav1.GetOptions{})
110+
requires.NoError(err)
111+
112+
depl.Finalizers = nil
113+
err = suite.ManagedAgentClient.Update(suite.Ctx, depl, metav1.UpdateOptions{})
114+
requires.NoError(err)
115+
116+
t.Log("Verify managed agent application is eventually deleted")
117+
requires.Eventually(func() bool {
118+
app := argoapp.Application{}
119+
err := suite.ManagedAgentClient.Get(suite.Ctx, fixture.ToNamespacedName(&appOnPrincipal), &app, metav1.GetOptions{})
120+
return err != nil && errors.IsNotFound(err)
121+
}, 60*time.Second, 1*time.Second)
122+
123+
t.Log("Verify guestbook Deployment is deleted from managed-agent cluster, which proves it is a cascaded delete")
124+
requires.Eventually(func() bool {
125+
err := suite.ManagedAgentClient.Get(suite.Ctx, fixture.ToNamespacedName(depl), depl, metav1.GetOptions{})
126+
return err != nil && errors.IsNotFound(err)
127+
}, 60*time.Second, 1*time.Second)
128+
129+
t.Log("Verify principal application is deleted")
130+
requires.Eventually(func() bool {
131+
app := argoapp.Application{}
132+
err := suite.PrincipalClient.Get(suite.Ctx, fixture.ToNamespacedName(&appOnPrincipal), &app, metav1.GetOptions{})
133+
return err != nil && errors.IsNotFound(err)
134+
}, 60*time.Second, 1*time.Second)
135+
136+
}
137+
138+
func TestDeleteTestSuite(t *testing.T) {
139+
suite.Run(t, new(DeleteTestSuite))
140+
}

test/e2e2/fixture/fixture.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,31 @@ func EnsureDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) err
8585
return err
8686
}
8787

88+
// Wait for the object to be deleted for 60 seconds
89+
// - Primarily this will be waiting for the finalizer to be removed, so that the object is deleted
8890
key := types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}
89-
for count := 0; count < 120; count++ {
91+
for count := 0; count < 60; count++ {
92+
err := kclient.Get(ctx, key, obj, metav1.GetOptions{})
93+
if errors.IsNotFound(err) {
94+
return nil
95+
} else if err == nil {
96+
time.Sleep(1 * time.Second)
97+
} else {
98+
return err
99+
}
100+
}
101+
102+
// After X seconds, give up waiting for the child objects to be deleted, and remove any finalizers on the object
103+
if len(obj.GetFinalizers()) > 0 {
104+
obj.SetFinalizers(nil)
105+
if err := kclient.Update(ctx, obj, metav1.UpdateOptions{}); err != nil {
106+
return err
107+
}
108+
}
109+
110+
// Continue waiting for object to be deleted, now that finalizers have been removed.
111+
key = types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}
112+
for count := 0; count < 60; count++ {
90113
err := kclient.Get(ctx, key, obj, metav1.GetOptions{})
91114
if errors.IsNotFound(err) {
92115
return nil

0 commit comments

Comments
 (0)