Skip to content

Commit 4dbe2c4

Browse files
feat: reporter uses sources-server (#357)
1 parent 21ccd78 commit 4dbe2c4

File tree

12 files changed

+191
-13
lines changed

12 files changed

+191
-13
lines changed

changelog/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
### Features
2-
- feat(event-reporter): multisourced apps support improvements: reporting syncOperationRevisions, detecting correct resource sourceIdx, reporting correct git commit info
2+
- feat(event-reporter): using sources-server for getting application version

cmd/event-reporter-server/commands/event_reporter_server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"math"
77
"time"
88

9+
"github.com/argoproj/argo-cd/v2/pkg/sources_server_client"
10+
911
"github.com/argoproj/argo-cd/v2/event_reporter/reporter"
1012

1113
"github.com/argoproj/argo-cd/v2/event_reporter"
@@ -104,6 +106,8 @@ func NewCommand() *cobra.Command {
104106
rateLimiterBucketSize int
105107
rateLimiterDuration time.Duration
106108
rateLimiterLearningMode bool
109+
useSourcesServer bool
110+
sourcesServerBaseURL string
107111
)
108112
command := &cobra.Command{
109113
Use: cliName,
@@ -192,6 +196,10 @@ func NewCommand() *cobra.Command {
192196
Capacity: rateLimiterBucketSize,
193197
LearningMode: rateLimiterLearningMode,
194198
},
199+
UseSourcesServer: useSourcesServer,
200+
SourcesServerConfig: &sources_server_client.SourcesServerConfig{
201+
BaseURL: sourcesServerBaseURL,
202+
},
195203
}
196204

197205
log.Infof("Starting event reporter server with grpc transport %v", useGrpc)
@@ -245,6 +253,8 @@ func NewCommand() *cobra.Command {
245253
command.Flags().IntVar(&rateLimiterBucketSize, "rate-limiter-bucket-size", env.ParseNumFromEnv("RATE_LIMITER_BUCKET_SIZE", math.MaxInt, 0, math.MaxInt), "The maximum amount of requests allowed per window.")
246254
command.Flags().DurationVar(&rateLimiterDuration, "rate-limiter-period", env.ParseDurationFromEnv("RATE_LIMITER_DURATION", 24*time.Hour, 0, math.MaxInt64), "The rate limit window size.")
247255
command.Flags().BoolVar(&rateLimiterLearningMode, "rate-limiter-learning-mode", env.ParseBoolFromEnv("RATE_LIMITER_LEARNING_MODE_ENABLED", false), "The rate limit enabled in learning mode ( not blocking sending to queue but logging it )")
256+
command.Flags().BoolVar(&useSourcesServer, "use-sources-server", env.ParseBoolFromEnv("SOURCES_SERVER_ENABLED", false), "Use sources-server instead of repo-server fork")
257+
command.Flags().StringVar(&sourcesServerBaseURL, "sources-server-base-url", env.StringFromEnv("SOURCES_SERVER_BASE_URL", common.DefaultSourcesServerAddr), "Sources-server base URL")
248258
cacheSrc = servercache.AddCacheFlagsToCmd(command, cacheutil.Options{
249259
OnClientCreated: func(client *redis.Client) {
250260
redisClient = client

common/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const (
2525
// DefaultDexServerAddr is the HTTP address of the Dex OIDC server, which we run a reverse proxy against
2626
DefaultDexServerAddr = "argocd-dex-server:5556"
2727
// DefaultRedisAddr is the default redis address
28-
DefaultRedisAddr = "argocd-redis:6379"
28+
DefaultRedisAddr = "argocd-redis:6379"
29+
DefaultSourcesServerAddr = "sources-server:8090"
2930
)
3031

3132
// Kubernetes ConfigMap and Secret resource names which hold Argo CD settings

event_reporter/controller/controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/argoproj/argo-cd/v2/pkg/sources_server_client"
10+
911
"github.com/argoproj/argo-cd/v2/util/db"
1012

1113
appclient "github.com/argoproj/argo-cd/v2/event_reporter/application"
@@ -45,15 +47,15 @@ type eventReporterController struct {
4547
metricsServer *metrics.MetricsServer
4648
}
4749

48-
func NewEventReporterController(appInformer cache.SharedIndexInformer, cache *servercache.Cache, settingsMgr *settings.SettingsManager, applicationServiceClient appclient.ApplicationClient, appLister applisters.ApplicationLister, codefreshConfig *codefresh.CodefreshConfig, metricsServer *metrics.MetricsServer, featureManager *reporter.FeatureManager, rateLimiterOpts *reporter.RateLimiterOpts, db db.ArgoDB) EventReporterController {
50+
func NewEventReporterController(appInformer cache.SharedIndexInformer, cache *servercache.Cache, settingsMgr *settings.SettingsManager, applicationServiceClient appclient.ApplicationClient, appLister applisters.ApplicationLister, codefreshConfig *codefresh.CodefreshConfig, metricsServer *metrics.MetricsServer, featureManager *reporter.FeatureManager, rateLimiterOpts *reporter.RateLimiterOpts, db db.ArgoDB, useSourcesServer bool, sourcesServerConfig *sources_server_client.SourcesServerConfig) EventReporterController {
4951
appBroadcaster := reporter.NewBroadcaster(featureManager, metricsServer, rateLimiterOpts)
5052
_, err := appInformer.AddEventHandler(appBroadcaster)
5153
if err != nil {
5254
log.Error(err)
5355
}
5456
return &eventReporterController{
5557
appBroadcaster: appBroadcaster,
56-
applicationEventReporter: reporter.NewApplicationEventReporter(cache, applicationServiceClient, appLister, codefreshConfig, metricsServer, db),
58+
applicationEventReporter: reporter.NewApplicationEventReporter(cache, applicationServiceClient, appLister, codefreshConfig, metricsServer, db, useSourcesServer, sourcesServerConfig),
5759
cache: cache,
5860
settingsMgr: settingsMgr,
5961
applicationServiceClient: applicationServiceClient,

event_reporter/reporter/app_revision_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func TestGetRevisionsDetails(t *testing.T) {
5656
&metrics.MetricsServer{},
5757
fakeArgoDb(),
5858
"0.0.1",
59+
false,
60+
nil,
5961
}
6062

6163
result, _ := reporter.getRevisionsDetails(context.Background(), &app, []string{expectedRevision})
@@ -122,6 +124,8 @@ func TestGetRevisionsDetails(t *testing.T) {
122124
&metrics.MetricsServer{},
123125
fakeArgoDb(),
124126
"0.0.1",
127+
false,
128+
nil,
125129
}
126130

127131
result, _ := reporter.getRevisionsDetails(context.Background(), &app, []string{expectedRevision1, expectedRevision2})
@@ -165,6 +169,8 @@ func TestGetRevisionsDetails(t *testing.T) {
165169
&metrics.MetricsServer{},
166170
fakeArgoDb(),
167171
"0.0.1",
172+
false,
173+
nil,
168174
}
169175

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

event_reporter/reporter/application_event_reporter.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/argoproj/argo-cd/v2/pkg/sources_server_client"
13+
1214
"github.com/argoproj/argo-cd/v2/util/db"
1315

1416
"github.com/argoproj/argo-cd/v2/event_reporter/utils"
@@ -46,6 +48,8 @@ type applicationEventReporter struct {
4648
metricsServer *metrics.MetricsServer
4749
db db.ArgoDB
4850
runtimeVersion string
51+
useSourcesServer bool
52+
sourcesServerClient sources_server_client.SourceServerClientInteface
4953
}
5054

5155
type ApplicationEventReporter interface {
@@ -59,7 +63,7 @@ type ApplicationEventReporter interface {
5963
ShouldSendApplicationEvent(ae *appv1.ApplicationWatchEvent) (shouldSend bool, syncStatusChanged bool)
6064
}
6165

62-
func NewApplicationEventReporter(cache *servercache.Cache, applicationServiceClient appclient.ApplicationClient, appLister applisters.ApplicationLister, codefreshConfig *codefresh.CodefreshConfig, metricsServer *metrics.MetricsServer, db db.ArgoDB) ApplicationEventReporter {
66+
func NewApplicationEventReporter(cache *servercache.Cache, applicationServiceClient appclient.ApplicationClient, appLister applisters.ApplicationLister, codefreshConfig *codefresh.CodefreshConfig, metricsServer *metrics.MetricsServer, db db.ArgoDB, useSourcesServer bool, sourcesServerConfig *sources_server_client.SourcesServerConfig) ApplicationEventReporter {
6367
return &applicationEventReporter{
6468
cache: cache,
6569
applicationServiceClient: applicationServiceClient,
@@ -68,6 +72,8 @@ func NewApplicationEventReporter(cache *servercache.Cache, applicationServiceCli
6872
metricsServer: metricsServer,
6973
db: db,
7074
runtimeVersion: codefreshConfig.RuntimeVersion,
75+
useSourcesServer: useSourcesServer,
76+
sourcesServerClient: sources_server_client.NewSourceServerClient(sourcesServerConfig),
7177
}
7278
}
7379

@@ -265,7 +271,20 @@ func (s *applicationEventReporter) resolveApplicationVersions(ctx context.Contex
265271
}
266272

267273
syncManifests, _ := s.getDesiredManifests(ctx, logCtx, a, nil, &sourcePositions, syncResultRevisions)
268-
return syncManifests.GetApplicationVersions()
274+
275+
var applicationVersions *apiclient.ApplicationVersions
276+
if s.useSourcesServer {
277+
log.Infof("cfGetAppVersion. Getting version from sourcesserver")
278+
if len(*syncResultRevisions) == 0 {
279+
return nil
280+
}
281+
appVers := s.sourcesServerClient.GetAppVersion(a, &(*syncResultRevisions)[0])
282+
applicationVersions = utils.SourcesAppVersionsToRepo(appVers, logCtx)
283+
} else {
284+
applicationVersions = syncManifests.GetApplicationVersions()
285+
}
286+
287+
return applicationVersions
269288
}
270289

271290
syncResultRevision := utils.GetOperationSyncResultRevision(a)
@@ -275,7 +294,16 @@ func (s *applicationEventReporter) resolveApplicationVersions(ctx context.Contex
275294
}
276295

277296
syncManifests, _ := s.getDesiredManifests(ctx, logCtx, a, syncResultRevision, nil, nil)
278-
return syncManifests.GetApplicationVersions()
297+
298+
var applicationVersions *apiclient.ApplicationVersions
299+
if s.useSourcesServer {
300+
log.Infof("cfGetAppVersion. Getting version from sourcesserver")
301+
appVers := s.sourcesServerClient.GetAppVersion(a, syncResultRevision)
302+
applicationVersions = utils.SourcesAppVersionsToRepo(appVers, logCtx)
303+
} else {
304+
applicationVersions = syncManifests.GetApplicationVersions()
305+
}
306+
return applicationVersions
279307
}
280308

281309
func (s *applicationEventReporter) getAppForResourceReporting(
@@ -309,8 +337,8 @@ func (s *applicationEventReporter) processResource(
309337
appEventProcessingStartedAt string,
310338
desiredManifests *apiclient.ManifestResponse,
311339
manifestGenErr bool,
312-
originalApplication *appv1.Application, // passed onlu if resource is app
313-
applicationVersions *apiclient.ApplicationVersions, // passed onlu if resource is app
340+
originalApplication *appv1.Application, // passed only if resource is app
341+
applicationVersions *apiclient.ApplicationVersions, // passed only if resource is app
314342
reportedEntityParentApp *ReportedEntityParentApp,
315343
argoTrackingMetadata *ArgoTrackingMetadata,
316344
) error {

event_reporter/reporter/application_event_reporter_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ func fakeReporter(customAppServiceClient appclient.ApplicationClient) *applicati
173173
metricsServ,
174174
fakeArgoDb(),
175175
"0.0.1",
176+
false,
177+
nil,
176178
}
177179
}
178180

event_reporter/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/argoproj/argo-cd/v2/pkg/sources_server_client"
14+
1315
appclient "github.com/argoproj/argo-cd/v2/event_reporter/application"
1416
"github.com/argoproj/argo-cd/v2/event_reporter/reporter"
1517

@@ -95,6 +97,8 @@ type EventReporterServerOpts struct {
9597
RootPath string
9698
CodefreshConfig *codefresh.CodefreshConfig
9799
RateLimiterOpts *reporter.RateLimiterOpts
100+
UseSourcesServer bool
101+
SourcesServerConfig *sources_server_client.SourcesServerConfig
98102
}
99103

100104
type handlerSwitcher struct {
@@ -153,7 +157,7 @@ func (a *EventReporterServer) Init(ctx context.Context) {
153157
}
154158

155159
func (a *EventReporterServer) RunController(ctx context.Context) {
156-
controller := event_reporter.NewEventReporterController(a.appInformer, a.Cache, a.settingsMgr, a.ApplicationServiceClient, a.appLister, a.CodefreshConfig, a.serviceSet.MetricsServer, a.featureManager, a.RateLimiterOpts, a.db)
160+
controller := event_reporter.NewEventReporterController(a.appInformer, a.Cache, a.settingsMgr, a.ApplicationServiceClient, a.appLister, a.CodefreshConfig, a.serviceSet.MetricsServer, a.featureManager, a.RateLimiterOpts, a.db, a.UseSourcesServer, a.SourcesServerConfig)
157161
go controller.Run(ctx)
158162
}
159163

event_reporter/utils/app_version.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package utils
33
import (
44
"encoding/json"
55

6+
log "github.com/sirupsen/logrus"
7+
68
"github.com/argoproj/argo-cd/v2/pkg/apiclient/events"
9+
"github.com/argoproj/argo-cd/v2/pkg/sources_server_client"
710
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
811
)
912

@@ -16,3 +19,17 @@ func RepoAppVersionsToEvent(applicationVersions *apiclient.ApplicationVersions)
1619
}
1720
return applicationVersionsEvents, nil
1821
}
22+
23+
func SourcesAppVersionsToRepo(applicationVersions *sources_server_client.AppVersionResult, logCtx *log.Entry) *apiclient.ApplicationVersions {
24+
if applicationVersions == nil {
25+
return nil
26+
}
27+
applicationVersionsRepo := &apiclient.ApplicationVersions{}
28+
applicationVersionsData, _ := json.Marshal(applicationVersions)
29+
err := json.Unmarshal(applicationVersionsData, applicationVersionsRepo)
30+
if err != nil {
31+
logCtx.Errorf("can't unmarshal app version: %v", err)
32+
return nil
33+
}
34+
return applicationVersionsRepo
35+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ require (
190190
github.com/davecgh/go-spew v1.1.1 // indirect
191191
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
192192
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
193-
github.com/dlclark/regexp2 v1.11.2
193+
github.com/dlclark/regexp2 v1.11.4
194194
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
195195
github.com/emirpasic/gods v1.18.1 // indirect
196196
github.com/evanphx/json-patch/v5 v5.8.0 // indirect

0 commit comments

Comments
 (0)