|
15 | 15 | package agent
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "errors" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/argoproj-labs/argocd-agent/internal/argocd/cluster" |
18 | 22 | "github.com/argoproj-labs/argocd-agent/internal/event"
|
19 | 23 | "github.com/argoproj-labs/argocd-agent/internal/logging/logfields"
|
20 | 24 | "github.com/argoproj-labs/argocd-agent/internal/resources"
|
21 | 25 | "github.com/argoproj-labs/argocd-agent/pkg/types"
|
22 | 26 | "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
|
| 27 | + cacheutil "github.com/argoproj/argo-cd/v3/util/cache" |
23 | 28 | "github.com/sirupsen/logrus"
|
24 | 29 | corev1 "k8s.io/api/core/v1"
|
25 | 30 | )
|
@@ -105,6 +110,13 @@ func (a *Agent) addAppUpdateToQueue(old *v1alpha1.Application, new *v1alpha1.App
|
105 | 110 | WithField(logfields.SendQueueLen, q.Len()).
|
106 | 111 | WithField(logfields.SendQueueName, defaultQueueName).
|
107 | 112 | Debugf("Added event of type %s to send queue", eventType)
|
| 113 | + |
| 114 | + // When sync status changed to Synced, trigger a cluster cache info update event |
| 115 | + // so that the principal can be informed of the cache update. |
| 116 | + if a.mode == types.AgentModeManaged && old.Status.Sync.Status != new.Status.Sync.Status && |
| 117 | + new.Status.Sync.Status == v1alpha1.SyncStatusCodeSynced { |
| 118 | + go a.addClusterCacheInfoUpdateToQueue("application_synced") |
| 119 | + } |
108 | 120 | }
|
109 | 121 |
|
110 | 122 | // addAppDeletionToQueue processes an application delete event originating from
|
@@ -261,3 +273,50 @@ func (a *Agent) addAppProjectDeletionToQueue(appProject *v1alpha1.AppProject) {
|
261 | 273 | q.Add(a.emitter.AppProjectEvent(event.Delete, appProject))
|
262 | 274 | logCtx.WithField(logfields.SendQueueLen, q.Len()).Debugf("Added appProject delete event to send queue")
|
263 | 275 | }
|
| 276 | + |
| 277 | +// addClusterCacheInfoUpdateToQueue processes a cluster cache info update event |
| 278 | +// and puts it in the send queue. |
| 279 | +func (a *Agent) addClusterCacheInfoUpdateToQueue(reason string) { |
| 280 | + logCtx := log().WithFields(logrus.Fields{ |
| 281 | + "event": "addClusterCacheInfoUpdateToQueue", |
| 282 | + "reason": reason, |
| 283 | + }) |
| 284 | + |
| 285 | + clusterServer := "https://kubernetes.default.svc" |
| 286 | + var clusterInfo *v1alpha1.ClusterInfo |
| 287 | + var err error |
| 288 | + |
| 289 | + // If the reason is application_synced, application_created or application_deleted, |
| 290 | + // we wait for 20 seconds to ensure that updated info is sent to principal. |
| 291 | + // It is because the cluster cache is not updated immediately by Argo CD. |
| 292 | + // Instead it is updated in next clusterInfoUpdater cycle. |
| 293 | + if reason != "periodic_sync" { |
| 294 | + time.Sleep(20 * time.Second) |
| 295 | + } |
| 296 | + |
| 297 | + // Get the updated cluster info from agent's cache. |
| 298 | + clusterInfo, err = cluster.GetClusterInfo(a.context, a.kubeClient.Clientset, a.namespace, clusterServer, a.redisProxyMsgHandler.redisAddress, cacheutil.RedisCompressionGZip) |
| 299 | + if err != nil { |
| 300 | + if !errors.Is(err, cacheutil.ErrCacheMiss) { |
| 301 | + logCtx.WithError(err).Errorf("Failed to get cluster info from cache") |
| 302 | + } |
| 303 | + return |
| 304 | + } |
| 305 | + |
| 306 | + // Send the event to principal to update the cluster cache info. |
| 307 | + q := a.queues.SendQ(defaultQueueName) |
| 308 | + if q != nil { |
| 309 | + clusterInfoEvent := a.emitter.ClusterCacheInfoUpdateEvent(event.ClusterCacheInfoUpdate, clusterInfo) |
| 310 | + q.Add(clusterInfoEvent) |
| 311 | + logCtx.WithFields(logrus.Fields{ |
| 312 | + "sendq_len": q.Len(), |
| 313 | + "sendq_name": defaultQueueName, |
| 314 | + "applicationsCount": clusterInfo.ApplicationsCount, |
| 315 | + "apisCount": clusterInfo.CacheInfo.APIsCount, |
| 316 | + "resourcesCount": clusterInfo.CacheInfo.ResourcesCount, |
| 317 | + "reason": reason, |
| 318 | + }).Infof("Added ClusterCacheInfoUpdate event to send queue") |
| 319 | + } else { |
| 320 | + logCtx.Error("Default queue not found, unable to send ClusterCacheInfoUpdate event") |
| 321 | + } |
| 322 | +} |
0 commit comments