From 969bb8b422113b838eaf3d72bbc88afac969be46 Mon Sep 17 00:00:00 2001 From: Andrew Suderman Date: Thu, 21 Apr 2022 11:31:22 -0600 Subject: [PATCH 1/5] Env Var fixes --- pkg/course/course.go | 32 +++++++++------ pkg/course/course_test.go | 72 +++++++++++++++++++++++++++++++++ pkg/course/testdata/v2_env.yaml | 15 +++++++ 3 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 pkg/course/testdata/v2_env.yaml diff --git a/pkg/course/course.go b/pkg/course/course.go index 52e2cd9d..4b2a3c5d 100644 --- a/pkg/course/course.go +++ b/pkg/course/course.go @@ -61,17 +61,20 @@ type FileV2 struct { // Hooks is a set of scripts to be run before or after the release is installed. Hooks Hooks `yaml:"hooks,omitempty" json:"hooks,omitempty"` // NamespaceMgmt contains the default namespace config for all namespaces managed by this course. - NamespaceMgmt struct { - // Default is the default namespace config for this course - Default *NamespaceConfig `yaml:"default" json:"default"` - } `yaml:"namespace_management,omitempty" json:"namespace_management,omitempty"` - Secrets SecretsList `yaml:"secrets,omitempty" json:"secrets,omitempty"` + NamespaceMgmt NamespaceMgmt `yaml:"namespace_management,omitempty" json:"namespace_management,omitempty"` + Secrets SecretsList `yaml:"secrets,omitempty" json:"secrets,omitempty"` // Releases is the list of releases that should be maintained by this course file. Releases []*Release `yaml:"releases,omitempty" json:"releases,omitempty"` // HelmArgs is a list of arguments to pass to helm commands HelmArgs []string `yaml:"helm_args,omitempty" json:"helm_args,omitempty"` } +type NamespaceMgmt struct { + // Default is the default namespace config for this course + + Default *NamespaceConfig `yaml:"default" json:"default"` +} + // FileV2Unmarshal is a helper type that allows us to have a custom unmarshal function for the FileV2 struct type FileV2Unmarshal FileV2 @@ -95,14 +98,17 @@ type Hooks struct { // NamespaceConfig allows setting namespace annotations and labels type NamespaceConfig struct { - Metadata struct { - Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"` - Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"` - } `yaml:"metadata,omitempty" json:"metadata,omitempty"` - Settings struct { - // Overwrite specifies if these annotations and labels should be overwritten in the event that they already exist. - Overwrite *bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"` - } `yaml:"settings" json:"settings"` + Metadata NSMetadata `yaml:"metadata,omitempty" json:"metadata,omitempty"` + Settings NSSettings `yaml:"settings" json:"settings"` +} + +type NSMetadata struct { + Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"` + Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"` +} + +type NSSettings struct { + Overwrite *bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"` } // Release represents a helm release and all of its configuration diff --git a/pkg/course/course_test.go b/pkg/course/course_test.go index 26493cb3..13d23ef9 100644 --- a/pkg/course/course_test.go +++ b/pkg/course/course_test.go @@ -255,6 +255,16 @@ func Test_parseEnv(t *testing.T) { }, wantErr: false, }, + { + name: "multiple env vars", + data: `$TEST_ENV_KEY some other text $TEST_ENV_KEY2`, + want: "value1 some other text value2", + envMap: map[string]string{ + "TEST_ENV_KEY": "value1", + "TEST_ENV_KEY2": "value2", + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -277,3 +287,65 @@ func Test_boolPtr(t *testing.T) { testBool := true assert.EqualValues(t, &testBool, boolPtr(testBool)) } + +func TestOpenCourseV2(t *testing.T) { + tests := []struct { + name string + fileName string + envMap map[string]string + want *FileV2 + wantErr bool + }{ + { + name: "file not exist", + fileName: "thisfileshouldneverexist", + wantErr: true, + }, + { + name: "test env var with v2 course", + fileName: "testdata/v2_env.yaml", + envMap: map[string]string{ + "EXAMPLE_ENV_VAR": "example-env-value", + "EXAMPLE_ENV_NS": "example-env-ns", + "HELM_REPO_URL": "https://example.com/stable", + }, + want: &FileV2{ + SchemaVersion: "v2", + DefaultRepository: "helm-repo", + DefaultNamespace: "example-env-ns", + Context: "farglebargle", + Repositories: map[string]Repository{ + "helm-repo": { + URL: "https://example.com/stable", + }, + }, + Releases: []*Release{ + { + Name: "basic", + Namespace: "namespace", + Chart: "somechart", + Repository: "helm-repo", + Version: "2.0.0", + Values: map[string]interface{}{ + "envVar": "example-env-value", + }, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for k, v := range tt.envMap { + os.Setenv(k, v) + } + got, err := OpenCourseV2(tt.fileName) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.EqualValues(t, tt.want, got) + } + }) + } +} diff --git a/pkg/course/testdata/v2_env.yaml b/pkg/course/testdata/v2_env.yaml new file mode 100644 index 00000000..b7b9c957 --- /dev/null +++ b/pkg/course/testdata/v2_env.yaml @@ -0,0 +1,15 @@ +schema: v2 +namespace: $EXAMPLE_ENV_NS +repository: helm-repo +context: farglebargle +repositories: + helm-repo: + url: $HELM_REPO_URL +releases: + - name: basic + namespace: namespace + chart: somechart + version: 2.0.0 + repository: helm-repo + values: + envVar: $EXAMPLE_ENV_VAR From c93950059a0bdf94019cfd0f6627cb17ff73f263 Mon Sep 17 00:00:00 2001 From: Andrew Suderman Date: Thu, 21 Apr 2022 11:58:09 -0600 Subject: [PATCH 2/5] recurse better --- pkg/course/course.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/course/course.go b/pkg/course/course.go index 4b2a3c5d..a64e700a 100644 --- a/pkg/course/course.go +++ b/pkg/course/course.go @@ -441,6 +441,15 @@ func decodeYamlWithEnv(value *yaml.Node) error { if v.Kind == yaml.SequenceNode { for i := range v.Content { var err error + + if v.Content[i].Kind != yaml.ScalarNode { + err := decodeYamlWithEnv(v.Content[i]) + if err != nil { + return err + } + continue + } + v.Content[i].Value, err = parseEnv(v.Content[i].Value) if err != nil { return err From 5ef74344ef775a83d639eb94b1bd3ca192872a63 Mon Sep 17 00:00:00 2001 From: Andrew Suderman Date: Thu, 21 Apr 2022 12:43:53 -0600 Subject: [PATCH 3/5] Update pkg/course/course.go --- pkg/course/course.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/course/course.go b/pkg/course/course.go index a64e700a..8651ee8b 100644 --- a/pkg/course/course.go +++ b/pkg/course/course.go @@ -71,7 +71,6 @@ type FileV2 struct { type NamespaceMgmt struct { // Default is the default namespace config for this course - Default *NamespaceConfig `yaml:"default" json:"default"` } From d5fbd1767fca654d703960137f73b99439af0fe4 Mon Sep 17 00:00:00 2001 From: Andrew Suderman Date: Thu, 21 Apr 2022 12:46:28 -0600 Subject: [PATCH 4/5] Update pkg/course/course.go --- pkg/course/course.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/course/course.go b/pkg/course/course.go index 8651ee8b..ad1658b4 100644 --- a/pkg/course/course.go +++ b/pkg/course/course.go @@ -107,6 +107,7 @@ type NSMetadata struct { } type NSSettings struct { + // Overwrite specifies if these annotations and labels should be overwritten in the event that they already exist. Overwrite *bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"` } From 9d880d355bdcc0576f9f21c04ae3415c551b7afc Mon Sep 17 00:00:00 2001 From: Andrew Suderman Date: Thu, 21 Apr 2022 12:57:27 -0600 Subject: [PATCH 5/5] fmt --- pkg/course/course.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/course/course.go b/pkg/course/course.go index ad1658b4..64719305 100644 --- a/pkg/course/course.go +++ b/pkg/course/course.go @@ -107,7 +107,7 @@ type NSMetadata struct { } type NSSettings struct { - // Overwrite specifies if these annotations and labels should be overwritten in the event that they already exist. + // Overwrite specifies if these annotations and labels should be overwritten in the event that they already exist. Overwrite *bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"` }