@@ -20,152 +20,97 @@ import (
2020)
2121
2222func getResourceEventPayload (
23- rs * appv1.ResourceStatus ,
24- actualState * application.ApplicationResourceResponse ,
25- desiredState * apiclient.Manifest ,
26- manifestGenErr bool ,
2723 appEventProcessingStartedAt string ,
28- originalApplication * appv1.Application , // passed when rs is application
29- originalAppRevisionsMetadata * utils.AppSyncRevisionsMetadata , // passed when rs is application
30- applicationVersions * apiclient.ApplicationVersions ,
24+ rr * ReportedResource ,
3125 reportedEntityParentApp * ReportedEntityParentApp ,
3226 argoTrackingMetadata * ArgoTrackingMetadata ,
3327) (* events.Event , error ) {
3428 var (
3529 err error
3630 syncStarted = metav1 .Now ()
3731 syncFinished * metav1.Time
38- errors = []* events.ObjectError {}
3932 logCtx * log.Entry
4033 )
4134
42- if originalApplication != nil {
43- logCtx = log .WithField ("application" , originalApplication .Name )
35+ if rr . rsAsAppInfo . app != nil {
36+ logCtx = log .WithField ("application" , rr . rsAsAppInfo . app .Name )
4437 } else {
4538 logCtx = log .NewEntry (log .StandardLogger ())
4639 }
4740
48- object := []byte (* actualState .Manifest )
41+ object := []byte (* rr . actualState .Manifest )
4942
50- if originalAppRevisionsMetadata != nil && len (object ) != 0 {
51- actualObject , err := appv1 .UnmarshalToUnstructured (* actualState .Manifest )
43+ if rr . rsAsAppInfo . revisionsMetadata != nil && len (object ) != 0 {
44+ actualObject , err := appv1 .UnmarshalToUnstructured (* rr . actualState .Manifest )
5245
53- if err == nil {
54- actualObject = utils .AddCommitsDetailsToAnnotations (actualObject , originalAppRevisionsMetadata )
55- if originalApplication != nil {
56- actualObject = utils .AddCommitDetailsToLabels (actualObject , getApplicationLegacyRevisionDetails (originalApplication , originalAppRevisionsMetadata ))
57- }
46+ if err != nil {
47+ return nil , fmt .Errorf ("failed to unmarshal manifest: %w" , err )
48+ }
5849
59- object , err = actualObject .MarshalJSON ()
60- if err != nil {
61- return nil , fmt .Errorf ("failed to marshal unstructured object: %w" , err )
62- }
50+ object , err = addCommitDetailsToUnstructured (actualObject , rr )
51+ if err != nil {
52+ return nil , err
6353 }
6454 }
6555 if len (object ) == 0 {
66- if len (desiredState .CompiledManifest ) == 0 {
67- // no actual or desired state, don't send event
68- u := & unstructured.Unstructured {}
69- apiVersion := rs .Version
70- if rs .Group != "" {
71- apiVersion = rs .Group + "/" + rs .Version
72- }
73-
74- u .SetAPIVersion (apiVersion )
75- u .SetKind (rs .Kind )
76- u .SetName (rs .Name )
77- u .SetNamespace (rs .Namespace )
78- if originalAppRevisionsMetadata != nil {
79- u = utils .AddCommitsDetailsToAnnotations (u , originalAppRevisionsMetadata )
80- if originalApplication != nil {
81- u = utils .AddCommitDetailsToLabels (u , getApplicationLegacyRevisionDetails (originalApplication , originalAppRevisionsMetadata ))
82- }
83- }
84-
85- object , err = u .MarshalJSON ()
56+ if len (rr .desiredState .CompiledManifest ) == 0 {
57+ object , err = buildEventObjectAsLiveAndCompiledManifestsEmpty (rr )
8658 if err != nil {
87- return nil , fmt . Errorf ( "failed to marshal unstructured object: %w" , err )
59+ return nil , err
8860 }
8961 } else {
90- // no actual state, use desired state as event object
91- unstructuredWithNamespace , err := utils .AddDestNamespaceToManifest ([]byte (desiredState .CompiledManifest ), rs )
62+ object , err = useCompiledManifestAsEventObject (rr )
9263 if err != nil {
93- return nil , fmt . Errorf ( "failed to add destination namespace to manifest: %w" , err )
64+ return nil , err
9465 }
95- if originalAppRevisionsMetadata != nil {
96- unstructuredWithNamespace = utils .AddCommitsDetailsToAnnotations (unstructuredWithNamespace , originalAppRevisionsMetadata )
97- if originalApplication != nil {
98- unstructuredWithNamespace = utils .AddCommitDetailsToLabels (unstructuredWithNamespace , getApplicationLegacyRevisionDetails (originalApplication , originalAppRevisionsMetadata ))
99- }
100- }
101-
102- object , _ = unstructuredWithNamespace .MarshalJSON ()
10366 }
104- } else if rs .RequiresPruning && ! manifestGenErr {
67+ } else if rr . rs .RequiresPruning && ! rr . manifestGenErr {
10568 // resource should be deleted
106- desiredState .CompiledManifest = ""
107- manifest := ""
108- actualState .Manifest = & manifest
69+ makeDesiredAndLiveManifestEmpty (rr .actualState , rr .desiredState )
10970 }
11071
111- if (originalApplication != nil && originalApplication .DeletionTimestamp != nil ) || reportedEntityParentApp .app .ObjectMeta .DeletionTimestamp != nil {
72+ if (rr . rsAsAppInfo . app != nil && rr . rsAsAppInfo . app .DeletionTimestamp != nil ) || reportedEntityParentApp .app .ObjectMeta .DeletionTimestamp != nil {
11273 // resource should be deleted in case if application in process of deletion
113- desiredState .CompiledManifest = ""
114- manifest := ""
115- actualState .Manifest = & manifest
74+ makeDesiredAndLiveManifestEmpty (rr .actualState , rr .desiredState )
75+ }
76+
77+ if len (rr .desiredState .RawManifest ) == 0 && len (rr .desiredState .CompiledManifest ) != 0 {
78+ // for handling helm defined resources, etc...
79+ y , err := yaml .JSONToYAML ([]byte (rr .desiredState .CompiledManifest ))
80+ if err == nil {
81+ rr .desiredState .RawManifest = string (y )
82+ }
11683 }
11784
11885 if reportedEntityParentApp .app .Status .OperationState != nil {
11986 syncStarted = reportedEntityParentApp .app .Status .OperationState .StartedAt
12087 syncFinished = reportedEntityParentApp .app .Status .OperationState .FinishedAt
121- errors = append (errors , parseResourceSyncResultErrors (rs , reportedEntityParentApp .app .Status .OperationState )... )
12288 }
12389
12490 // for primitive resources that are synced right away and don't require progression time (like configmap)
125- if rs .Status == appv1 .SyncStatusCodeSynced && rs .Health != nil && rs .Health .Status == health .HealthStatusHealthy {
91+ if rr . rs .Status == appv1 .SyncStatusCodeSynced && rr . rs .Health != nil && rr . rs .Health .Status == health .HealthStatusHealthy {
12692 syncFinished = & syncStarted
12793 }
12894
129- // parent application not include errors in application originally was created with broken state, for example in destination missed namespace
130- if originalApplication != nil && originalApplication .Status .OperationState != nil {
131- errors = append (errors , parseApplicationSyncResultErrors (originalApplication .Status .OperationState )... )
132- }
133-
134- if originalApplication != nil && originalApplication .Status .Conditions != nil {
135- errors = append (errors , parseApplicationSyncResultErrorsFromConditions (originalApplication .Status )... )
136- }
137-
138- if originalApplication != nil {
139- errors = append (errors , parseAggregativeHealthErrorsOfApplication (originalApplication , reportedEntityParentApp .appTree )... )
140- }
141-
142- if len (desiredState .RawManifest ) == 0 && len (desiredState .CompiledManifest ) != 0 {
143- // for handling helm defined resources, etc...
144- y , err := yaml .JSONToYAML ([]byte (desiredState .CompiledManifest ))
145- if err == nil {
146- desiredState .RawManifest = string (y )
147- }
148- }
149-
150- applicationVersionsEvents , err := utils .RepoAppVersionsToEvent (applicationVersions )
95+ applicationVersionsEvents , err := utils .RepoAppVersionsToEvent (rr .rsAsAppInfo .applicationVersions )
15196 if err != nil {
15297 logCtx .Errorf ("failed to convert appVersions: %v" , err )
15398 }
15499
155100 source := events.ObjectSource {
156- DesiredManifest : desiredState .CompiledManifest ,
157- ActualManifest : * actualState .Manifest ,
158- GitManifest : desiredState .RawManifest ,
101+ DesiredManifest : rr . desiredState .CompiledManifest ,
102+ ActualManifest : * rr . actualState .Manifest ,
103+ GitManifest : rr . desiredState .RawManifest ,
159104 RepoURL : reportedEntityParentApp .app .Status .Sync .ComparedTo .Source .RepoURL ,
160- Path : desiredState .Path ,
105+ Path : rr . desiredState .Path ,
161106 Revision : utils .GetApplicationLatestRevision (reportedEntityParentApp .app ),
162107 OperationSyncRevision : utils .GetOperationRevision (reportedEntityParentApp .app ),
163108 HistoryId : utils .GetLatestAppHistoryId (reportedEntityParentApp .app ),
164109 AppName : reportedEntityParentApp .app .Name ,
165110 AppNamespace : reportedEntityParentApp .app .Namespace ,
166111 AppUID : string (reportedEntityParentApp .app .ObjectMeta .UID ),
167112 AppLabels : reportedEntityParentApp .app .Labels ,
168- SyncStatus : string (rs .Status ),
113+ SyncStatus : string (rr . rs .Status ),
169114 SyncStartedAt : syncStarted ,
170115 SyncFinishedAt : syncFinished ,
171116 Cluster : reportedEntityParentApp .app .Spec .Destination .Server ,
@@ -182,32 +127,121 @@ func getResourceEventPayload(
182127 }
183128 }
184129
185- if rs .Health != nil {
186- source .HealthStatus = (* string )(& rs .Health .Status )
187- source .HealthMessage = & rs .Health .Message
188- if rs .Health .Status != health .HealthStatusHealthy {
189- errors = append (errors , parseAggregativeHealthErrors (rs , reportedEntityParentApp .appTree , false )... )
190- }
130+ if rr .rs .Health != nil {
131+ source .HealthStatus = (* string )(& rr .rs .Health .Status )
132+ source .HealthMessage = & rr .rs .Health .Message
191133 }
192134
193135 payload := events.EventPayload {
194136 Timestamp : appEventProcessingStartedAt ,
195137 Object : object ,
196138 Source : & source ,
197- Errors : errors ,
139+ Errors : getResourceEventPayloadErrors ( rr , reportedEntityParentApp ) ,
198140 AppVersions : applicationVersionsEvents ,
199141 }
200142
201143 logCtx .Infof ("AppVersion before encoding: %v" , utils .SafeString (payload .AppVersions .AppVersion ))
202144
203145 payloadBytes , err := json .Marshal (& payload )
204146 if err != nil {
205- return nil , fmt .Errorf ("failed to marshal payload for resource %s/%s: %w" , rs .Namespace , rs .Name , err )
147+ return nil , fmt .Errorf ("failed to marshal payload for resource %s/%s: %w" , rr . rs .Namespace , rr . rs .Name , err )
206148 }
207149
208150 return & events.Event {Payload : payloadBytes }, nil
209151}
210152
153+ func getResourceEventPayloadErrors (
154+ rr * ReportedResource ,
155+ reportedEntityParentApp * ReportedEntityParentApp ,
156+ ) []* events.ObjectError {
157+ var errors = []* events.ObjectError {}
158+
159+ if reportedEntityParentApp .app .Status .OperationState != nil {
160+ errors = append (errors , parseResourceSyncResultErrors (rr .rs , reportedEntityParentApp .app .Status .OperationState )... )
161+ }
162+
163+ // parent application not include errors in application originally was created with broken state, for example in destination missed namespace
164+ if rr .rsAsAppInfo .app != nil && rr .rsAsAppInfo .app .Status .OperationState != nil {
165+ errors = append (errors , parseApplicationSyncResultErrors (rr .rsAsAppInfo .app .Status .OperationState )... )
166+ }
167+
168+ if rr .rsAsAppInfo .app != nil && rr .rsAsAppInfo .app .Status .Conditions != nil {
169+ errors = append (errors , parseApplicationSyncResultErrorsFromConditions (rr .rsAsAppInfo .app .Status )... )
170+ }
171+
172+ if rr .rsAsAppInfo .app != nil {
173+ errors = append (errors , parseAggregativeHealthErrorsOfApplication (rr .rsAsAppInfo .app , reportedEntityParentApp .appTree )... )
174+ }
175+
176+ if rr .rs .Health != nil {
177+ if rr .rs .Health .Status != health .HealthStatusHealthy {
178+ errors = append (errors , parseAggregativeHealthErrors (rr .rs , reportedEntityParentApp .appTree , false )... )
179+ }
180+ }
181+
182+ return errors
183+ }
184+
185+ func useCompiledManifestAsEventObject (
186+ rr * ReportedResource ,
187+ ) ([]byte , error ) {
188+ // no actual state, use desired state as event object
189+ unstructuredWithNamespace , err := utils .AddDestNamespaceToManifest ([]byte (rr .desiredState .CompiledManifest ), rr .rs )
190+ if err != nil {
191+ return nil , fmt .Errorf ("failed to add destination namespace to manifest: %w" , err )
192+ }
193+
194+ return addCommitDetailsToUnstructured (unstructuredWithNamespace , rr )
195+ }
196+
197+ func buildEventObjectAsLiveAndCompiledManifestsEmpty (
198+ rr * ReportedResource ,
199+ ) ([]byte , error ) {
200+ // no actual or desired state, don't send event
201+ u := & unstructured.Unstructured {}
202+ apiVersion := rr .rs .Version
203+ if rr .rs .Group != "" {
204+ apiVersion = rr .rs .Group + "/" + rr .rs .Version
205+ }
206+
207+ u .SetAPIVersion (apiVersion )
208+ u .SetKind (rr .rs .Kind )
209+ u .SetName (rr .rs .Name )
210+ u .SetNamespace (rr .rs .Namespace )
211+
212+ return addCommitDetailsToUnstructured (u , rr )
213+ }
214+
215+ // when empty minifests reported to codefresh they will get deleted
216+ func makeDesiredAndLiveManifestEmpty (
217+ actualState * application.ApplicationResourceResponse ,
218+ desiredState * apiclient.Manifest ,
219+ ) {
220+ // resource should be deleted
221+ desiredState .CompiledManifest = ""
222+ manifest := ""
223+ actualState .Manifest = & manifest
224+ }
225+
226+ func addCommitDetailsToUnstructured (
227+ u * unstructured.Unstructured ,
228+ rr * ReportedResource ,
229+ ) ([]byte , error ) {
230+ if rr .rsAsAppInfo .revisionsMetadata != nil {
231+ u = utils .AddCommitsDetailsToAnnotations (u , rr .rsAsAppInfo .revisionsMetadata )
232+ if rr .rsAsAppInfo .app != nil {
233+ u = utils .AddCommitDetailsToLabels (u , getApplicationLegacyRevisionDetails (rr .rsAsAppInfo .app , rr .rsAsAppInfo .revisionsMetadata ))
234+ }
235+ }
236+
237+ object , err := u .MarshalJSON ()
238+ if err != nil {
239+ return nil , fmt .Errorf ("failed to marshal unstructured object: %w" , err )
240+ }
241+
242+ return object , err
243+ }
244+
211245func (s * applicationEventReporter ) getApplicationEventPayload (
212246 ctx context.Context ,
213247 a * appv1.Application ,
0 commit comments