Skip to content

Commit

Permalink
Promote operator.collector.rewritetargetallocator feature flag to sta…
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm authored Mar 29, 2024
1 parent 880441c commit 60d37e1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 170 deletions.
16 changes: 16 additions & 0 deletions .chloggen/promote-rewrite-featuregate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: target allocator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Promote `operator.collector.rewritetargetallocator` feature flag to stable

# One or more tracking issues related to the change
issues: [2796]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
61 changes: 1 addition & 60 deletions internal/manifests/collector/config_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,17 @@ import (
"os"
"testing"

"github.com/prometheus/prometheus/discovery/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
colfeaturegate "go.opentelemetry.io/collector/featuregate"
"gopkg.in/yaml.v2"

ta "github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator/adapters"
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
)

func TestPrometheusParser(t *testing.T) {
param, err := newParams("test/test-img", "testdata/http_sd_config_test.yaml")
assert.NoError(t, err)

t.Run("should update config with http_sd_config", func(t *testing.T) {
err := colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), false)
require.NoError(t, err)
t.Cleanup(func() {
_ = colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), true)
})
actualConfig, err := ReplaceConfig(param.OtelCol)
assert.NoError(t, err)

// prepare
var cfg Config
promCfgMap, err := ta.ConfigToPromConfig(actualConfig)
assert.NoError(t, err)

promCfg, err := yaml.Marshal(promCfgMap)
assert.NoError(t, err)

err = yaml.UnmarshalStrict(promCfg, &cfg)
assert.NoError(t, err)

// test
expectedMap := map[string]bool{
"prometheus": false,
"service-x": false,
}
for _, scrapeConfig := range cfg.PromConfig.ScrapeConfigs {
assert.Len(t, scrapeConfig.ServiceDiscoveryConfigs, 1)
assert.Equal(t, scrapeConfig.ServiceDiscoveryConfigs[0].Name(), "http")
assert.Equal(t, scrapeConfig.ServiceDiscoveryConfigs[0].(*http.SDConfig).URL, "http://test-targetallocator:80/jobs/"+scrapeConfig.JobName+"/targets?collector_id=$POD_NAME")
expectedMap[scrapeConfig.JobName] = true
}
for k := range expectedMap {
assert.True(t, expectedMap[k], k)
}
assert.True(t, cfg.TargetAllocConfig == nil)
})

t.Run("should update config with targetAllocator block if block not present", func(t *testing.T) {
// Set up the test scenario
param.OtelCol.Spec.TargetAllocator.Enabled = true
Expand Down Expand Up @@ -171,26 +131,7 @@ func TestReplaceConfig(t *testing.T) {
assert.YAMLEq(t, expectedConfig, actualConfig)
})

t.Run("should rewrite scrape configs with SD config when TargetAllocator is enabled and feature flag is not set", func(t *testing.T) {
err := colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), false)
require.NoError(t, err)
t.Cleanup(func() {
_ = colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), true)
})

param.OtelCol.Spec.TargetAllocator.Enabled = true

expectedConfigBytes, err := os.ReadFile("testdata/relabel_config_expected_with_sd_config.yaml")
assert.NoError(t, err)
expectedConfig := string(expectedConfigBytes)

actualConfig, err := ReplaceConfig(param.OtelCol)
assert.NoError(t, err)

assert.YAMLEq(t, expectedConfig, actualConfig)
})

t.Run("should remove scrape configs if TargetAllocator is enabled and feature flag is set", func(t *testing.T) {
t.Run("should remove scrape configs if TargetAllocator is enabled", func(t *testing.T) {
param.OtelCol.Spec.TargetAllocator.Enabled = true

expectedConfigBytes, err := os.ReadFile("testdata/config_expected_targetallocator.yaml")
Expand Down
109 changes: 0 additions & 109 deletions internal/manifests/collector/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ package collector
import (
"testing"

colfeaturegate "go.opentelemetry.io/collector/featuregate"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
)

func TestDesiredConfigMap(t *testing.T) {
Expand Down Expand Up @@ -73,111 +69,6 @@ service:
}
})

t.Run("should return expected collector config map with http_sd_config if rewrite flag disabled", func(t *testing.T) {
err := colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), false)
assert.NoError(t, err)
t.Cleanup(func() {
_ = colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), true)
})
expectedLables["app.kubernetes.io/component"] = "opentelemetry-collector"
expectedLables["app.kubernetes.io/name"] = "test-collector"

expectedData := map[string]string{
"collector.yaml": `exporters:
debug:
receivers:
jaeger:
protocols:
grpc: null
prometheus:
config:
scrape_configs:
- http_sd_configs:
- url: http://test-targetallocator:80/jobs/otel-collector/targets?collector_id=$POD_NAME
job_name: otel-collector
scrape_interval: 10s
service:
pipelines:
metrics:
exporters:
- debug
processors: []
receivers:
- prometheus
- jaeger
`,
}

param := deploymentParams()
param.OtelCol.Spec.TargetAllocator.Enabled = true
actual, err := ConfigMap(param)

assert.NoError(t, err)
assert.Equal(t, "test-collector", actual.GetName())
assert.Equal(t, expectedLables, actual.GetLabels())
assert.Equal(t, len(expectedData), len(actual.Data))
for k, expected := range expectedData {
assert.YAMLEq(t, expected, actual.Data[k])
}

})

t.Run("should return expected escaped collector config map with http_sd_config if rewrite flag disabled", func(t *testing.T) {
err := colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), false)
assert.NoError(t, err)
t.Cleanup(func() {
_ = colfeaturegate.GlobalRegistry().Set(featuregate.EnableTargetAllocatorRewrite.ID(), true)
})

expectedLables["app.kubernetes.io/component"] = "opentelemetry-collector"
expectedLables["app.kubernetes.io/name"] = "test-collector"
expectedLables["app.kubernetes.io/version"] = "latest"

expectedData := map[string]string{
"collector.yaml": `exporters:
debug:
receivers:
prometheus:
config:
scrape_configs:
- http_sd_configs:
- url: http://test-targetallocator:80/jobs/serviceMonitor%2Ftest%2Ftest%2F0/targets?collector_id=$POD_NAME
job_name: serviceMonitor/test/test/0
target_allocator:
collector_id: ${POD_NAME}
endpoint: http://test-targetallocator:80
http_sd_config:
refresh_interval: 60s
interval: 30s
service:
pipelines:
metrics:
exporters:
- debug
processors: []
receivers:
- prometheus
`,
}

param, err := newParams("test/test-img", "testdata/http_sd_config_servicemonitor_test_ta_set.yaml")
assert.NoError(t, err)
param.OtelCol.Spec.TargetAllocator.Enabled = true
actual, err := ConfigMap(param)

assert.NoError(t, err)
assert.Equal(t, "test-collector", actual.Name)
assert.Equal(t, expectedLables, actual.Labels)
assert.Equal(t, len(expectedData), len(actual.Data))
for k, expected := range expectedData {
assert.YAMLEq(t, expected, actual.Data[k])
}

// Reset the value
expectedLables["app.kubernetes.io/version"] = "0.47.0"

})

t.Run("should return expected escaped collector config map with target_allocator config block", func(t *testing.T) {
expectedLables["app.kubernetes.io/component"] = "opentelemetry-collector"
expectedLables["app.kubernetes.io/name"] = "test-collector"
Expand Down
3 changes: 2 additions & 1 deletion pkg/featuregate/featuregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ var (
// automatically be rewritten when the target allocator is enabled.
EnableTargetAllocatorRewrite = featuregate.GlobalRegistry().MustRegister(
"operator.collector.rewritetargetallocator",
featuregate.StageBeta,
featuregate.StageStable,
featuregate.WithRegisterDescription("controls whether the operator should configure the collector's targetAllocator configuration"),
featuregate.WithRegisterFromVersion("v0.76.1"),
featuregate.WithRegisterToVersion("v0.98.0"),
)

// PrometheusOperatorIsAvailable is the feature gate that enables features associated to the Prometheus Operator.
Expand Down

0 comments on commit 60d37e1

Please sign in to comment.