Skip to content

Commit

Permalink
feat/reporting-v2-switching (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-codefresh authored Nov 25, 2023
1 parent 6c2b4d6 commit 41777a6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 3 additions & 1 deletion event_reporter/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
28 changes: 27 additions & 1 deletion event_reporter/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions util/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 41777a6

Please sign in to comment.