@@ -28,6 +28,7 @@ import (
28
28
kubeappproject "github.com/argoproj-labs/argocd-agent/internal/backend/kubernetes/appproject"
29
29
kubenamespace "github.com/argoproj-labs/argocd-agent/internal/backend/kubernetes/namespace"
30
30
kuberepository "github.com/argoproj-labs/argocd-agent/internal/backend/kubernetes/repository"
31
+ "github.com/argoproj-labs/argocd-agent/internal/cache"
31
32
"github.com/argoproj-labs/argocd-agent/internal/config"
32
33
"github.com/argoproj-labs/argocd-agent/internal/event"
33
34
"github.com/argoproj-labs/argocd-agent/internal/informer"
@@ -49,7 +50,6 @@ import (
49
50
"k8s.io/apimachinery/pkg/runtime"
50
51
"k8s.io/apimachinery/pkg/watch"
51
52
52
- appCache "github.com/argoproj-labs/argocd-agent/internal/cache"
53
53
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
54
54
cacheutil "github.com/argoproj/argo-cd/v3/util/cache"
55
55
appstatecache "github.com/argoproj/argo-cd/v3/util/cache/appstate"
@@ -104,6 +104,9 @@ type Agent struct {
104
104
105
105
cacheRefreshInterval time.Duration
106
106
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
107
110
}
108
111
109
112
const defaultQueueName = "default"
@@ -310,6 +313,8 @@ func NewAgent(ctx context.Context, client *kube.KubernetesClient, namespace stri
310
313
}
311
314
a .clusterCache = clusterCache
312
315
316
+ a .sourceCache = cache .NewSourceCache ()
317
+
313
318
return a , nil
314
319
}
315
320
@@ -319,21 +324,10 @@ func (a *Agent) Start(ctx context.Context) error {
319
324
a .context = infCtx
320
325
a .cancelFn = cancelFn
321
326
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.
324
329
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 )
337
331
}
338
332
339
333
if a .options .metricsPort > 0 {
@@ -482,3 +476,46 @@ func (a *Agent) healthzHandler(w http.ResponseWriter, r *http.Request) {
482
476
w .WriteHeader (http .StatusServiceUnavailable )
483
477
}
484
478
}
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
+ }
0 commit comments