Skip to content

Commit 3539bc7

Browse files
event-reporter(resource source payload): added AppSourceIdx with source index to which this resource belongs. Also updated logic of retrieving correct git commit info based on AppSourceIdx
1 parent 56d46f2 commit 3539bc7

File tree

12 files changed

+511
-259
lines changed

12 files changed

+511
-259
lines changed

assets/swagger.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5294,6 +5294,14 @@
52945294
"sourceType": {
52955295
"type": "string"
52965296
},
5297+
"sourcesManifestsStartingIdx": {
5298+
"type": "array",
5299+
"title": "for multisourced apps will be [0,12,20], so this means that 0-11 - from first app source, 12-19 from second one, 20-x - third one",
5300+
"items": {
5301+
"type": "integer",
5302+
"format": "int32"
5303+
}
5304+
},
52975305
"verifyResult": {
52985306
"type": "string",
52995307
"title": "Raw response of git verify-commit operation (always the empty string for Helm)"

event_reporter/reporter/app_revision_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestGetRevisionsDetails(t *testing.T) {
5454
newAppLister(),
5555
appServiceClient,
5656
&metrics.MetricsServer{},
57+
fakeArgoDb(),
5758
}
5859

5960
result, _ := reporter.getRevisionsDetails(context.Background(), &app, []string{expectedRevision})
@@ -118,6 +119,7 @@ func TestGetRevisionsDetails(t *testing.T) {
118119
newAppLister(),
119120
appServiceClient,
120121
&metrics.MetricsServer{},
122+
fakeArgoDb(),
121123
}
122124

123125
result, _ := reporter.getRevisionsDetails(context.Background(), &app, []string{expectedRevision1, expectedRevision2})
@@ -159,6 +161,7 @@ func TestGetRevisionsDetails(t *testing.T) {
159161
newAppLister(),
160162
appServiceClient,
161163
&metrics.MetricsServer{},
164+
fakeArgoDb(),
162165
}
163166

164167
result, _ := reporter.getRevisionsDetails(context.Background(), &app, []string{expectedRevision})

event_reporter/reporter/application_event_reporter.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (s *applicationEventReporter) processResource(
323323
})
324324

325325
// get resource desired state
326-
desiredState := getResourceDesiredState(&rs, desiredManifests, logCtx)
326+
desiredState, appSourceIdx := getResourceDesiredState(&rs, desiredManifests, logCtx)
327327

328328
actualState, err := s.getResourceActualState(ctx, logCtx, metricsEventType, rs, reportedEntityParentApp.app, originalApplication)
329329
if err != nil {
@@ -348,16 +348,19 @@ func (s *applicationEventReporter) processResource(
348348
actualState: actualState,
349349
desiredState: desiredState,
350350
manifestGenErr: manifestGenErr,
351+
appSourceIdx: appSourceIdx,
351352
rsAsAppInfo: &ReportedResourceAsApp{
352353
app: originalApplication,
353354
revisionsMetadata: originalAppRevisionMetadata,
354355
applicationVersions: applicationVersions,
355356
},
356357
},
357358
&ReportedEntityParentApp{
358-
app: parentApplicationToReport,
359-
appTree: reportedEntityParentApp.appTree,
360-
revisionsMetadata: revisionMetadataToReport,
359+
app: parentApplicationToReport,
360+
appTree: reportedEntityParentApp.appTree,
361+
revisionsMetadata: revisionMetadataToReport,
362+
validatedDestination: reportedEntityParentApp.validatedDestination,
363+
desiredManifests: reportedEntityParentApp.desiredManifests,
361364
},
362365
argoTrackingMetadata,
363366
)
@@ -518,11 +521,11 @@ func applicationMetadataChanged(ae *appv1.ApplicationWatchEvent, cachedApp *appv
518521
return !reflect.DeepEqual(newEventAppMeta, cachedAppMeta)
519522
}
520523

521-
func getResourceDesiredState(rs *appv1.ResourceStatus, ds *apiclient.ManifestResponse, logger *log.Entry) *apiclient.Manifest {
524+
func getResourceDesiredState(rs *appv1.ResourceStatus, ds *apiclient.ManifestResponse, logger *log.Entry) (manifest *apiclient.Manifest, sourceIdx int32) {
522525
if ds == nil {
523-
return &apiclient.Manifest{}
526+
return &apiclient.Manifest{}, 0
524527
}
525-
for _, m := range ds.Manifests {
528+
for idx, m := range ds.Manifests {
526529
u, err := appv1.UnmarshalToUnstructured(m.CompiledManifest)
527530
if err != nil {
528531
logger.WithError(err).Warnf("failed to unmarshal compiled manifest")
@@ -542,11 +545,27 @@ func getResourceDesiredState(rs *appv1.ResourceStatus, ds *apiclient.ManifestRes
542545
m.RawManifest = m.CompiledManifest
543546
}
544547

545-
return m
548+
return m, getResourceSourceIdxFromManifestResponse(idx, ds)
546549
}
547550
}
548551

549552
// no desired state for resource
550553
// it's probably deleted from git
551-
return &apiclient.Manifest{}
554+
return &apiclient.Manifest{}, 0
555+
}
556+
557+
func getResourceSourceIdxFromManifestResponse(rsIdx int, ds *apiclient.ManifestResponse) int32 {
558+
if ds.SourcesManifestsStartingIdx == nil {
559+
return -1
560+
}
561+
562+
sourceIdx := int32(-1)
563+
564+
for currentSourceIdx, sourceStartingIdx := range ds.SourcesManifestsStartingIdx {
565+
if int32(rsIdx) >= sourceStartingIdx {
566+
sourceIdx = int32(currentSourceIdx)
567+
}
568+
}
569+
570+
return sourceIdx
552571
}

event_reporter/reporter/application_event_reporter_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/argoproj/argo-cd/v2/util/db"
8+
"github.com/argoproj/argo-cd/v2/util/settings"
9+
"k8s.io/client-go/kubernetes/fake"
710
"net/http"
811
"testing"
912
"time"
@@ -19,8 +22,8 @@ import (
1922
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
2023
apiclientapppkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
2124
appv1reg "github.com/argoproj/argo-cd/v2/pkg/apis/application"
25+
repoapiclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient"
2226
"github.com/argoproj/argo-cd/v2/util/io"
23-
2427
"google.golang.org/grpc"
2528
"k8s.io/apimachinery/pkg/runtime"
2629

@@ -93,6 +96,12 @@ func fakeAppServiceClient() apiclientapppkg.ApplicationServiceClient {
9396
return applicationServiceClient
9497
}
9598

99+
func fakeArgoDb() db.ArgoDB {
100+
clientset := fake.NewSimpleClientset()
101+
102+
return db.NewDB("", settings.NewSettingsManager(context.Background(), clientset, testNamespace), clientset)
103+
}
104+
96105
func fakeReporter(customAppServiceClient appclient.ApplicationClient) *applicationEventReporter {
97106
guestbookApp := &appsv1.Application{
98107
TypeMeta: metav1.TypeMeta{
@@ -159,6 +168,7 @@ func fakeReporter(customAppServiceClient appclient.ApplicationClient) *applicati
159168
appLister,
160169
customAppServiceClient,
161170
metricsServ,
171+
fakeArgoDb(),
162172
}
163173
}
164174

@@ -459,3 +469,20 @@ func TestGetResourceActualState(t *testing.T) {
459469
assert.Equal(t, "", ptr.ToString(res.Manifest))
460470
})
461471
}
472+
473+
func TestGetResourceSourceIdxFromManifestResponse(t *testing.T) {
474+
t.Run("should return correct sourceIdx", func(t *testing.T) {
475+
sourcesManifestsStartingIdx := []int32{0, 5, 15}
476+
desiredManifests := &repoapiclient.ManifestResponse{
477+
SourcesManifestsStartingIdx: sourcesManifestsStartingIdx,
478+
}
479+
480+
assert.Equal(t, int32(0), getResourceSourceIdxFromManifestResponse(0, desiredManifests))
481+
assert.Equal(t, int32(0), getResourceSourceIdxFromManifestResponse(1, desiredManifests))
482+
assert.Equal(t, int32(1), getResourceSourceIdxFromManifestResponse(5, desiredManifests))
483+
assert.Equal(t, int32(1), getResourceSourceIdxFromManifestResponse(6, desiredManifests))
484+
assert.Equal(t, int32(2), getResourceSourceIdxFromManifestResponse(15, desiredManifests))
485+
assert.Equal(t, int32(2), getResourceSourceIdxFromManifestResponse(16, desiredManifests))
486+
assert.Equal(t, int32(-1), getResourceSourceIdxFromManifestResponse(2, &repoapiclient.ManifestResponse{}))
487+
})
488+
}

event_reporter/reporter/event_payload.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func getResourceEventPayload(
103103
DesiredManifest: rr.desiredState.CompiledManifest,
104104
ActualManifest: *rr.actualState.Manifest,
105105
GitManifest: rr.desiredState.RawManifest,
106-
RepoURL: reportedEntityParentApp.app.Status.Sync.ComparedTo.Source.RepoURL,
107106
Path: rr.desiredState.Path,
108107
Revision: utils.GetApplicationLatestRevision(reportedEntityParentApp.app),
109108
OperationSyncRevision: utils.GetOperationRevision(reportedEntityParentApp.app),
@@ -120,22 +119,17 @@ func getResourceEventPayload(
120119
AppInstanceLabelKey: *argoTrackingMetadata.AppInstanceLabelKey,
121120
TrackingMethod: string(*argoTrackingMetadata.TrackingMethod),
122121
AppMultiSourced: reportedEntityParentApp.app.Spec.HasMultipleSources(),
122+
AppSourceIdx: rr.appSourceIdx,
123123
}
124124

125+
source.RepoURL = getResourceSourceRepoUrl(rr, reportedEntityParentApp)
126+
addResourceEventPayloadGitCommitDetails(&source, rr, reportedEntityParentApp)
127+
125128
if reportedEntityParentApp.validatedDestination != nil {
126129
source.DestName = &reportedEntityParentApp.validatedDestination.Name
127130
source.DestServer = reportedEntityParentApp.validatedDestination.Server
128131
}
129132

130-
if reportedEntityParentApp.revisionsMetadata != nil && reportedEntityParentApp.revisionsMetadata.SyncRevisions != nil {
131-
revisionMetadata := getApplicationLegacyRevisionDetails(reportedEntityParentApp.app, reportedEntityParentApp.revisionsMetadata)
132-
if revisionMetadata != nil {
133-
source.CommitMessage = revisionMetadata.Message
134-
source.CommitAuthor = revisionMetadata.Author
135-
source.CommitDate = &revisionMetadata.Date
136-
}
137-
}
138-
139133
if rr.rs.Health != nil {
140134
source.HealthStatus = (*string)(&rr.rs.Health.Status)
141135
source.HealthMessage = &rr.rs.Health.Message
@@ -161,6 +155,43 @@ func getResourceEventPayload(
161155
return &events.Event{Payload: payloadBytes}, nil
162156
}
163157

158+
func getResourceSourceRepoUrl(
159+
rr *ReportedResource,
160+
reportedEntityParentApp *ReportedEntityParentApp,
161+
) string {
162+
specCopy := reportedEntityParentApp.app.Spec.DeepCopy()
163+
164+
specCopy.Sources = reportedEntityParentApp.app.Status.Sync.ComparedTo.Sources
165+
specCopy.Source = reportedEntityParentApp.app.Status.Sync.ComparedTo.Source.DeepCopy()
166+
167+
if specCopy.HasMultipleSources() {
168+
if rr.appSourceIdx == -1 {
169+
return ""
170+
}
171+
return specCopy.GetSourcePtrByIndex(int(rr.appSourceIdx)).RepoURL
172+
}
173+
174+
return specCopy.Source.RepoURL
175+
}
176+
177+
func addResourceEventPayloadGitCommitDetails(
178+
source *events.ObjectSource,
179+
rr *ReportedResource,
180+
reportedEntityParentApp *ReportedEntityParentApp,
181+
) {
182+
if reportedEntityParentApp.revisionsMetadata == nil || reportedEntityParentApp.revisionsMetadata.SyncRevisions == nil || rr.appSourceIdx == -1 {
183+
return
184+
}
185+
186+
syncRevisionWithMetadata := reportedEntityParentApp.revisionsMetadata.GetSyncRevisionAt(int(rr.appSourceIdx))
187+
188+
if syncRevisionWithMetadata != nil && syncRevisionWithMetadata.Metadata != nil {
189+
source.CommitMessage = syncRevisionWithMetadata.Metadata.Message
190+
source.CommitAuthor = syncRevisionWithMetadata.Metadata.Author
191+
source.CommitDate = &syncRevisionWithMetadata.Metadata.Date
192+
}
193+
}
194+
164195
func getResourceEventPayloadErrors(
165196
rr *ReportedResource,
166197
reportedEntityParentApp *ReportedEntityParentApp,

event_reporter/reporter/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type ReportedResource struct {
1111
rs *appv1.ResourceStatus
1212
rsAsAppInfo *ReportedResourceAsApp // passed if resource is application
13+
appSourceIdx int32
1314
actualState *application.ApplicationResourceResponse
1415
desiredState *apiclient.Manifest
1516
manifestGenErr bool
@@ -26,6 +27,7 @@ type ReportedEntityParentApp struct {
2627
appTree *appv1.ApplicationTree
2728
revisionsMetadata *utils.AppSyncRevisionsMetadata
2829
validatedDestination *appv1.ApplicationDestination // with resolved Server url field if server Name used
30+
desiredManifests *apiclient.ManifestResponse
2931
}
3032

3133
type ArgoTrackingMetadata struct {

event_reporter/utils/app_revision.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ type RevisionsData struct {
2525

2626
const annotationRevisionKey = "app.meta.revisions-metadata"
2727

28+
func (asrm *AppSyncRevisionsMetadata) GetSyncRevisionAt(idx int) *RevisionWithMetadata {
29+
if asrm == nil || asrm.SyncRevisions == nil {
30+
return nil
31+
}
32+
return asrm.SyncRevisions[idx]
33+
}
34+
2835
func GetLatestAppHistoryId(a *appv1.Application) int64 {
2936
if lastHistory := getLatestAppHistoryItem(a); lastHistory != nil {
3037
return lastHistory.ID

0 commit comments

Comments
 (0)