From 1563c2db92ceb0f786edbad52666978e519718df Mon Sep 17 00:00:00 2001 From: Israel Blancas Date: Tue, 25 Apr 2023 16:17:13 +0200 Subject: [PATCH] [chore] Ensure the autoscaling version is updated in the copies of autodetect (#1668) --- internal/config/main.go | 45 ++++++++++++++++++++++++++++++------ internal/config/main_test.go | 10 ++++++++ internal/config/options.go | 3 ++- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/internal/config/main.go b/internal/config/main.go index 44468decda..1d529379fb 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -49,7 +49,7 @@ type Config struct { labelsFilter []string openshiftRoutes openshiftRoutesStore autoDetectFrequency time.Duration - autoscalingVersion autodetect.AutoscalingVersion + hpaVersion hpaVersionStore } // New constructs a new configuration based on the given options. @@ -61,8 +61,8 @@ func New(opts ...Option) Config { targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry, logger: logf.Log.WithName("config"), openshiftRoutes: newOpenShiftRoutesWrapper(), + hpaVersion: newHPAVersionWrapper(), version: version.Get(), - autoscalingVersion: autodetect.DefaultAutoscalingVersion, onOpenShiftRoutesChange: newOnChange(), } for _, opt := range opts { @@ -79,13 +79,13 @@ func New(opts ...Option) Config { targetAllocatorConfigMapEntry: o.targetAllocatorConfigMapEntry, logger: o.logger, openshiftRoutes: o.openshiftRoutes, + hpaVersion: o.hpaVersion, onOpenShiftRoutesChange: o.onOpenShiftRoutesChange, autoInstrumentationJavaImage: o.autoInstrumentationJavaImage, autoInstrumentationNodeJSImage: o.autoInstrumentationNodeJSImage, autoInstrumentationPythonImage: o.autoInstrumentationPythonImage, autoInstrumentationDotNetImage: o.autoInstrumentationDotNetImage, labelsFilter: o.labelsFilter, - autoscalingVersion: o.autoscalingVersion, } } @@ -126,12 +126,14 @@ func (c *Config) AutoDetect() error { } } - hpaVersion, err := c.autoDetect.HPAVersion() + hpaV, err := c.autoDetect.HPAVersion() if err != nil { return err } - c.autoscalingVersion = hpaVersion - c.logger.V(2).Info("autoscaling version detected", "autoscaling-version", c.autoscalingVersion.String()) + if c.hpaVersion.Get() != hpaV { + c.logger.V(1).Info("HPA version detected", "version", hpaV) + c.hpaVersion.Set(hpaV) + } return nil } @@ -163,7 +165,7 @@ func (c *Config) OpenShiftRoutes() autodetect.OpenShiftRoutesAvailability { // AutoscalingVersion represents the preferred version of autoscaling. func (c *Config) AutoscalingVersion() autodetect.AutoscalingVersion { - return c.autoscalingVersion + return c.hpaVersion.Get() } // AutoInstrumentationJavaImage returns OpenTelemetry Java auto-instrumentation container image. @@ -197,6 +199,35 @@ func (c *Config) RegisterOpenShiftRoutesChangeCallback(f func() error) { c.onOpenShiftRoutesChange.Register(f) } +type hpaVersionStore interface { + Set(hpaV autodetect.AutoscalingVersion) + Get() autodetect.AutoscalingVersion +} + +func newHPAVersionWrapper() hpaVersionStore { + return &hpaVersionWrapper{ + current: autodetect.AutoscalingVersionUnknown, + } +} + +type hpaVersionWrapper struct { + mu sync.Mutex + current autodetect.AutoscalingVersion +} + +func (p *hpaVersionWrapper) Set(hpaV autodetect.AutoscalingVersion) { + p.mu.Lock() + p.current = hpaV + p.mu.Unlock() +} + +func (p *hpaVersionWrapper) Get() autodetect.AutoscalingVersion { + p.mu.Lock() + hpaV := p.current + p.mu.Unlock() + return hpaV +} + type openshiftRoutesStore interface { Set(ora autodetect.OpenShiftRoutesAvailability) Get() autodetect.OpenShiftRoutesAvailability diff --git a/internal/config/main_test.go b/internal/config/main_test.go index bca0fdb753..f08dc26263 100644 --- a/internal/config/main_test.go +++ b/internal/config/main_test.go @@ -38,6 +38,7 @@ func TestNewConfig(t *testing.T) { assert.Equal(t, "some-image", cfg.CollectorImage()) assert.Equal(t, "some-config.yaml", cfg.CollectorConfigMapEntry()) assert.Equal(t, autodetect.OpenShiftRoutesNotAvailable, cfg.OpenShiftRoutes()) + assert.Equal(t, autodetect.AutoscalingVersionUnknown, cfg.AutoscalingVersion()) } func TestOnPlatformChangeCallback(t *testing.T) { @@ -77,6 +78,10 @@ func TestAutoDetectInBackground(t *testing.T) { wg.Done() return autodetect.OpenShiftRoutesNotAvailable, nil }, + HPAVersionFunc: func() (autodetect.AutoscalingVersion, error) { + wg.Done() + return autodetect.AutoscalingVersionV2, nil + }, } cfg := config.New( config.WithAutoDetect(mock), @@ -85,6 +90,7 @@ func TestAutoDetectInBackground(t *testing.T) { // sanity check require.Equal(t, autodetect.OpenShiftRoutesNotAvailable, cfg.OpenShiftRoutes()) + require.Equal(t, autodetect.AutoscalingVersionUnknown, cfg.AutoscalingVersion()) // test err := cfg.StartAutoDetect() @@ -98,9 +104,13 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil) type mockAutoDetect struct { OpenShiftRoutesAvailabilityFunc func() (autodetect.OpenShiftRoutesAvailability, error) + HPAVersionFunc func() (autodetect.AutoscalingVersion, error) } func (m *mockAutoDetect) HPAVersion() (autodetect.AutoscalingVersion, error) { + if m.HPAVersionFunc != nil { + return m.HPAVersionFunc() + } return autodetect.DefaultAutoscalingVersion, nil } diff --git a/internal/config/options.go b/internal/config/options.go index fecb49bb01..f86cdeb71e 100644 --- a/internal/config/options.go +++ b/internal/config/options.go @@ -44,8 +44,8 @@ type options struct { onOpenShiftRoutesChange changeHandler labelsFilter []string openshiftRoutes openshiftRoutesStore + hpaVersion hpaVersionStore autoDetectFrequency time.Duration - autoscalingVersion autodetect.AutoscalingVersion } func WithAutoDetect(a autodetect.AutoDetect) Option { @@ -88,6 +88,7 @@ func WithLogger(logger logr.Logger) Option { o.logger = logger } } + func WithOnOpenShiftRoutesChangeCallback(f func() error) Option { return func(o *options) { if o.onOpenShiftRoutesChange == nil {