From f7cd7a1dd72e3b7b8e9dd07b444db42ee7074293 Mon Sep 17 00:00:00 2001 From: oleksandr-codefresh Date: Fri, 24 Nov 2023 19:45:54 +0200 Subject: [PATCH 1/2] event_reporter: disabling when param value set to 'v1' or empty --- event_reporter/controller/controller.go | 4 +++- event_reporter/server.go | 28 ++++++++++++++++++++++++- util/settings/settings.go | 22 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/event_reporter/controller/controller.go b/event_reporter/controller/controller.go index f9f26a1ecf7ab..048c13217c831 100644 --- a/event_reporter/controller/controller.go +++ b/event_reporter/controller/controller.go @@ -7,8 +7,8 @@ import ( "time" argocommon "github.com/argoproj/argo-cd/v2/common" - "github.com/argoproj/argo-cd/v2/event_reporter/metrics" "github.com/argoproj/argo-cd/v2/event_reporter/codefresh" + "github.com/argoproj/argo-cd/v2/event_reporter/metrics" "github.com/argoproj/argo-cd/v2/event_reporter/reporter" applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -108,6 +108,8 @@ func (c *eventReporterController) Run(ctx context.Context) { defer unsubscribe() for { select { + case <-ctx.Done(): + return case event := <-eventsChannel: logCtx.Infof("channel size is %d", len(eventsChannel)) c.metricsServer.SetQueueSizeCounter(len(eventsChannel)) diff --git a/event_reporter/server.go b/event_reporter/server.go index bf3627d265db4..c381b84daeac8 100644 --- a/event_reporter/server.go +++ b/event_reporter/server.go @@ -148,8 +148,30 @@ func (a *EventReporterServer) Init(ctx context.Context) { } func (a *EventReporterServer) RunController(ctx context.Context) { + running := false + controllerCtx, cancel := context.WithCancel(ctx) controller := event_reporter.NewEventReporterController(a.appInformer, a.Cache, a.settingsMgr, a.ApplicationServiceClient, a.appLister, a.CodefreshConfig, a.serviceSet.MetricsServer) - go controller.Run(ctx) + tick := time.Tick(5 * time.Second) + + for { + select { + case <-tick: + { + rVersion, err := a.settingsMgr.GetCodefreshReporterVersion() + if !running && rVersion == string(settings_util.CodefreshV2ReporterVersion) { + controllerCtx, cancel = context.WithCancel(ctx) + log.Warnf("Reporter parameter (%s) detected - starting controller", rVersion) + go controller.Run(controllerCtx) + running = true + } + if running == true && err == nil && isOldReporterVersion(rVersion) { + log.Warnf("Stopping reporter because version param changed to %s or missing", settings_util.CodefreshV1ReporterVersion) + cancel() + running = false + } + } + } + } } // newHTTPServer returns the HTTP server to serve HTTP/HTTPS requests. This is implemented @@ -229,6 +251,10 @@ func (a *EventReporterServer) Run(ctx context.Context, lns *Listeners) { <-a.stopCh } +func isOldReporterVersion(reporterVersion string) bool { + return reporterVersion == "" || reporterVersion == string(settings_util.CodefreshV1ReporterVersion) +} + // NewServer returns a new instance of the Argo CD API server func NewEventReporterServer(ctx context.Context, opts EventReporterServerOpts) *EventReporterServer { settingsMgr := settings_util.NewSettingsManager(ctx, opts.KubeClientset, opts.Namespace) diff --git a/util/settings/settings.go b/util/settings/settings.go index 7762224f7284d..668ed8799e440 100644 --- a/util/settings/settings.go +++ b/util/settings/settings.go @@ -202,6 +202,14 @@ type KustomizeSettings struct { Versions []KustomizeVersion } +// CodefreshReporterVersion includes all cf reporter versions +type CodefreshReporterVersion string + +const ( + CodefreshV1ReporterVersion CodefreshReporterVersion = "v1" + CodefreshV2ReporterVersion CodefreshReporterVersion = "v2" +) + var ( ByClusterURLIndexer = "byClusterURL" byClusterURLIndexerFunc = func(obj interface{}) ([]string, error) { @@ -416,6 +424,8 @@ const ( settingsWebhookGogsSecretKey = "webhook.gogs.secret" // settingsApplicationInstanceLabelKey is the key to configure injected app instance label key settingsApplicationInstanceLabelKey = "application.instanceLabelKey" + // settingsCodefreshReporterVersion is the key to configure injected app instance label key + settingsCodefreshReporterVersion = "codefresh.reporterVersion" // settingsResourceTrackingMethodKey is the key to configure tracking method for application resources settingsResourceTrackingMethodKey = "application.resourceTrackingMethod" // resourcesCustomizationsKey is the key to the map of resource overrides @@ -720,6 +730,18 @@ func (mgr *SettingsManager) GetAppInstanceLabelKey() (string, error) { return label, nil } +func (mgr *SettingsManager) GetCodefreshReporterVersion() (string, error) { + argoCDCM, err := mgr.getConfigMap() + if err != nil { + return "", err + } + label := argoCDCM.Data[settingsCodefreshReporterVersion] + if label == "" { + return string(CodefreshV1ReporterVersion), nil + } + return label, nil +} + func (mgr *SettingsManager) GetKustomizeSetNamespaceEnabled() bool { argoCDCM, err := mgr.getConfigMap() if err != nil { From f46cff971c8db85627a0131ed05f145c20d3b9d0 Mon Sep 17 00:00:00 2001 From: oleksandr-codefresh Date: Fri, 24 Nov 2023 20:10:01 +0200 Subject: [PATCH 2/2] server: disabling cf reporter when param value set to 'v2' --- server/application/application.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server/application/application.go b/server/application/application.go index 011b09b9e6035..f9f88da76d90b 100644 --- a/server/application/application.go +++ b/server/application/application.go @@ -1121,6 +1121,12 @@ func (s *Server) StartEventSource(es *events.EventSource, stream events.Eventing for { select { case event := <-eventsChannel: + rVersion, _ := s.settingsMgr.GetCodefreshReporterVersion() + if rVersion == string(settings.CodefreshV2ReporterVersion) { + logCtx.Info("v1 reported disabled skipping event") + continue + } + shouldProcess, ignoreResourceCache := s.applicationEventReporter.shouldSendApplicationEvent(event) if !shouldProcess { continue