44 "context"
55 "encoding/json"
66 "fmt"
7+ "github.com/argoproj/argo-cd/v2/event_reporter/utils"
78 "math"
89 "reflect"
910 "strings"
@@ -63,13 +64,13 @@ func NewApplicationEventReporter(cache *servercache.Cache, applicationServiceCli
6364}
6465
6566func (s * applicationEventReporter ) shouldSendResourceEvent (a * appv1.Application , rs appv1.ResourceStatus ) bool {
66- logCtx := logWithResourceStatus (log .WithFields (log.Fields {
67+ logCtx := utils . LogWithResourceStatus (log .WithFields (log.Fields {
6768 "app" : a .Name ,
6869 "gvk" : fmt .Sprintf ("%s/%s/%s" , rs .Group , rs .Version , rs .Kind ),
6970 "resource" : fmt .Sprintf ("%s/%s" , rs .Namespace , rs .Name ),
7071 }), rs )
7172
72- cachedRes , err := s .cache .GetLastResourceEvent (a , rs , getApplicationLatestRevision (a ))
73+ cachedRes , err := s .cache .GetLastResourceEvent (a , rs , utils . GetApplicationLatestRevision (a ))
7374 if err != nil {
7475 logCtx .Debug ("resource not in cache" )
7576 return true
@@ -144,19 +145,19 @@ func (s *applicationEventReporter) StreamApplicationEvents(
144145
145146 logCtx .Info ("getting parent application name" )
146147
147- parentAppIdentity := getParentAppIdentity (a , appInstanceLabelKey , trackingMethod )
148+ parentAppIdentity := utils . GetParentAppIdentity (a , appInstanceLabelKey , trackingMethod )
148149
149- if isChildApp (parentAppIdentity ) {
150+ if utils . IsChildApp (parentAppIdentity ) {
150151 logCtx .Info ("processing as child application" )
151152 parentApplicationEntity , err := s .applicationServiceClient .Get (ctx , & application.ApplicationQuery {
152- Name : & parentAppIdentity .name ,
153- AppNamespace : & parentAppIdentity .namespace ,
153+ Name : & parentAppIdentity .Name ,
154+ AppNamespace : & parentAppIdentity .Namespace ,
154155 })
155156 if err != nil {
156157 return fmt .Errorf ("failed to get parent application entity: %w" , err )
157158 }
158159
159- rs := getAppAsResource (a )
160+ rs := utils . GetAppAsResource (a )
160161
161162 parentDesiredManifests , err , manifestGenErr := s .getDesiredManifests (ctx , parentApplicationEntity , logCtx )
162163 if err != nil {
@@ -165,13 +166,13 @@ func (s *applicationEventReporter) StreamApplicationEvents(
165166
166167 // helm app hasnt revision
167168 // TODO: add check if it helm application
168- parentOperationRevision := getOperationRevision (parentApplicationEntity )
169+ parentOperationRevision := utils . GetOperationRevision (parentApplicationEntity )
169170 parentRevisionMetadata , err := s .getApplicationRevisionDetails (ctx , parentApplicationEntity , parentOperationRevision )
170171 if err != nil {
171172 logCtx .WithError (err ).Warn ("failed to get parent application's revision metadata, resuming" )
172173 }
173174
174- setHealthStatusIfMissing (rs )
175+ utils . SetHealthStatusIfMissing (rs )
175176 err = s .processResource (ctx , * rs , parentApplicationEntity , logCtx , ts , parentDesiredManifests , appTree , manifestGenErr , a , parentRevisionMetadata , appInstanceLabelKey , trackingMethod , desiredManifests .ApplicationVersions )
176177 if err != nil {
177178 s .metricsServer .IncErroredEventsCounter (metrics .MetricChildAppEventType , metrics .MetricEventUnknownErrorType , a .Name )
@@ -193,7 +194,7 @@ func (s *applicationEventReporter) StreamApplicationEvents(
193194 return nil
194195 }
195196
196- logWithAppStatus (a , logCtx , ts ).Info ("sending root application event" )
197+ utils . LogWithAppStatus (a , logCtx , ts ).Info ("sending root application event" )
197198 if err := s .codefreshClient .SendEvent (ctx , a .Name , appEvent ); err != nil {
198199 s .metricsServer .IncErroredEventsCounter (metrics .MetricParentAppEventType , metrics .MetricEventDeliveryErrorType , a .Name )
199200 return fmt .Errorf ("failed to send event for root application %s/%s: %w" , a .Namespace , a .Name , err )
@@ -202,14 +203,14 @@ func (s *applicationEventReporter) StreamApplicationEvents(
202203 s .metricsServer .ObserveEventProcessingDurationHistogramDuration (a .Name , metrics .MetricParentAppEventType , reconcileDuration )
203204 }
204205
205- revisionMetadata , _ := s .getApplicationRevisionDetails (ctx , a , getOperationRevision (a ))
206+ revisionMetadata , _ := s .getApplicationRevisionDetails (ctx , a , utils . GetOperationRevision (a ))
206207 // for each resource in the application get desired and actual state,
207208 // then stream the event
208209 for _ , rs := range a .Status .Resources {
209- if isApp (rs ) {
210+ if utils . IsApp (rs ) {
210211 continue
211212 }
212- setHealthStatusIfMissing (& rs )
213+ utils . SetHealthStatusIfMissing (& rs )
213214 if ! ignoreResourceCache && ! s .shouldSendResourceEvent (a , rs ) {
214215 s .metricsServer .IncCachedIgnoredEventsCounter (metrics .MetricResourceEventType , a .Name )
215216 continue
@@ -239,7 +240,7 @@ func (s *applicationEventReporter) getAppForResourceReporting(
239240 return a , revisionMetadata
240241 }
241242
242- revisionMetadataToReport , err := s .getApplicationRevisionDetails (ctx , latestAppStatus , getOperationRevision (latestAppStatus ))
243+ revisionMetadataToReport , err := s .getApplicationRevisionDetails (ctx , latestAppStatus , utils . GetOperationRevision (latestAppStatus ))
243244
244245 if err != nil {
245246 return a , revisionMetadata
@@ -264,7 +265,7 @@ func (s *applicationEventReporter) processResource(
264265 applicationVersions * apiclient.ApplicationVersions ,
265266) error {
266267 metricsEventType := metrics .MetricResourceEventType
267- if isApp (rs ) {
268+ if utils . IsApp (rs ) {
268269 metricsEventType = metrics .MetricChildAppEventType
269270 }
270271
@@ -290,7 +291,7 @@ func (s *applicationEventReporter) processResource(
290291 var originalAppRevisionMetadata * appv1.RevisionMetadata = nil
291292
292293 if originalApplication != nil {
293- originalAppRevisionMetadata , _ = s .getApplicationRevisionDetails (ctx , originalApplication , getOperationRevision (originalApplication ))
294+ originalAppRevisionMetadata , _ = s .getApplicationRevisionDetails (ctx , originalApplication , utils . GetOperationRevision (originalApplication ))
294295 }
295296
296297 ev , err := getResourceEventPayload (parentApplicationToReport , & rs , actualState , desiredState , appTree , manifestGenErr , ts , originalApplication , revisionMetadataToReport , originalAppRevisionMetadata , appInstanceLabelKey , trackingMethod , applicationVersions )
@@ -302,11 +303,11 @@ func (s *applicationEventReporter) processResource(
302303
303304 appRes := appv1.Application {}
304305 appName := ""
305- if isApp (rs ) && actualState .Manifest != nil && json .Unmarshal ([]byte (* actualState .Manifest ), & appRes ) == nil {
306- logWithAppStatus (& appRes , logCtx , ts ).Info ("streaming resource event" )
306+ if utils . IsApp (rs ) && actualState .Manifest != nil && json .Unmarshal ([]byte (* actualState .Manifest ), & appRes ) == nil {
307+ utils . LogWithAppStatus (& appRes , logCtx , ts ).Info ("streaming resource event" )
307308 appName = appRes .Name
308309 } else {
309- logWithResourceStatus (logCtx , rs ).Info ("streaming resource event" )
310+ utils . LogWithResourceStatus (logCtx , rs ).Info ("streaming resource event" )
310311 appName = rs .Name
311312 }
312313
@@ -320,15 +321,15 @@ func (s *applicationEventReporter) processResource(
320321 return nil
321322 }
322323
323- if err := s .cache .SetLastResourceEvent (parentApplicationToReport , rs , resourceEventCacheExpiration , getApplicationLatestRevision (parentApplicationToReport )); err != nil {
324+ if err := s .cache .SetLastResourceEvent (parentApplicationToReport , rs , resourceEventCacheExpiration , utils . GetApplicationLatestRevision (parentApplicationToReport )); err != nil {
324325 logCtx .WithError (err ).Warn ("failed to cache resource event" )
325326 }
326327
327328 return nil
328329}
329330
330331func (s * applicationEventReporter ) getResourceActualState (ctx context.Context , logCtx * log.Entry , metricsEventType metrics.MetricEventType , rs appv1.ResourceStatus , parentApplication * appv1.Application , childApplication * appv1.Application ) (* application.ApplicationResourceResponse , error ) {
331- if isApp (rs ) {
332+ if utils . IsApp (rs ) {
332333 if childApplication .IsEmptyTypeMeta () {
333334 // make sure there is type meta on object
334335 childApplication .SetDefaultTypeMeta ()
0 commit comments