Skip to content

Commit

Permalink
event-reporter: added utils methods to retrieve revisions metadata fo…
Browse files Browse the repository at this point in the history
…r application
  • Loading branch information
oleksandr-codefresh committed Sep 10, 2024
1 parent 2886cfb commit 9ecb8eb
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
46 changes: 45 additions & 1 deletion event_reporter/reporter/app_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package reporter

import (
"context"

"github.com/argoproj/argo-cd/v2/event_reporter/utils"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
log "github.com/sirupsen/logrus"
)

func (s *applicationEventReporter) getApplicationRevisionDetails(ctx context.Context, a *appv1.Application, revision string) (*appv1.RevisionMetadata, error) {
Expand All @@ -16,3 +17,46 @@ func (s *applicationEventReporter) getApplicationRevisionDetails(ctx context.Con
Project: &project,
})
}

func (s *applicationEventReporter) getCommitRevisionsDetails(ctx context.Context, a *appv1.Application, revisions []string) ([]*appv1.RevisionMetadata, error) {
project := a.Spec.GetProject()
rms := make([]*appv1.RevisionMetadata, 0)

for _, revision := range revisions {
rm, err := s.applicationServiceClient.RevisionMetadata(ctx, &application.RevisionMetadataQuery{
Name: &a.Name,
AppNamespace: &a.Namespace,
Revision: &revision,
Project: &project,
})
if err != nil {
return nil, err
}
rms = append(rms, rm)
}

return rms, nil
}

func (s *applicationEventReporter) getApplicationRevisionsMetadata(ctx context.Context, logCtx *log.Entry, a *appv1.Application) (*utils.AppSyncRevisionsMetadata, error) {
result := &utils.AppSyncRevisionsMetadata{}

// can be the latest revision of repository
operationSyncRevisionsMetadata, err := s.getCommitRevisionsDetails(ctx, a, utils.GetOperationSyncRevisions(a))

if err != nil {
logCtx.WithError(err).Warnf("failed to get application(%s) revisions metadata, resuming", a.GetName())
}

if operationSyncRevisionsMetadata != nil {
result.SyncRevisions = operationSyncRevisionsMetadata
}
// latest revision of repository where changes to app resource were actually made; empty if no changeRevisionі present
operationChangeRevisionsMetadata, err := s.getCommitRevisionsDetails(ctx, a, utils.GetOperationChangeRevisions(a))

if err == nil && operationChangeRevisionsMetadata != nil {
result.ChangeRevisions = operationChangeRevisionsMetadata
}

return result, nil
}
85 changes: 85 additions & 0 deletions event_reporter/utils/app_revision.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package utils

import (
"encoding/json"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)

type AppSyncRevisionsMetadata struct {
SyncRevisions []*appv1.RevisionMetadata `json:"syncRevisions" protobuf:"bytes,1,name=syncRevisions"`
ChangeRevisions []*appv1.RevisionMetadata `json:"changeRevisions" protobuf:"bytes,2,name=changeRevisions"`
}

type RevisionsData struct {
Revision string `json:"revision,omitempty" protobuf:"bytes,1,opt,name=revision"`
Revisions []string `json:"revisions,omitempty" protobuf:"bytes,2,opt,name=revisions"`
}

func GetLatestAppHistoryId(a *appv1.Application) int64 {
if lastHistory := getLatestAppHistoryItem(a); lastHistory != nil {
return lastHistory.ID
Expand Down Expand Up @@ -46,6 +58,59 @@ func GetOperationRevision(a *appv1.Application) string {
return revision
}

func GetOperationSyncRevisions(a *appv1.Application) []string {
var revisions []string
if a != nil {
// this value will be used in case if application hasn't resources, like empty gitsource
revisions = getRevisions(RevisionsData{
Revision: a.Status.Sync.Revision,
Revisions: a.Status.Sync.Revisions,
})

if a.Status.OperationState != nil && a.Status.OperationState.Operation.Sync != nil {
revisions = getRevisions(RevisionsData{
Revision: a.Status.OperationState.Operation.Sync.Revision,
Revisions: a.Status.OperationState.Operation.Sync.Revisions,
})
} else if a.Operation != nil && a.Operation.Sync != nil {
revisions = getRevisions(RevisionsData{
Revision: a.Operation.Sync.Revision,
Revisions: a.Operation.Sync.Revisions,
})
}
}

return revisions
}

// for monorepo support: list with revisions where actual changes to source directory were committed
func GetOperationChangeRevisions(a *appv1.Application) []string {
revisions := []string{}
if a != nil {
// this value will be used in case if application hasn't resources, like empty gitsource
// TODO uncomment later
//if a.Status.OperationState != nil && a.Status.OperationState.Operation.Sync != nil {
// if a.Status.OperationState.Operation.Sync.ChangeRevision != "" {
// revisions = []string{a.Status.OperationState.Operation.Sync.ChangeRevision}
// }
//} else if a.Operation != nil && a.Operation.Sync != nil {
// if a.Operation.Sync.ChangeRevision != "" {
// revisions = []string{a.Operation.Sync.ChangeRevision}
// }
//}
}

return revisions
}

func getRevisions(rd RevisionsData) []string {
if rd.Revisions != nil {
return rd.Revisions
}

return []string{rd.Revision}
}

func AddCommitDetailsToLabels(u *unstructured.Unstructured, revisionMetadata *appv1.RevisionMetadata) *unstructured.Unstructured {
if revisionMetadata == nil || u == nil {
return u
Expand All @@ -61,3 +126,23 @@ func AddCommitDetailsToLabels(u *unstructured.Unstructured, revisionMetadata *ap

return u
}

func AddCommitsDetailsToAnnotations(unstrApp *unstructured.Unstructured, revisionsMetadata *AppSyncRevisionsMetadata) *unstructured.Unstructured {
if revisionsMetadata == nil || unstrApp == nil {
return unstrApp
}

if field, _, _ := unstructured.NestedFieldCopy(unstrApp.Object, "metadata", "annotations"); field == nil {
_ = unstructured.SetNestedStringMap(unstrApp.Object, map[string]string{}, "metadata", "annotations")
}

jsonRevisionsMetadata, err := json.Marshal(revisionsMetadata)

if err != nil {
return unstrApp
}

_ = unstructured.SetNestedField(unstrApp.Object, jsonRevisionsMetadata, "metadata", "annotations", "app.meta.revisions-metadata")

return unstrApp
}

0 comments on commit 9ecb8eb

Please sign in to comment.