Skip to content

Commit 3e7c741

Browse files
author
Andrew Suderman
authored
Fix env vars in Go (#576)
* Env Var fixes * recurse better * Update pkg/course/course.go * Update pkg/course/course.go * fmt
1 parent a163b28 commit 3e7c741

File tree

3 files changed

+115
-13
lines changed

3 files changed

+115
-13
lines changed

pkg/course/course.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,19 @@ type FileV2 struct {
6161
// Hooks is a set of scripts to be run before or after the release is installed.
6262
Hooks Hooks `yaml:"hooks,omitempty" json:"hooks,omitempty"`
6363
// NamespaceMgmt contains the default namespace config for all namespaces managed by this course.
64-
NamespaceMgmt struct {
65-
// Default is the default namespace config for this course
66-
Default *NamespaceConfig `yaml:"default" json:"default"`
67-
} `yaml:"namespace_management,omitempty" json:"namespace_management,omitempty"`
68-
Secrets SecretsList `yaml:"secrets,omitempty" json:"secrets,omitempty"`
64+
NamespaceMgmt NamespaceMgmt `yaml:"namespace_management,omitempty" json:"namespace_management,omitempty"`
65+
Secrets SecretsList `yaml:"secrets,omitempty" json:"secrets,omitempty"`
6966
// Releases is the list of releases that should be maintained by this course file.
7067
Releases []*Release `yaml:"releases,omitempty" json:"releases,omitempty"`
7168
// HelmArgs is a list of arguments to pass to helm commands
7269
HelmArgs []string `yaml:"helm_args,omitempty" json:"helm_args,omitempty"`
7370
}
7471

72+
type NamespaceMgmt struct {
73+
// Default is the default namespace config for this course
74+
Default *NamespaceConfig `yaml:"default" json:"default"`
75+
}
76+
7577
// FileV2Unmarshal is a helper type that allows us to have a custom unmarshal function for the FileV2 struct
7678
type FileV2Unmarshal FileV2
7779

@@ -95,14 +97,18 @@ type Hooks struct {
9597

9698
// NamespaceConfig allows setting namespace annotations and labels
9799
type NamespaceConfig struct {
98-
Metadata struct {
99-
Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"`
100-
Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
101-
} `yaml:"metadata,omitempty" json:"metadata,omitempty"`
102-
Settings struct {
103-
// Overwrite specifies if these annotations and labels should be overwritten in the event that they already exist.
104-
Overwrite *bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"`
105-
} `yaml:"settings" json:"settings"`
100+
Metadata NSMetadata `yaml:"metadata,omitempty" json:"metadata,omitempty"`
101+
Settings NSSettings `yaml:"settings" json:"settings"`
102+
}
103+
104+
type NSMetadata struct {
105+
Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"`
106+
Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
107+
}
108+
109+
type NSSettings struct {
110+
// Overwrite specifies if these annotations and labels should be overwritten in the event that they already exist.
111+
Overwrite *bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"`
106112
}
107113

108114
// Release represents a helm release and all of its configuration
@@ -435,6 +441,15 @@ func decodeYamlWithEnv(value *yaml.Node) error {
435441
if v.Kind == yaml.SequenceNode {
436442
for i := range v.Content {
437443
var err error
444+
445+
if v.Content[i].Kind != yaml.ScalarNode {
446+
err := decodeYamlWithEnv(v.Content[i])
447+
if err != nil {
448+
return err
449+
}
450+
continue
451+
}
452+
438453
v.Content[i].Value, err = parseEnv(v.Content[i].Value)
439454
if err != nil {
440455
return err

pkg/course/course_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ func Test_parseEnv(t *testing.T) {
255255
},
256256
wantErr: false,
257257
},
258+
{
259+
name: "multiple env vars",
260+
data: `$TEST_ENV_KEY some other text $TEST_ENV_KEY2`,
261+
want: "value1 some other text value2",
262+
envMap: map[string]string{
263+
"TEST_ENV_KEY": "value1",
264+
"TEST_ENV_KEY2": "value2",
265+
},
266+
wantErr: false,
267+
},
258268
}
259269
for _, tt := range tests {
260270
t.Run(tt.name, func(t *testing.T) {
@@ -277,3 +287,65 @@ func Test_boolPtr(t *testing.T) {
277287
testBool := true
278288
assert.EqualValues(t, &testBool, boolPtr(testBool))
279289
}
290+
291+
func TestOpenCourseV2(t *testing.T) {
292+
tests := []struct {
293+
name string
294+
fileName string
295+
envMap map[string]string
296+
want *FileV2
297+
wantErr bool
298+
}{
299+
{
300+
name: "file not exist",
301+
fileName: "thisfileshouldneverexist",
302+
wantErr: true,
303+
},
304+
{
305+
name: "test env var with v2 course",
306+
fileName: "testdata/v2_env.yaml",
307+
envMap: map[string]string{
308+
"EXAMPLE_ENV_VAR": "example-env-value",
309+
"EXAMPLE_ENV_NS": "example-env-ns",
310+
"HELM_REPO_URL": "https://example.com/stable",
311+
},
312+
want: &FileV2{
313+
SchemaVersion: "v2",
314+
DefaultRepository: "helm-repo",
315+
DefaultNamespace: "example-env-ns",
316+
Context: "farglebargle",
317+
Repositories: map[string]Repository{
318+
"helm-repo": {
319+
URL: "https://example.com/stable",
320+
},
321+
},
322+
Releases: []*Release{
323+
{
324+
Name: "basic",
325+
Namespace: "namespace",
326+
Chart: "somechart",
327+
Repository: "helm-repo",
328+
Version: "2.0.0",
329+
Values: map[string]interface{}{
330+
"envVar": "example-env-value",
331+
},
332+
},
333+
},
334+
},
335+
},
336+
}
337+
for _, tt := range tests {
338+
t.Run(tt.name, func(t *testing.T) {
339+
for k, v := range tt.envMap {
340+
os.Setenv(k, v)
341+
}
342+
got, err := OpenCourseV2(tt.fileName)
343+
if tt.wantErr {
344+
assert.Error(t, err)
345+
} else {
346+
assert.NoError(t, err)
347+
assert.EqualValues(t, tt.want, got)
348+
}
349+
})
350+
}
351+
}

pkg/course/testdata/v2_env.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
schema: v2
2+
namespace: $EXAMPLE_ENV_NS
3+
repository: helm-repo
4+
context: farglebargle
5+
repositories:
6+
helm-repo:
7+
url: $HELM_REPO_URL
8+
releases:
9+
- name: basic
10+
namespace: namespace
11+
chart: somechart
12+
version: 2.0.0
13+
repository: helm-repo
14+
values:
15+
envVar: $EXAMPLE_ENV_VAR

0 commit comments

Comments
 (0)