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

feat: Support app deletion for autonomous mode #89

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ packages:
github.com/jannfis/argocd-agent/internal/queue:
github.com/jannfis/argocd-agent/internal/issuer:
github.com/jannfis/argocd-agent/internal/backend:
k8s.io/client-go/util/workqueue:
config:
dir: "test/mocks/k8s-workqueue"
all: false
interfaces:
RateLimitingInterface:
6 changes: 3 additions & 3 deletions agent/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a *Agent) addAppCreationToQueue(app *v1alpha1.Application) {
return
}

q.Add(a.emitter.NewApplicationEvent(event.Create, app))
q.Add(a.emitter.ApplicationEvent(event.Create, app))
logCtx.WithField("sendq_len", q.Len()).WithField("sendq_name", a.remote.ClientID()).Debugf("Added app create event to send queue")
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func (a *Agent) addAppUpdateToQueue(old *v1alpha1.Application, new *v1alpha1.App
eventType = event.SpecUpdate
}

q.Add(a.emitter.NewApplicationEvent(eventType, new))
q.Add(a.emitter.ApplicationEvent(eventType, new))
// q.Add(ev)
logCtx.
WithField("sendq_len", q.Len()).
Expand Down Expand Up @@ -130,6 +130,6 @@ func (a *Agent) addAppDeletionToQueue(app *v1alpha1.Application) {
logCtx.Error("Default queue disappeared!")
return
}
q.Add(a.emitter.NewApplicationEvent(event.Delete, app))
q.Add(a.emitter.ApplicationEvent(event.Delete, app))
logCtx.WithField("sendq_len", q.Len()).Debugf("Added app delete event to send queue")
}
2 changes: 2 additions & 0 deletions hack/demo-env/apps/autonomous-guestbook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ kind: Application
metadata:
name: guestbook
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: default
source:
Expand Down
9 changes: 8 additions & 1 deletion internal/event/event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package event

import (
"errors"
"fmt"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand Down Expand Up @@ -35,6 +36,8 @@ const (
TargetAppProject EventTarget = "appproject"
)

var ErrEventDiscarded error = errors.New("discarded")

func (t EventType) String() string {
return string(t)
}
Expand All @@ -58,7 +61,11 @@ func NewEventSource(source string) *EventSource {
return ev
}

func (evs EventSource) NewApplicationEvent(evType EventType, app *v1alpha1.Application) *cloudevents.Event {
func IsEventDiscarded(err error) bool {
return errors.Is(err, ErrEventDiscarded)
}

func (evs EventSource) ApplicationEvent(evType EventType, app *v1alpha1.Application) *cloudevents.Event {
cev := cloudevents.NewEvent()
cev.SetSource(evs.source)
cev.SetSpecVersion(cloudEventSpecVersion)
Expand Down
87 changes: 76 additions & 11 deletions internal/manager/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
type updateTransformer func(existing, incoming *v1alpha1.Application)
type patchTransformer func(existing, incoming *v1alpha1.Application) (jsondiff.Patch, error)

// LastUpdatedLabel is a label put on applications which contains the time when
// an update was last received for this Application
const LastUpdatedLabel = "argocd-agent.argoproj.io/last-updated"
// LastUpdatedAnnotation is a label put on applications which contains the time
// when an update was last received for this Application
const LastUpdatedAnnotation = "argocd-agent.argoproj.io/last-updated"

// ApplicationManager manages Argo CD application resources on a given backend.
//
Expand Down Expand Up @@ -90,10 +90,10 @@ func NewApplicationManager(be backend.Application, namespace string, opts ...App

// stampLastUpdated "stamps" an application with the last updated label
func stampLastUpdated(app *v1alpha1.Application) {
if app.Labels == nil {
app.Labels = make(map[string]string)
if app.Annotations == nil {
app.Annotations = make(map[string]string)
}
app.Labels[LastUpdatedLabel] = time.Now().Format(time.RFC3339)
app.Annotations[LastUpdatedAnnotation] = time.Now().Format(time.RFC3339)
}

// Create creates the application app using the Manager's application backend.
Expand Down Expand Up @@ -153,6 +153,7 @@ func (m *ApplicationManager) UpdateManagedApp(ctx context.Context, incoming *v1a
updated, err = m.update(ctx, m.AllowUpsert, incoming, func(existing, incoming *v1alpha1.Application) {
existing.ObjectMeta.Annotations = incoming.ObjectMeta.Annotations
existing.ObjectMeta.Labels = incoming.ObjectMeta.Labels
existing.ObjectMeta.Finalizers = incoming.ObjectMeta.Finalizers
existing.Spec = *incoming.Spec.DeepCopy()
existing.Operation = incoming.Operation.DeepCopy()
existing.Status = *incoming.Status.DeepCopy()
Expand All @@ -168,6 +169,7 @@ func (m *ApplicationManager) UpdateManagedApp(ctx context.Context, incoming *v1a
ObjectMeta: v1.ObjectMeta{
Annotations: incoming.Annotations,
Labels: incoming.Labels,
Finalizers: incoming.Finalizers,
},
Spec: incoming.Spec,
Operation: incoming.Operation,
Expand Down Expand Up @@ -231,22 +233,31 @@ func (m *ApplicationManager) UpdateAutonomousApp(ctx context.Context, namespace
updated, err = m.update(ctx, true, incoming, func(existing, incoming *v1alpha1.Application) {
existing.ObjectMeta.Annotations = incoming.ObjectMeta.Annotations
existing.ObjectMeta.Labels = incoming.ObjectMeta.Labels
existing.DeletionTimestamp = incoming.DeletionTimestamp
existing.DeletionGracePeriodSeconds = incoming.DeletionGracePeriodSeconds
existing.ObjectMeta.DeletionTimestamp = incoming.DeletionTimestamp
existing.ObjectMeta.DeletionGracePeriodSeconds = incoming.DeletionGracePeriodSeconds
existing.ObjectMeta.Finalizers = incoming.Finalizers
existing.Spec = incoming.Spec
existing.Status = *incoming.Status.DeepCopy()
existing.Operation = nil
logCtx.Infof("Updating")
}, func(existing, incoming *v1alpha1.Application) (jsondiff.Patch, error) {
target := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Labels: incoming.Labels,
Annotations: incoming.Annotations,
Labels: incoming.Labels,
Annotations: incoming.Annotations,
DeletionTimestamp: incoming.DeletionTimestamp,
DeletionGracePeriodSeconds: incoming.DeletionGracePeriodSeconds,
Finalizers: incoming.Finalizers,
},
Spec: incoming.Spec,
Status: incoming.Status,
}
source := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
DeletionTimestamp: existing.DeletionTimestamp,
DeletionGracePeriodSeconds: existing.DeletionGracePeriodSeconds,
Finalizers: existing.Finalizers,
},
Spec: existing.Spec,
Status: existing.Status,
}
Expand Down Expand Up @@ -374,7 +385,7 @@ func (m *ApplicationManager) UpdateOperation(ctx context.Context, incoming *v1al

var updated *v1alpha1.Application
var err error
if m.Role == manager.ManagerRolePrincipal {
if m.Role.IsPrincipal() {
stampLastUpdated(incoming)
}
updated, err = m.update(ctx, false, incoming, func(existing, incoming *v1alpha1.Application) {
Expand Down Expand Up @@ -419,6 +430,37 @@ func (m *ApplicationManager) UpdateOperation(ctx context.Context, incoming *v1al
return updated, err
}

// Delete will delete an application resource. If Delete is called by the
// principal, any existing finalizers will be removed before deletion is
// attempted.
func (m *ApplicationManager) Delete(ctx context.Context, namespace string, incoming *v1alpha1.Application) error {
removeFinalizer := false
logCtx := log().WithFields(logrus.Fields{
"component": "DeleteOperation",
"application": incoming.QualifiedName(),
"resourceVersion": incoming.ResourceVersion,
})
if m.Role.IsPrincipal() {
removeFinalizer = true
incoming.SetNamespace(namespace)
}
var err error
var updated *v1alpha1.Application

if removeFinalizer {
updated, err = m.RemoveFinalizers(ctx, incoming)
if err == nil {
logCtx.Debugf("Removed finalizer for app %s", updated.QualifiedName())
} else {
return fmt.Errorf("error removing finalizer: %w", err)
}
}

err = m.Application.Delete(ctx, incoming.Name, incoming.Namespace)

return err
}

// update updates an existing Application resource on the Manager m's backend
// to match the incoming resource. If the backend supports patch, the existing
// resource will be patched, otherwise it will be updated.
Expand Down Expand Up @@ -466,6 +508,29 @@ func (m *ApplicationManager) update(ctx context.Context, upsert bool, incoming *
return updated, err
}

// RemoveFinalizers will remove finalizers on an existing application
func (m *ApplicationManager) RemoveFinalizers(ctx context.Context, incoming *v1alpha1.Application) (*v1alpha1.Application, error) {
updated, err := m.update(ctx, false, incoming, func(existing, incoming *v1alpha1.Application) {
existing.ObjectMeta.Finalizers = nil
}, func(existing, incoming *v1alpha1.Application) (jsondiff.Patch, error) {
var err error
var patch jsondiff.Patch
target := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Finalizers: nil,
},
}
source := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Finalizers: existing.Finalizers,
},
}
patch, err = jsondiff.Compare(source, target, jsondiff.SkipCompact())
return patch, err
})
return updated, err
}

func log() *logrus.Entry {
return logrus.WithField("component", "AppManager")
}
103 changes: 92 additions & 11 deletions internal/manager/application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jannfis/argocd-agent/internal/metrics"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand All @@ -26,6 +27,13 @@ import (
var appExistsError = errors.NewAlreadyExists(schema.GroupResource{Group: "argoproj.io", Resource: "application"}, "existing")
var appNotFoundError = errors.NewNotFound(schema.GroupResource{Group: "argoproj.io", Resource: "application"}, "existing")

func fakeAppManager(objects ...runtime.Object) (*fakeappclient.Clientset, *ApplicationManager) {
appC := fakeappclient.NewSimpleClientset(objects...)
informer := appinformer.NewAppInformer(context.Background(), appC, "argocd")
be := kubernetes.NewKubernetesBackend(appC, "", informer, true)
return appC, NewApplicationManager(be, "argocd")
}

func Test_ManagerOptions(t *testing.T) {
t.Run("NewManager with default options", func(t *testing.T) {
m := NewApplicationManager(nil, "")
Expand Down Expand Up @@ -377,14 +385,87 @@ func Test_ManagerUpdateOperation(t *testing.T) {
},
}

appC := fakeappclient.NewSimpleClientset(existing)
informer := appinformer.NewAppInformer(context.Background(), appC, "argocd")
be := kubernetes.NewKubernetesBackend(appC, "", informer, true)
mgr := NewApplicationManager(be, "argocd")
// appC := fakeappclient.NewSimpleClientset(existing)
// informer := appinformer.NewAppInformer(context.Background(), appC, "argocd")
// be := kubernetes.NewKubernetesBackend(appC, "", informer, true)
// mgr := NewApplicationManager(be, "argocd")
_, mgr := fakeAppManager(existing)
updated, err := mgr.UpdateOperation(context.TODO(), incoming)
require.NoError(t, err)
require.NotNil(t, updated)
prettyPrint(updated)
})
}

func Test_DeleteApp(t *testing.T) {
t.Run("Delete without finalizer", func(t *testing.T) {
existing := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "foobar",
Namespace: "argocd",
Labels: map[string]string{
"foo": "bar",
"bar": "foo",
},
Finalizers: []string{"resource-finalizer.argoproj.io"},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "github.com",
TargetRevision: "HEAD",
Path: ".",
},
Destination: v1alpha1.ApplicationDestination{
Server: "in-cluster",
Namespace: "guestbook",
},
},
Operation: &v1alpha1.Operation{
InitiatedBy: v1alpha1.OperationInitiator{Username: "foobar"},
},
}
appC, mgr := fakeAppManager(existing)
app, err := appC.ArgoprojV1alpha1().Applications("argocd").Get(context.TODO(), "foobar", v1.GetOptions{})
assert.NoError(t, err)
assert.NotNil(t, app)
err = mgr.Delete(context.TODO(), "argocd", existing)
assert.NoError(t, err)
app, err = appC.ArgoprojV1alpha1().Applications("argocd").Get(context.TODO(), "foobar", v1.GetOptions{})
assert.True(t, errors.IsNotFound(err))
assert.Nil(t, app)
})
t.Run("Remove finalizers", func(t *testing.T) {
existing := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "foobar",
Namespace: "argocd",
Labels: map[string]string{
"foo": "bar",
"bar": "foo",
},
Finalizers: []string{"resource-finalizer.argoproj.io"},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "github.com",
TargetRevision: "HEAD",
Path: ".",
},
Destination: v1alpha1.ApplicationDestination{
Server: "in-cluster",
Namespace: "guestbook",
},
},
Operation: &v1alpha1.Operation{
InitiatedBy: v1alpha1.OperationInitiator{Username: "foobar"},
},
}
appC, mgr := fakeAppManager(existing)
app, err := appC.ArgoprojV1alpha1().Applications("argocd").Get(context.TODO(), "foobar", v1.GetOptions{})
assert.NoError(t, err)
assert.NotNil(t, app)
app, err = mgr.RemoveFinalizers(context.TODO(), app)
assert.NoError(t, err)
assert.Empty(t, app.ObjectMeta.Finalizers)
})
}

Expand Down Expand Up @@ -461,15 +542,15 @@ func Test_stampLastUpdated(t *testing.T) {
},
}
stampLastUpdated(app)
assert.Contains(t, app.Labels, LastUpdatedLabel)
assert.Len(t, app.Labels, 1)
assert.Contains(t, app.Annotations, LastUpdatedAnnotation)
assert.Len(t, app.Annotations, 1)
})
t.Run("Stamp app with existing labels", func(t *testing.T) {
t.Run("Stamp app with existing annotations", func(t *testing.T) {
app := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Labels: map[string]string{
Annotations: map[string]string{
"foo": "bar",
"bar": "baz",
},
Expand All @@ -479,8 +560,8 @@ func Test_stampLastUpdated(t *testing.T) {
},
}
stampLastUpdated(app)
assert.Contains(t, app.Labels, LastUpdatedLabel)
assert.Len(t, app.Labels, 3)
assert.Contains(t, app.Annotations, LastUpdatedAnnotation)
assert.Len(t, app.Annotations, 3)
})
}

Expand Down
Loading
Loading