Skip to content

Commit d03ef99

Browse files
committed
feat: revert appproject updates on peer clusters
Signed-off-by: Chetan Banavikalmutt <[email protected]>
1 parent 2f23d9f commit d03ef99

File tree

15 files changed

+541
-170
lines changed

15 files changed

+541
-170
lines changed

agent/agent.go

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
kubeappproject "github.com/argoproj-labs/argocd-agent/internal/backend/kubernetes/appproject"
2929
kubenamespace "github.com/argoproj-labs/argocd-agent/internal/backend/kubernetes/namespace"
3030
kuberepository "github.com/argoproj-labs/argocd-agent/internal/backend/kubernetes/repository"
31+
"github.com/argoproj-labs/argocd-agent/internal/cache"
3132
"github.com/argoproj-labs/argocd-agent/internal/config"
3233
"github.com/argoproj-labs/argocd-agent/internal/event"
3334
"github.com/argoproj-labs/argocd-agent/internal/informer"
@@ -49,7 +50,6 @@ import (
4950
"k8s.io/apimachinery/pkg/runtime"
5051
"k8s.io/apimachinery/pkg/watch"
5152

52-
appCache "github.com/argoproj-labs/argocd-agent/internal/cache"
5353
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
5454
cacheutil "github.com/argoproj/argo-cd/v3/util/cache"
5555
appstatecache "github.com/argoproj/argo-cd/v3/util/cache/appstate"
@@ -104,6 +104,9 @@ type Agent struct {
104104

105105
cacheRefreshInterval time.Duration
106106
clusterCache *appstatecache.Cache
107+
108+
// sourceCache is a cache of resources from the source. We use it to revert any changes made to the local resources.
109+
sourceCache *cache.SourceCache
107110
}
108111

109112
const defaultQueueName = "default"
@@ -310,6 +313,8 @@ func NewAgent(ctx context.Context, client *kube.KubernetesClient, namespace stri
310313
}
311314
a.clusterCache = clusterCache
312315

316+
a.sourceCache = cache.NewSourceCache()
317+
313318
return a, nil
314319
}
315320

@@ -319,21 +324,10 @@ func (a *Agent) Start(ctx context.Context) error {
319324
a.context = infCtx
320325
a.cancelFn = cancelFn
321326

322-
// For managed-agent we need to maintain a cache to keep applications in sync with last known state of
323-
// principal in case agent is disconnected with principal or application in managed-cluster is modified.
327+
// For managed-agent we need to maintain a cache to keep resources in sync with last known state of
328+
// principal in case agent is disconnected with principal or resources in managed-cluster are modified.
324329
if a.mode == types.AgentModeManaged {
325-
log().Infof("Recreating application spec cache from existing resources on cluster")
326-
appList, err := a.appManager.List(ctx, backend.ApplicationSelector{Namespaces: []string{a.namespace}})
327-
if err != nil {
328-
log().Errorf("Error while fetching list of applications: %v", err)
329-
}
330-
331-
for _, app := range appList {
332-
sourceUID, exists := app.Annotations[manager.SourceUIDAnnotation]
333-
if exists {
334-
appCache.SetApplicationSpec(ty.UID(sourceUID), app.Spec, log())
335-
}
336-
}
330+
a.populateSourceCache(ctx)
337331
}
338332

339333
if a.options.metricsPort > 0 {
@@ -482,3 +476,46 @@ func (a *Agent) healthzHandler(w http.ResponseWriter, r *http.Request) {
482476
w.WriteHeader(http.StatusServiceUnavailable)
483477
}
484478
}
479+
480+
func (a *Agent) populateSourceCache(ctx context.Context) {
481+
log().Infof("Recreating application spec cache from existing resources on cluster")
482+
appList, err := a.appManager.List(ctx, backend.ApplicationSelector{Namespaces: []string{a.namespace}})
483+
if err != nil {
484+
log().Errorf("Error while fetching list of applications: %v", err)
485+
}
486+
487+
for _, app := range appList {
488+
sourceUID, exists := app.Annotations[manager.SourceUIDAnnotation]
489+
if exists {
490+
a.sourceCache.Application.Set(ty.UID(sourceUID), app.Spec)
491+
}
492+
}
493+
494+
log().Infof("Recreating appProject spec cache from existing resources on cluster")
495+
appProjectList, err := a.projectManager.List(ctx, backend.AppProjectSelector{Namespace: a.namespace})
496+
if err != nil {
497+
log().Errorf("Error while fetching list of appProjects: %v", err)
498+
}
499+
500+
for _, appProject := range appProjectList {
501+
sourceUID, exists := appProject.Annotations[manager.SourceUIDAnnotation]
502+
if exists {
503+
a.sourceCache.AppProject.Set(ty.UID(sourceUID), appProject.Spec)
504+
}
505+
}
506+
507+
log().Infof("Recreating repository spec cache from existing resources on cluster")
508+
repoList, err := a.repoManager.List(ctx, backend.RepositorySelector{Namespace: a.namespace})
509+
if err != nil {
510+
log().Errorf("Error while fetching list of repositories: %v", err)
511+
}
512+
513+
for _, repo := range repoList {
514+
sourceUID, exists := repo.Annotations[manager.SourceUIDAnnotation]
515+
if exists {
516+
a.sourceCache.Repository.Set(ty.UID(sourceUID), repo.Data)
517+
}
518+
}
519+
520+
log().Infof("Source cache populated successfully")
521+
}

agent/inbound.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"time"
2020

2121
"github.com/argoproj-labs/argocd-agent/internal/backend"
22-
appCache "github.com/argoproj-labs/argocd-agent/internal/cache"
2322
"github.com/argoproj-labs/argocd-agent/internal/checkpoint"
2423
"github.com/argoproj-labs/argocd-agent/internal/event"
2524
"github.com/argoproj-labs/argocd-agent/internal/manager"
@@ -459,7 +458,7 @@ func (a *Agent) createApplication(incoming *v1alpha1.Application) (*v1alpha1.App
459458

460459
if a.mode == types.AgentModeManaged {
461460
// Store app spec in cache
462-
appCache.SetApplicationSpec(incoming.UID, incoming.Spec, logCtx)
461+
a.sourceCache.Application.Set(incoming.UID, incoming.Spec)
463462
}
464463

465464
created, err := a.appManager.Create(a.context, incoming)
@@ -500,7 +499,7 @@ func (a *Agent) updateApplication(incoming *v1alpha1.Application) (*v1alpha1.App
500499

501500
// Update app spec in cache
502501
logCtx.Tracef("Calling update spec for this event")
503-
appCache.SetApplicationSpec(incoming.UID, incoming.Spec, logCtx)
502+
a.sourceCache.Application.Set(incoming.UID, incoming.Spec)
504503

505504
napp, err = a.appManager.UpdateManagedApp(a.context, incoming)
506505
case types.AgentModeAutonomous:
@@ -535,15 +534,15 @@ func (a *Agent) deleteApplication(app *v1alpha1.Application) error {
535534
if apierrors.IsNotFound(err) {
536535
logCtx.Debug("application is not found, perhaps it is already deleted")
537536
if a.mode == types.AgentModeManaged {
538-
appCache.DeleteApplicationSpec(app.UID, logCtx)
537+
a.sourceCache.Application.Delete(app.UID)
539538
}
540539
return nil
541540
}
542541
return err
543542
}
544543

545544
if a.mode == types.AgentModeManaged {
546-
appCache.DeleteApplicationSpec(app.UID, logCtx)
545+
a.sourceCache.Application.Delete(app.UID)
547546
}
548547

549548
err = a.appManager.Unmanage(app.QualifiedName())
@@ -577,6 +576,8 @@ func (a *Agent) createAppProject(incoming *v1alpha1.AppProject) (*v1alpha1.AppPr
577576

578577
logCtx.Infof("Creating a new AppProject on behalf of an incoming event")
579578

579+
a.sourceCache.AppProject.Set(incoming.UID, incoming.Spec)
580+
580581
// Get rid of some fields that we do not want to have on the AppProject
581582
// as we start fresh.
582583
if incoming.Annotations != nil {
@@ -611,6 +612,8 @@ func (a *Agent) updateAppProject(incoming *v1alpha1.AppProject) (*v1alpha1.AppPr
611612

612613
logCtx.Infof("Updating appProject")
613614

615+
a.sourceCache.AppProject.Set(incoming.UID, incoming.Spec)
616+
614617
logCtx.Tracef("Calling update spec for this event")
615618
return a.projectManager.UpdateAppProject(a.context, incoming)
616619

@@ -637,10 +640,14 @@ func (a *Agent) deleteAppProject(project *v1alpha1.AppProject) error {
637640
if err != nil {
638641
if apierrors.IsNotFound(err) {
639642
logCtx.Debug("appProject not found, perhaps it is already deleted")
643+
a.sourceCache.AppProject.Delete(project.UID)
640644
return nil
641645
}
642646
return err
643647
}
648+
649+
a.sourceCache.AppProject.Delete(project.UID)
650+
644651
err = a.projectManager.Unmanage(project.Name)
645652
if err != nil {
646653
log().Warnf("Could not unmanage appProject %s: %v", project.Name, err)

agent/outbound.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (a *Agent) addAppUpdateToQueue(old *v1alpha1.Application, new *v1alpha1.App
8181

8282
// Revert any direct modifications done in application on managed-cluster
8383
// because for managed-agent all changes should be done through principal
84-
if reverted := a.appManager.RevertManagedAppChanges(a.context, new); reverted {
84+
if reverted := a.appManager.RevertManagedAppChanges(a.context, new, a.sourceCache.Application); reverted {
8585
logCtx.Debugf("Modifications done in application: %s are reverted", new.Name)
8686
return
8787
}
@@ -215,6 +215,13 @@ func (a *Agent) addAppProjectUpdateToQueue(old *v1alpha1.AppProject, new *v1alph
215215
return
216216
}
217217

218+
// Revert any direct modifications done to appProject on managed-cluster
219+
// because for managed-agent all changes should be done through principal
220+
if reverted := a.projectManager.RevertAppProjectChanges(a.context, new, a.sourceCache.AppProject); reverted {
221+
logCtx.Debugf("Modifications done to appProject are reverted")
222+
return
223+
}
224+
218225
// Only send the update event when we're in autonomous mode
219226
if !a.mode.IsAutonomous() {
220227
return

internal/cache/appcache.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

internal/cache/appcache_test.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)