Skip to content

Commit 22b5fd6

Browse files
[targetallocator] collector namespace overridden by env var (#3976)
* [targetallocator] do not override the config file value of collector namespace if env var is not set * add test * fix use of os.Setenv in tests --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 261aea6 commit 22b5fd6

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

.chloggen/override_with_env.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: targetallocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Do not override the collector namespace from the config file if the environment variable is not set.
9+
10+
# One or more tracking issues related to the change
11+
issues: [3976]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

cmd/otel-allocator/internal/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
301301

302302
// LoadFromEnv loads configuration from environment variables.
303303
func LoadFromEnv(target *Config) error {
304-
target.CollectorNamespace = os.Getenv("OTELCOL_NAMESPACE")
304+
if ns, ok := os.LookupEnv("OTELCOL_NAMESPACE"); ok {
305+
target.CollectorNamespace = ns
306+
}
305307
return nil
306308
}
307309

cmd/otel-allocator/internal/config/config_test.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,8 @@ func TestLoadFromFile(t *testing.T) {
501501
}
502502

503503
func TestLoadFromEnv(t *testing.T) {
504-
current := os.Getenv("OTELCOL_NAMESPACE")
505-
t.Cleanup(func() {
506-
err := os.Setenv("OTELCOL_NAMESPACE", current)
507-
assert.NoError(t, err)
508-
})
509504
namespace := "default"
510-
os.Setenv("OTELCOL_NAMESPACE", namespace)
505+
t.Setenv("OTELCOL_NAMESPACE", namespace)
511506
cfg := &Config{}
512507
err := LoadFromEnv(cfg)
513508
require.NoError(t, err)
@@ -767,6 +762,7 @@ kube_config_file_path: "/config/kube.config"
767762
kubeConfigPath := createDummyKubeConfig(t, tempDir)
768763

769764
configContent := `
765+
collector_namespace: config-file-namespace
770766
listen_addr: "` + configListenAddr + `"
771767
prometheus_cr:
772768
enabled: true
@@ -794,6 +790,7 @@ kube_config_file_path: "` + kubeConfigPath + `"
794790
assert.True(t, config.HTTPS.Enabled, "Config file should override defaults for HTTPS enabled")
795791
assert.Equal(t, ":7443", config.HTTPS.ListenAddr, "Config file should override defaults for HTTPS listen address")
796792
assert.Equal(t, kubeConfigPath, config.KubeConfigFilePath, "Config file should set kube config path")
793+
assert.Equal(t, "config-file-namespace", config.CollectorNamespace, "Config file should set collector namespace")
797794
})
798795

799796
t.Run("environment variables are applied", func(t *testing.T) {
@@ -805,17 +802,9 @@ kube_config_file_path: "` + kubeConfigPath + `"
805802

806803
kubeConfigPath := createDummyKubeConfig(t, tempDir)
807804

808-
// Save original env var value and restore after test
809-
originalNamespace := os.Getenv("OTELCOL_NAMESPACE")
810-
defer func() {
811-
envErr := os.Setenv("OTELCOL_NAMESPACE", originalNamespace)
812-
assert.NoError(t, envErr)
813-
}()
814-
815805
// Set environment variable
816806
testNamespace := "test-namespace"
817-
err = os.Setenv("OTELCOL_NAMESPACE", testNamespace)
818-
require.NoError(t, err)
807+
t.Setenv("OTELCOL_NAMESPACE", testNamespace)
819808

820809
// Prepare args for Load function
821810
args := []string{
@@ -848,17 +837,9 @@ kube_config_file_path: "` + kubeConfigPath + `"
848837
err := os.WriteFile(configPath, []byte(configContent), 0600)
849838
require.NoError(t, err)
850839

851-
// Save original env var value and restore after test
852-
originalNamespace := os.Getenv("OTELCOL_NAMESPACE")
853-
defer func() {
854-
envErr := os.Setenv("OTELCOL_NAMESPACE", originalNamespace)
855-
assert.NoError(t, envErr)
856-
}()
857-
858840
// Environment variable sets value
859841
testNamespace := "env-var-namespace"
860-
err = os.Setenv("OTELCOL_NAMESPACE", testNamespace)
861-
require.NoError(t, err)
842+
t.Setenv("OTELCOL_NAMESPACE", testNamespace)
862843

863844
// Prepare args for Load function with CLI values
864845
cliListenAddr := ":8888"

0 commit comments

Comments
 (0)