-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: revisions info reporting with multi-sourced apps support #333
Changes from 13 commits
9ecb8eb
37f5a05
bf0e87a
e757883
1ad1112
3dd3f0d
54258ef
a9c0b2c
7e1f1c2
472aa63
9bc3265
c3becdc
d3ccc7d
006f6db
c6d1667
fb704ae
91272b6
a8cc45e
02c14a3
09fddac
ea8cdce
29db9f8
b395e00
d5c22f3
dc7b414
33e0b97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
### Features | ||
- feat: monorepo controller v1.0.0 | ||
- feat: event-reporter: report change revisions metadata in app annotations |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,82 @@ 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) { | ||
// treats multi-sourced apps as single source and gets first revision details | ||
func getApplicationLegacyRevisionDetails(a *appv1.Application, revisionsMetadata *utils.AppSyncRevisionsMetadata) *appv1.RevisionMetadata { | ||
_, sourceIdx := a.Spec.GetNonRefSource() | ||
|
||
if sourceIdx == -1 { // single source app | ||
sourceIdx = 0 | ||
} | ||
|
||
if revisionsMetadata.SyncRevisions == nil || len(revisionsMetadata.SyncRevisions) == 0 { | ||
return nil | ||
} | ||
|
||
return revisionsMetadata.SyncRevisions[sourceIdx].Metadata | ||
} | ||
|
||
func (s *applicationEventReporter) getRevisionsDetails(ctx context.Context, a *appv1.Application, revisions []string) ([]*utils.RevisionWithMetadata, error) { | ||
project := a.Spec.GetProject() | ||
return s.applicationServiceClient.RevisionMetadata(ctx, &application.RevisionMetadataQuery{ | ||
Name: &a.Name, | ||
AppNamespace: &a.Namespace, | ||
Revision: &revision, | ||
Project: &project, | ||
}) | ||
rms := make([]*utils.RevisionWithMetadata, 0) | ||
|
||
for idx, revision := range revisions { | ||
// report just revision for helm sources | ||
if (a.Spec.HasMultipleSources() && a.Spec.Sources[idx].IsHelm()) || (a.Spec.Source != nil && a.Spec.Source.IsHelm()) { | ||
rms = append(rms, &utils.RevisionWithMetadata{ | ||
Revision: revision, | ||
}) | ||
continue | ||
} | ||
|
||
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, &utils.RevisionWithMetadata{ | ||
Revision: revision, | ||
Metadata: rm, | ||
}) | ||
} | ||
|
||
return rms, nil | ||
} | ||
|
||
func (s *applicationEventReporter) getApplicationRevisionsMetadata(ctx context.Context, logCtx *log.Entry, a *appv1.Application) (*utils.AppSyncRevisionsMetadata, error) { | ||
result := &utils.AppSyncRevisionsMetadata{} | ||
|
||
if a.Status.Sync.Revision != "" || a.Status.Sync.Revisions != nil || (a.Status.History != nil && len(a.Status.History) > 0) { | ||
// can be the latest revision of repository | ||
operationSyncRevisionsMetadata, err := s.getRevisionsDetails(ctx, a, utils.GetOperationSyncRevisions(a)) | ||
if err != nil { | ||
logCtx.WithError(err).Warnf("failed to get application(%s) sync revisions metadata, resuming", a.GetName()) | ||
} | ||
|
||
if err == nil && operationSyncRevisionsMetadata != nil { | ||
result.SyncRevisions = operationSyncRevisionsMetadata | ||
} | ||
// latest revision of repository where changes to app resource were actually made; empty if no changeRevisionі present | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
operationChangeRevisionsMetadata, err := s.getRevisionsDetails(ctx, a, utils.GetOperationChangeRevisions(a)) | ||
if err != nil { | ||
logCtx.WithError(err).Warnf("failed to get application(%s) change revisions metadata, resuming", a.GetName()) | ||
} | ||
|
||
if err == nil && operationChangeRevisionsMetadata != nil { | ||
result.ChangeRevisions = operationChangeRevisionsMetadata | ||
} | ||
} | ||
|
||
return result, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,8 @@ func getResourceEventPayload( | |
manifestGenErr bool, | ||
ts string, | ||
originalApplication *appv1.Application, // passed when rs is application | ||
revisionMetadata *appv1.RevisionMetadata, | ||
originalAppRevisionMetadata *appv1.RevisionMetadata, // passed when rs is application | ||
revisionsMetadata *utils.AppSyncRevisionsMetadata, | ||
originalAppRevisionsMetadata *utils.AppSyncRevisionsMetadata, // passed when rs is application | ||
appInstanceLabelKey string, | ||
trackingMethod appv1.TrackingMethod, | ||
applicationVersions *apiclient.ApplicationVersions, | ||
|
@@ -50,11 +50,15 @@ func getResourceEventPayload( | |
|
||
object := []byte(*actualState.Manifest) | ||
|
||
if originalAppRevisionMetadata != nil && len(object) != 0 { | ||
if originalAppRevisionsMetadata != nil && len(object) != 0 { | ||
actualObject, err := appv1.UnmarshalToUnstructured(*actualState.Manifest) | ||
|
||
if err == nil { | ||
actualObject = utils.AddCommitDetailsToLabels(actualObject, originalAppRevisionMetadata) | ||
actualObject = utils.AddCommitsDetailsToAnnotations(actualObject, originalAppRevisionsMetadata) | ||
if originalApplication != nil { | ||
actualObject = utils.AddCommitDetailsToLabels(actualObject, getApplicationLegacyRevisionDetails(originalApplication, originalAppRevisionsMetadata)) | ||
} | ||
|
||
object, err = actualObject.MarshalJSON() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal unstructured object: %w", err) | ||
|
@@ -74,8 +78,11 @@ func getResourceEventPayload( | |
u.SetKind(rs.Kind) | ||
u.SetName(rs.Name) | ||
u.SetNamespace(rs.Namespace) | ||
if originalAppRevisionMetadata != nil { | ||
u = utils.AddCommitDetailsToLabels(u, originalAppRevisionMetadata) | ||
if originalAppRevisionsMetadata != nil { | ||
u = utils.AddCommitsDetailsToAnnotations(u, originalAppRevisionsMetadata) | ||
if originalApplication != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imho it is redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
u = utils.AddCommitDetailsToLabels(u, getApplicationLegacyRevisionDetails(originalApplication, originalAppRevisionsMetadata)) | ||
} | ||
} | ||
|
||
object, err = u.MarshalJSON() | ||
|
@@ -88,8 +95,11 @@ func getResourceEventPayload( | |
if err != nil { | ||
return nil, fmt.Errorf("failed to add destination namespace to manifest: %w", err) | ||
} | ||
if originalAppRevisionMetadata != nil { | ||
unstructuredWithNamespace = utils.AddCommitDetailsToLabels(unstructuredWithNamespace, originalAppRevisionMetadata) | ||
if originalAppRevisionsMetadata != nil { | ||
unstructuredWithNamespace = utils.AddCommitsDetailsToAnnotations(unstructuredWithNamespace, originalAppRevisionsMetadata) | ||
if originalApplication != nil { | ||
unstructuredWithNamespace = utils.AddCommitDetailsToLabels(unstructuredWithNamespace, getApplicationLegacyRevisionDetails(originalApplication, originalAppRevisionsMetadata)) | ||
} | ||
} | ||
|
||
object, _ = unstructuredWithNamespace.MarshalJSON() | ||
|
@@ -166,10 +176,13 @@ func getResourceEventPayload( | |
TrackingMethod: string(trackingMethod), | ||
} | ||
|
||
if revisionMetadata != nil { | ||
source.CommitMessage = revisionMetadata.Message | ||
source.CommitAuthor = revisionMetadata.Author | ||
source.CommitDate = &revisionMetadata.Date | ||
if revisionsMetadata != nil && revisionsMetadata.SyncRevisions != nil { | ||
revisionMetadata := getApplicationLegacyRevisionDetails(parentApplication, revisionsMetadata) | ||
if revisionMetadata != nil { | ||
source.CommitMessage = revisionMetadata.Message | ||
source.CommitAuthor = revisionMetadata.Author | ||
source.CommitDate = &revisionMetadata.Date | ||
} | ||
} | ||
|
||
if rs.Health != nil { | ||
|
@@ -225,27 +238,18 @@ func (s *applicationEventReporter) getApplicationEventPayload( | |
syncFinished = a.Status.OperationState.FinishedAt | ||
} | ||
|
||
applicationSource := a.Spec.GetSource() | ||
if !applicationSource.IsHelm() && (a.Status.Sync.Revision != "" || (a.Status.History != nil && len(a.Status.History) > 0)) { | ||
revisionMetadata, err := s.getApplicationRevisionDetails(ctx, a, utils.GetOperationRevision(a)) | ||
|
||
if err != nil { | ||
if !strings.Contains(err.Error(), "not found") { | ||
return nil, fmt.Errorf("failed to get revision metadata: %w", err) | ||
} | ||
|
||
logCtx.Warnf("failed to get revision metadata: %s, reporting application deletion event", err.Error()) | ||
} else { | ||
if obj.ObjectMeta.Labels == nil { | ||
obj.ObjectMeta.Labels = map[string]string{} | ||
} | ||
|
||
obj.ObjectMeta.Labels["app.meta.commit-date"] = revisionMetadata.Date.Format("2006-01-02T15:04:05.000Z") | ||
obj.ObjectMeta.Labels["app.meta.commit-author"] = revisionMetadata.Author | ||
obj.ObjectMeta.Labels["app.meta.commit-message"] = revisionMetadata.Message | ||
revisionsMetadata, err := s.getApplicationRevisionsMetadata(ctx, logCtx, a) | ||
if err != nil { | ||
if !strings.Contains(err.Error(), "not found") { | ||
return nil, fmt.Errorf("failed to get revision metadata: %w", err) | ||
} | ||
|
||
logCtx.Warnf("failed to get revision metadata: %s, reporting application deletion event", err.Error()) | ||
} | ||
|
||
utils.AddCommitsDetailsToAppAnnotations(obj, revisionsMetadata) | ||
utils.AddCommitsDetailsToAppLabels(&obj, getApplicationLegacyRevisionDetails(&obj, revisionsMetadata)) | ||
|
||
object, err := json.Marshal(&obj) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal application event") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this condition into dedicated types . Make it a.Spec.IsHelm(idx)