Skip to content

Commit bcb573d

Browse files
event-reporter(refactoring): params grouping, added new type ReportedEntityParentApp to pass data between methods
1 parent 899313d commit bcb573d

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

event_reporter/reporter/application_event_reporter.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ func (s *applicationEventReporter) StreamApplicationEvents(
165165
logCtx.WithError(err).Warn("failed to get parent application's revision metadata, resuming")
166166
}
167167

168-
err = s.processResource(ctx, *rs, parentApplicationEntity, logCtx, eventProcessingStartedAt, parentDesiredManifests, appTree, manifestGenErr, a, parentAppSyncRevisionsMetadata, applicationVersions, argoTrackingMetadata)
168+
err = s.processResource(ctx, *rs, logCtx, eventProcessingStartedAt, parentDesiredManifests, manifestGenErr, a, applicationVersions, &ReportedEntityParentApp{
169+
app: parentApplicationEntity,
170+
appTree: appTree,
171+
revisionsMetadata: parentAppSyncRevisionsMetadata,
172+
}, argoTrackingMetadata)
169173
if err != nil {
170174
s.metricsServer.IncErroredEventsCounter(metrics.MetricChildAppEventType, metrics.MetricEventUnknownErrorType, a.Name)
171175
return err
@@ -205,7 +209,11 @@ func (s *applicationEventReporter) StreamApplicationEvents(
205209
s.metricsServer.IncCachedIgnoredEventsCounter(metrics.MetricResourceEventType, a.Name)
206210
continue
207211
}
208-
err := s.processResource(ctx, rs, a, logCtx, eventProcessingStartedAt, desiredManifests, appTree, manifestGenErr, nil, revisionsMetadata, nil, argoTrackingMetadata)
212+
err := s.processResource(ctx, rs, logCtx, eventProcessingStartedAt, desiredManifests, manifestGenErr, nil, nil, &ReportedEntityParentApp{
213+
app: a,
214+
appTree: appTree,
215+
revisionsMetadata: revisionsMetadata,
216+
}, argoTrackingMetadata)
209217
if err != nil {
210218
s.metricsServer.IncErroredEventsCounter(metrics.MetricResourceEventType, metrics.MetricEventUnknownErrorType, a.Name)
211219
return err
@@ -254,15 +262,13 @@ func (s *applicationEventReporter) getAppForResourceReporting(
254262
func (s *applicationEventReporter) processResource(
255263
ctx context.Context,
256264
rs appv1.ResourceStatus,
257-
parentApplication *appv1.Application,
258265
logCtx *log.Entry,
259266
appEventProcessingStartedAt string,
260267
desiredManifests *apiclient.ManifestResponse,
261-
appTree *appv1.ApplicationTree,
262268
manifestGenErr bool,
263269
originalApplication *appv1.Application,
264-
revisionsMetadata *utils.AppSyncRevisionsMetadata,
265270
applicationVersions *apiclient.ApplicationVersions,
271+
reportedEntityParentApp *ReportedEntityParentApp,
266272
argoTrackingMetadata *ArgoTrackingMetadata,
267273
) error {
268274
metricsEventType := metrics.MetricResourceEventType
@@ -278,25 +284,29 @@ func (s *applicationEventReporter) processResource(
278284
// get resource desired state
279285
desiredState := getResourceDesiredState(&rs, desiredManifests, logCtx)
280286

281-
actualState, err := s.getResourceActualState(ctx, logCtx, metricsEventType, rs, parentApplication, originalApplication)
287+
actualState, err := s.getResourceActualState(ctx, logCtx, metricsEventType, rs, reportedEntityParentApp.app, originalApplication)
282288
if err != nil {
283289
return err
284290
}
285291
if actualState == nil {
286292
return nil
287293
}
288294

289-
parentApplicationToReport, revisionMetadataToReport := s.getAppForResourceReporting(rs, ctx, logCtx, parentApplication, revisionsMetadata)
295+
parentApplicationToReport, revisionMetadataToReport := s.getAppForResourceReporting(rs, ctx, logCtx, reportedEntityParentApp.app, reportedEntityParentApp.revisionsMetadata)
290296

291297
var originalAppRevisionMetadata *utils.AppSyncRevisionsMetadata = nil
292298

293299
if originalApplication != nil {
294300
originalAppRevisionMetadata, _ = s.getApplicationRevisionsMetadata(ctx, logCtx, originalApplication)
295301
}
296302

297-
ev, err := getResourceEventPayload(parentApplicationToReport, &rs, actualState, desiredState, appTree, manifestGenErr, appEventProcessingStartedAt, originalApplication, revisionMetadataToReport, originalAppRevisionMetadata, applicationVersions, argoTrackingMetadata)
303+
ev, err := getResourceEventPayload(&rs, actualState, desiredState, manifestGenErr, appEventProcessingStartedAt, originalApplication, originalAppRevisionMetadata, applicationVersions, &ReportedEntityParentApp{
304+
app: parentApplicationToReport,
305+
appTree: reportedEntityParentApp.appTree,
306+
revisionsMetadata: revisionMetadataToReport,
307+
}, argoTrackingMetadata)
298308
if err != nil {
299-
s.metricsServer.IncErroredEventsCounter(metricsEventType, metrics.MetricEventGetPayloadErrorType, parentApplication.Name)
309+
s.metricsServer.IncErroredEventsCounter(metricsEventType, metrics.MetricEventGetPayloadErrorType, reportedEntityParentApp.app.Name)
300310
logCtx.WithError(err).Warn("failed to get event payload, resuming")
301311
return nil
302312
}
@@ -308,7 +318,7 @@ func (s *applicationEventReporter) processResource(
308318
appName = appRes.Name
309319
} else {
310320
utils.LogWithResourceStatus(logCtx, rs).Info("streaming resource event")
311-
appName = parentApplication.Name
321+
appName = reportedEntityParentApp.app.Name
312322
}
313323

314324
if err := s.codefreshClient.SendEvent(ctx, appName, ev); err != nil {

event_reporter/reporter/event_payload.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ import (
2020
)
2121

2222
func getResourceEventPayload(
23-
parentApplication *appv1.Application,
2423
rs *appv1.ResourceStatus,
2524
actualState *application.ApplicationResourceResponse,
2625
desiredState *apiclient.Manifest,
27-
apptree *appv1.ApplicationTree,
2826
manifestGenErr bool,
2927
appEventProcessingStartedAt string,
3028
originalApplication *appv1.Application, // passed when rs is application
31-
revisionsMetadata *utils.AppSyncRevisionsMetadata,
3229
originalAppRevisionsMetadata *utils.AppSyncRevisionsMetadata, // passed when rs is application
3330
applicationVersions *apiclient.ApplicationVersions,
31+
reportedEntityParentApp *ReportedEntityParentApp,
3432
argoTrackingMetadata *ArgoTrackingMetadata,
3533
) (*events.Event, error) {
3634
var (
@@ -110,17 +108,17 @@ func getResourceEventPayload(
110108
actualState.Manifest = &manifest
111109
}
112110

113-
if (originalApplication != nil && originalApplication.DeletionTimestamp != nil) || parentApplication.ObjectMeta.DeletionTimestamp != nil {
111+
if (originalApplication != nil && originalApplication.DeletionTimestamp != nil) || reportedEntityParentApp.app.ObjectMeta.DeletionTimestamp != nil {
114112
// resource should be deleted in case if application in process of deletion
115113
desiredState.CompiledManifest = ""
116114
manifest := ""
117115
actualState.Manifest = &manifest
118116
}
119117

120-
if parentApplication.Status.OperationState != nil {
121-
syncStarted = parentApplication.Status.OperationState.StartedAt
122-
syncFinished = parentApplication.Status.OperationState.FinishedAt
123-
errors = append(errors, parseResourceSyncResultErrors(rs, parentApplication.Status.OperationState)...)
118+
if reportedEntityParentApp.app.Status.OperationState != nil {
119+
syncStarted = reportedEntityParentApp.app.Status.OperationState.StartedAt
120+
syncFinished = reportedEntityParentApp.app.Status.OperationState.FinishedAt
121+
errors = append(errors, parseResourceSyncResultErrors(rs, reportedEntityParentApp.app.Status.OperationState)...)
124122
}
125123

126124
// for primitive resources that are synced right away and don't require progression time (like configmap)
@@ -138,7 +136,7 @@ func getResourceEventPayload(
138136
}
139137

140138
if originalApplication != nil {
141-
errors = append(errors, parseAggregativeHealthErrorsOfApplication(originalApplication, apptree)...)
139+
errors = append(errors, parseAggregativeHealthErrorsOfApplication(originalApplication, reportedEntityParentApp.appTree)...)
142140
}
143141

144142
if len(desiredState.RawManifest) == 0 && len(desiredState.CompiledManifest) != 0 {
@@ -158,25 +156,25 @@ func getResourceEventPayload(
158156
DesiredManifest: desiredState.CompiledManifest,
159157
ActualManifest: *actualState.Manifest,
160158
GitManifest: desiredState.RawManifest,
161-
RepoURL: parentApplication.Status.Sync.ComparedTo.Source.RepoURL,
159+
RepoURL: reportedEntityParentApp.app.Status.Sync.ComparedTo.Source.RepoURL,
162160
Path: desiredState.Path,
163-
Revision: utils.GetApplicationLatestRevision(parentApplication),
164-
OperationSyncRevision: utils.GetOperationRevision(parentApplication),
165-
HistoryId: utils.GetLatestAppHistoryId(parentApplication),
166-
AppName: parentApplication.Name,
167-
AppNamespace: parentApplication.Namespace,
168-
AppUID: string(parentApplication.ObjectMeta.UID),
169-
AppLabels: parentApplication.Labels,
161+
Revision: utils.GetApplicationLatestRevision(reportedEntityParentApp.app),
162+
OperationSyncRevision: utils.GetOperationRevision(reportedEntityParentApp.app),
163+
HistoryId: utils.GetLatestAppHistoryId(reportedEntityParentApp.app),
164+
AppName: reportedEntityParentApp.app.Name,
165+
AppNamespace: reportedEntityParentApp.app.Namespace,
166+
AppUID: string(reportedEntityParentApp.app.ObjectMeta.UID),
167+
AppLabels: reportedEntityParentApp.app.Labels,
170168
SyncStatus: string(rs.Status),
171169
SyncStartedAt: syncStarted,
172170
SyncFinishedAt: syncFinished,
173-
Cluster: parentApplication.Spec.Destination.Server,
171+
Cluster: reportedEntityParentApp.app.Spec.Destination.Server,
174172
AppInstanceLabelKey: *argoTrackingMetadata.AppInstanceLabelKey,
175173
TrackingMethod: string(*argoTrackingMetadata.TrackingMethod),
176174
}
177175

178-
if revisionsMetadata != nil && revisionsMetadata.SyncRevisions != nil {
179-
revisionMetadata := getApplicationLegacyRevisionDetails(parentApplication, revisionsMetadata)
176+
if reportedEntityParentApp.revisionsMetadata != nil && reportedEntityParentApp.revisionsMetadata.SyncRevisions != nil {
177+
revisionMetadata := getApplicationLegacyRevisionDetails(reportedEntityParentApp.app, reportedEntityParentApp.revisionsMetadata)
180178
if revisionMetadata != nil {
181179
source.CommitMessage = revisionMetadata.Message
182180
source.CommitAuthor = revisionMetadata.Author
@@ -188,7 +186,7 @@ func getResourceEventPayload(
188186
source.HealthStatus = (*string)(&rs.Health.Status)
189187
source.HealthMessage = &rs.Health.Message
190188
if rs.Health.Status != health.HealthStatusHealthy {
191-
errors = append(errors, parseAggregativeHealthErrors(rs, apptree, false)...)
189+
errors = append(errors, parseAggregativeHealthErrors(rs, reportedEntityParentApp.appTree, false)...)
192190
}
193191
}
194192

event_reporter/reporter/event_payload_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func TestGetResourceEventPayload(t *testing.T) {
5858
}},
5959
}
6060

61-
event, err := getResourceEventPayload(&app, &rs, &actualState, &desiredState, &appTree, true, "", nil, &revisionMetadata, nil, &repoApiclient.ApplicationVersions{}, getMockedArgoTrackingMetadata())
61+
event, err := getResourceEventPayload(&rs, &actualState, &desiredState, true, "", nil, nil, &repoApiclient.ApplicationVersions{}, &ReportedEntityParentApp{
62+
app: &app,
63+
appTree: &appTree,
64+
revisionsMetadata: &revisionMetadata,
65+
}, getMockedArgoTrackingMetadata())
6266
require.NoError(t, err)
6367

6468
var eventPayload events.EventPayload
@@ -90,7 +94,11 @@ func TestGetResourceEventPayload(t *testing.T) {
9094
SyncRevisions: []*utils.RevisionWithMetadata{},
9195
}
9296

93-
event, err := getResourceEventPayload(&app, &rs, &actualState, &desiredState, &appTree, true, "", nil, &revisionMetadata, nil, &repoApiclient.ApplicationVersions{}, getMockedArgoTrackingMetadata())
97+
event, err := getResourceEventPayload(&rs, &actualState, &desiredState, true, "", nil, nil, &repoApiclient.ApplicationVersions{}, &ReportedEntityParentApp{
98+
app: &app,
99+
appTree: &appTree,
100+
revisionsMetadata: &revisionMetadata,
101+
}, getMockedArgoTrackingMetadata())
94102
require.NoError(t, err)
95103

96104
var eventPayload events.EventPayload
@@ -117,6 +125,9 @@ func TestGetResourceEventPayloadWithoutRevision(t *testing.T) {
117125
}
118126
appTree := v1alpha1.ApplicationTree{}
119127

120-
_, err := getResourceEventPayload(&app, &rs, &actualState, &desiredState, &appTree, true, "", nil, nil, nil, &repoApiclient.ApplicationVersions{}, getMockedArgoTrackingMetadata())
128+
_, err := getResourceEventPayload(&rs, &actualState, &desiredState, true, "", nil, nil, &repoApiclient.ApplicationVersions{}, &ReportedEntityParentApp{
129+
app: &app,
130+
appTree: &appTree,
131+
}, getMockedArgoTrackingMetadata())
121132
assert.NoError(t, err)
122133
}

0 commit comments

Comments
 (0)