Skip to content

Commit

Permalink
[chore] Ensure the autoscaling version is updated in the copies of au…
Browse files Browse the repository at this point in the history
…todetect (open-telemetry#1668)
  • Loading branch information
iblancasa authored Apr 25, 2023
1 parent 8f3dd56 commit 782b710
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
45 changes: 38 additions & 7 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions internal/config/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand All @@ -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()
Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 782b710

Please sign in to comment.