Skip to content

Commit ca0b949

Browse files
authored
Opamp Bridge can read env variables on config generation (#2578)
* call os.expandenv in loadfromfile to substitute environment variable values in the yaml config * add tests to confirm headers can be parsed from env variables * undo this change i didnt intend * rm dupe call * add changelog
1 parent ee8db9d commit ca0b949

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed
+16
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: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
5+
component: opamp bridge
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Added env variable parsing to opamp bridge config loading
9+
10+
# One or more tracking issues related to the change
11+
issues: [2577]
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/operator-opamp-bridge/config/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ func LoadFromFile(cfg *Config, configFile string) error {
269269
if err != nil {
270270
return err
271271
}
272-
if err = yaml.Unmarshal(yamlFile, cfg); err != nil {
272+
envExpandedYaml := []byte(os.ExpandEnv(string(yamlFile)))
273+
if err = yaml.Unmarshal(envExpandedYaml, cfg); err != nil {
273274
return fmt.Errorf("error unmarshaling YAML: %w", err)
274275
}
275276
return nil

cmd/operator-opamp-bridge/config/config_test.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package config
1616

1717
import (
1818
"fmt"
19+
"os"
1920
"testing"
2021
"time"
2122

@@ -25,7 +26,8 @@ import (
2526

2627
func TestLoad(t *testing.T) {
2728
type args struct {
28-
file string
29+
file string
30+
envVariables map[string]string
2931
}
3032
tests := []struct {
3133
name string
@@ -136,13 +138,19 @@ func TestLoad(t *testing.T) {
136138
name: "base case with headers",
137139
args: args{
138140
file: "./testdata/agentwithheaders.yaml",
141+
envVariables: map[string]string{
142+
"MY_ENV_VAR_1": "my-env-variable-1-value",
143+
"MY_ENV_VAR_2": "my-env-variable-2-value",
144+
},
139145
},
140146
want: &Config{
141147
RootLogger: logr.Discard(),
142148
Endpoint: "ws://127.0.0.1:4320/v1/opamp",
143149
Headers: map[string]string{
144-
"authentication": "access-12345-token",
145-
"my-header-key": "my-header-value",
150+
"authentication": "access-12345-token",
151+
"my-header-key": "my-header-value",
152+
"my-env-variable-1": "my-env-variable-1-value",
153+
"my-env-variable-2": "my-env-variable-2-value",
146154
},
147155
Capabilities: map[Capability]bool{
148156
AcceptsRemoteConfig: true,
@@ -164,6 +172,12 @@ func TestLoad(t *testing.T) {
164172
}
165173
for _, tt := range tests {
166174
t.Run(tt.name, func(t *testing.T) {
175+
if tt.args.envVariables != nil {
176+
for key, value := range tt.args.envVariables {
177+
err := os.Setenv(key, value)
178+
assert.NoError(t, err)
179+
}
180+
}
167181
got := NewConfig(logr.Discard())
168182
err := LoadFromFile(got, tt.args.file)
169183
if !tt.wantErr(t, err, fmt.Sprintf("Load(%v)", tt.args.file)) {
@@ -173,7 +187,6 @@ func TestLoad(t *testing.T) {
173187
got.ClusterConfig = tt.want.ClusterConfig
174188
got.RootLogger = tt.want.RootLogger
175189
assert.Equalf(t, tt.want, got, "Load(%v)", tt.args.file)
176-
177190
})
178191
}
179192
}

cmd/operator-opamp-bridge/config/testdata/agentwithheaders.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ endpoint: ws://127.0.0.1:4320/v1/opamp
22
headers:
33
authentication: "access-12345-token"
44
my-header-key: "my-header-value"
5+
my-env-variable-1: "${MY_ENV_VAR_1}"
6+
my-env-variable-2: "$MY_ENV_VAR_2"
57
capabilities:
68
AcceptsRemoteConfig: true
79
ReportsEffectiveConfig: true

0 commit comments

Comments
 (0)