Skip to content

Commit

Permalink
event-reporter: changes after pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-codefresh committed Nov 13, 2024
1 parent 9f4fa6b commit 4b88afa
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 18 deletions.
1 change: 1 addition & 0 deletions event_reporter/reporter/application_event_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/argoproj/argo-cd/v2/util/db"
"github.com/argoproj/argo-cd/v2/util/settings"

"k8s.io/client-go/kubernetes/fake"

"github.com/aws/smithy-go/ptr"
Expand Down
8 changes: 6 additions & 2 deletions event_reporter/reporter/event_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,14 @@ func getResourceSourceRepoUrl(
specCopy.Source = reportedEntityParentApp.app.Status.Sync.ComparedTo.Source.DeepCopy()

if specCopy.HasMultipleSources() {
if rr.appSourceIdx == -1 {
if !rr.appSourceIdxDetected() {
return ""
}
return specCopy.GetSourcePtrByIndex(int(rr.appSourceIdx)).RepoURL
source := specCopy.GetSourcePtrByIndex(int(rr.appSourceIdx))
if source == nil {
return ""
}
return source.RepoURL
}

return specCopy.Source.RepoURL
Expand Down
4 changes: 4 additions & 0 deletions event_reporter/reporter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ func (rr *ReportedResource) GetApiVersion() string {

return apiVersion
}

func (rr *ReportedResource) appSourceIdxDetected() bool {
return rr.appSourceIdx >= 0
}
63 changes: 63 additions & 0 deletions event_reporter/utils/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package utils

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

type AppRevisionsFieldNames string

var AppRevisionFieldName AppRevisionsFieldNames = "Revision"
var AppRevisionsFieldName AppRevisionsFieldNames = "Revisions"

type AppUtils struct {
App *appv1.Application
}

func (au *AppUtils) operationStateSyncExists(fieldToCheck *AppRevisionsFieldNames) bool {
result := au.App != nil && au.App.Status.OperationState != nil && au.App.Status.OperationState.Operation.Sync != nil
if !result {
return false
}

return revisionsToCheck(RevisionsData{
Revision: au.App.Status.OperationState.Operation.Sync.Revision,
Revisions: au.App.Status.OperationState.Operation.Sync.Revisions,
}, fieldToCheck)
}

func (au *AppUtils) operationSyncExists(fieldToCheck *AppRevisionsFieldNames) bool {
result := au.App != nil && au.App.Operation != nil && au.App.Operation.Sync != nil
if !result {
return false
}

return revisionsToCheck(RevisionsData{
Revision: au.App.Operation.Sync.Revision,
Revisions: au.App.Operation.Sync.Revisions,
}, fieldToCheck)
}

func (au *AppUtils) operationSyncResultExists(fieldToCheck *AppRevisionsFieldNames) bool {
result := au.App != nil && au.App.Status.OperationState != nil && au.App.Status.OperationState.SyncResult != nil
if !result {
return false
}

return revisionsToCheck(RevisionsData{
Revision: au.App.Status.OperationState.SyncResult.Revision,
Revisions: au.App.Status.OperationState.SyncResult.Revisions,
}, fieldToCheck)
}

// expected to return true if fieldToCheck == nil
func revisionsToCheck(obj RevisionsData, fieldToCheck *AppRevisionsFieldNames) bool {
if fieldToCheck == nil {
return true
}
if *fieldToCheck == AppRevisionFieldName {
return obj.Revision != ""
}

if *fieldToCheck == AppRevisionsFieldName {
return obj.Revisions != nil && len(obj.Revisions) > 0
}
return true
}
37 changes: 21 additions & 16 deletions event_reporter/utils/app_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type RevisionsData struct {
const annotationRevisionKey = "app.meta.revisions-metadata"

func (asrm *AppSyncRevisionsMetadata) GetSyncRevisionAt(idx int) *RevisionWithMetadata {
if asrm == nil || asrm.SyncRevisions == nil {
if asrm == nil || asrm.SyncRevisions == nil || idx < 0 || idx >= len(asrm.SyncRevisions) {
return nil
}
return asrm.SyncRevisions[idx]
Expand Down Expand Up @@ -60,12 +60,12 @@ func GetOperationRevision(a *appv1.Application) string {
if a == nil {
return ""
}

au := &AppUtils{App: a}
// this value will be used in case if application hasn't resources , like gitsource
revision := a.Status.Sync.Revision
if a.Status.OperationState != nil && a.Status.OperationState.Operation.Sync != nil && a.Status.OperationState.Operation.Sync.Revision != "" {
if au.operationStateSyncExists(&AppRevisionFieldName) {
revision = a.Status.OperationState.Operation.Sync.Revision
} else if a.Operation != nil && a.Operation.Sync != nil && a.Operation.Sync.Revision != "" {
} else if au.operationSyncExists(&AppRevisionFieldName) {
revision = a.Operation.Sync.Revision
}

Expand All @@ -76,51 +76,55 @@ func GetOperationRevisions(a *appv1.Application) []string {
if a == nil {
return nil
}
au := &AppUtils{App: a}

// this value will be used in case if application hasn't resources , like gitsource
revisions := a.Status.Sync.Revisions
if a.Status.OperationState != nil && a.Status.OperationState.Operation.Sync != nil && a.Status.OperationState.Operation.Sync.Revisions != nil && len(a.Status.OperationState.Operation.Sync.Revisions) > 0 {
if au.operationStateSyncExists(&AppRevisionsFieldName) {
revisions = a.Status.OperationState.Operation.Sync.Revisions
} else if a.Operation != nil && a.Operation.Sync != nil && a.Operation.Sync.Revisions != nil && len(a.Operation.Sync.Revisions) > 0 {
} else if au.operationSyncExists(&AppRevisionsFieldName) {
revisions = a.Operation.Sync.Revisions
}

return revisions
}

func GetOperationSyncResultRevision(a *appv1.Application) *string {
if a == nil || a.Status.OperationState == nil || a.Status.OperationState.SyncResult == nil {
return nil
au := &AppUtils{App: a}
if au.operationSyncResultExists(nil) {
return &a.Status.OperationState.SyncResult.Revision
}

return &a.Status.OperationState.SyncResult.Revision
return nil
}

func GetOperationSyncResultRevisions(a *appv1.Application) *[]string {
if a == nil || a.Status.OperationState == nil || a.Status.OperationState.SyncResult == nil {
return nil
au := &AppUtils{App: a}
if au.operationSyncResultExists(nil) {
return &a.Status.OperationState.SyncResult.Revisions
}

return &a.Status.OperationState.SyncResult.Revisions
return nil
}

func GetOperationSyncRevisions(a *appv1.Application) []string {
if a == nil {
return []string{}
}
au := &AppUtils{App: a}

// 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 {
if au.operationStateSyncExists(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 {
} else if au.operationSyncExists(nil) {
revisions = getRevisions(RevisionsData{
Revision: a.Operation.Sync.Revision,
Revisions: a.Operation.Sync.Revisions,
Expand All @@ -137,16 +141,17 @@ func GetOperationChangeRevisions(a *appv1.Application) []string {
if a == nil {
return revisions
}
au := &AppUtils{App: a}

// this value will be used in case if application hasn't resources, like empty gitsource
if a.Status.OperationState != nil && a.Status.OperationState.Operation.Sync != nil {
if au.operationStateSyncExists(nil) {
if a.Status.OperationState.Operation.Sync.ChangeRevision != "" || a.Status.OperationState.Operation.Sync.ChangeRevisions != nil {
revisions = getRevisions(RevisionsData{
Revision: a.Status.OperationState.Operation.Sync.ChangeRevision,
Revisions: a.Status.OperationState.Operation.Sync.ChangeRevisions,
})
}
} else if a.Operation != nil && a.Operation.Sync != nil {
} else if au.operationSyncExists(nil) {
if a.Operation.Sync.ChangeRevision != "" || a.Operation.Sync.ChangeRevisions != nil {
revisions = getRevisions(RevisionsData{
Revision: a.Operation.Sync.ChangeRevision,
Expand Down
Loading

0 comments on commit 4b88afa

Please sign in to comment.