Skip to content

Commit ca6dd7a

Browse files
paichingerPeter Aichinger
and
Peter Aichinger
authored
Make .Environment values available in .gotmpl files. (roboll#2000)
Currently it's not possible to use `.Environment` values in `*.gomtpl` files. The documentation states the opposite: https://github.com/roboll/helmfile#environment (2nd paragraph). The problem is already described in roboll#1090. This PR fixes this bug. Fixes roboll#1090 Co-authored-by: Peter Aichinger <[email protected]>
1 parent 9354c38 commit ca6dd7a

5 files changed

+30
-16
lines changed

pkg/app/desired_state_file_loader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (ld *desiredStateLoader) Load(f string, opts LoadOpts) (*state.HelmState, e
5252
storage := state.NewStorage(opts.CalleePath, ld.logger, ld.glob)
5353
envld := state.NewEnvironmentValuesLoader(storage, ld.readFile, ld.logger, ld.remote)
5454
handler := state.MissingFileHandlerError
55-
vals, err := envld.LoadEnvironmentValues(&handler, args)
55+
vals, err := envld.LoadEnvironmentValues(&handler, args, &environment.EmptyEnvironment)
5656
if err != nil {
5757
return nil, err
5858
}

pkg/state/create.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (c *StateCreator) LoadEnvValues(target *HelmState, env string, ctxEnv *envi
153153
return nil, &StateLoadError{fmt.Sprintf("failed to read %s", state.FilePath), err}
154154
}
155155

156-
newDefaults, err := state.loadValuesEntries(nil, state.DefaultValues, c.remote)
156+
newDefaults, err := state.loadValuesEntries(nil, state.DefaultValues, c.remote, ctxEnv)
157157
if err != nil {
158158
return nil, err
159159
}
@@ -227,7 +227,7 @@ func (c *StateCreator) loadEnvValues(st *HelmState, name string, failOnMissingEn
227227
envSpec, ok := st.Environments[name]
228228
if ok {
229229
var err error
230-
envVals, err = st.loadValuesEntries(envSpec.MissingFileHandler, envSpec.Values, c.remote)
230+
envVals, err = st.loadValuesEntries(envSpec.MissingFileHandler, envSpec.Values, c.remote, ctxEnv)
231231
if err != nil {
232232
return nil, err
233233
}
@@ -363,13 +363,13 @@ func (c *StateCreator) scatterGatherEnvSecretFiles(st *HelmState, envSecretFiles
363363
return nil
364364
}
365365

366-
func (st *HelmState) loadValuesEntries(missingFileHandler *string, entries []interface{}, remote *remote.Remote) (map[string]interface{}, error) {
366+
func (st *HelmState) loadValuesEntries(missingFileHandler *string, entries []interface{}, remote *remote.Remote, ctxEnv *environment.Environment) (map[string]interface{}, error) {
367367
envVals := map[string]interface{}{}
368368

369369
valuesEntries := append([]interface{}{}, entries...)
370370
ld := NewEnvironmentValuesLoader(st.storage(), st.readFile, st.logger, remote)
371371
var err error
372-
envVals, err = ld.LoadEnvironmentValues(missingFileHandler, valuesEntries)
372+
envVals, err = ld.LoadEnvironmentValues(missingFileHandler, valuesEntries, ctxEnv)
373373
if err != nil {
374374
return nil, err
375375
}

pkg/state/create_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"testing"
99

10+
"github.com/roboll/helmfile/pkg/environment"
1011
"github.com/roboll/helmfile/pkg/remote"
1112

1213
"github.com/roboll/helmfile/pkg/testhelper"
@@ -117,6 +118,7 @@ baz: "{{ readFile \"baz.txt\" }}"`)
117118
barYamlFile := "/example/path/to/bar.yaml.gotmpl"
118119
barYamlContent := []byte(`foo: FOO
119120
bar: {{ readFile "bar.txt" }}
121+
env: {{ .Environment.Name }}
120122
`)
121123

122124
barTextFile := "/example/path/to/bar.txt"
@@ -127,6 +129,7 @@ bar: {{ readFile "bar.txt" }}
127129
"bar": "BAR",
128130
// As the file doesn't have an file extension ".gotmpl", this template expression should not be evaluated
129131
"baz": "{{ readFile \"baz.txt\" }}",
132+
"env": "production",
130133
}
131134

132135
valuesFile := "/example/path/to/values.yaml.gotmpl"
@@ -149,8 +152,11 @@ releaseNamespace: mynamespace
149152
testFs.Cwd = "/example/path/to"
150153

151154
r := remote.NewRemote(logger, testFs.Cwd, testFs.ReadFile, testFs.DirectoryExistsAt, testFs.FileExistsAt)
155+
env := environment.Environment{
156+
Name: "production",
157+
}
152158
state, err := NewCreator(logger, testFs.ReadFile, testFs.FileExists, testFs.Abs, testFs.Glob, testFs.DirectoryExistsAt, nil, nil, "", r).
153-
ParseAndLoad(yamlContent, filepath.Dir(yamlFile), yamlFile, "production", true, nil)
159+
ParseAndLoad(yamlContent, filepath.Dir(yamlFile), yamlFile, "production", true, &env)
154160
if err != nil {
155161
t.Fatalf("unexpected error: %v", err)
156162
}

pkg/state/envvals_loader.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package state
22

33
import (
44
"fmt"
5+
"path/filepath"
6+
57
"github.com/imdario/mergo"
68
"github.com/roboll/helmfile/pkg/environment"
79
"github.com/roboll/helmfile/pkg/maputil"
810
"github.com/roboll/helmfile/pkg/remote"
911
"github.com/roboll/helmfile/pkg/tmpl"
1012
"go.uber.org/zap"
1113
"gopkg.in/yaml.v2"
12-
"path/filepath"
1314
)
1415

1516
type EnvironmentValuesLoader struct {
@@ -31,7 +32,7 @@ func NewEnvironmentValuesLoader(storage *Storage, readFile func(string) ([]byte,
3132
}
3233
}
3334

34-
func (ld *EnvironmentValuesLoader) LoadEnvironmentValues(missingFileHandler *string, valuesEntries []interface{}) (map[string]interface{}, error) {
35+
func (ld *EnvironmentValuesLoader) LoadEnvironmentValues(missingFileHandler *string, valuesEntries []interface{}, ctxEnv *environment.Environment) (map[string]interface{}, error) {
3536
result := map[string]interface{}{}
3637

3738
for _, entry := range valuesEntries {
@@ -54,7 +55,13 @@ func (ld *EnvironmentValuesLoader) LoadEnvironmentValues(missingFileHandler *str
5455
}
5556

5657
for _, f := range files {
57-
tmplData := EnvironmentTemplateData{environment.EmptyEnvironment, "", map[string]interface{}{}}
58+
var env environment.Environment
59+
if ctxEnv == nil {
60+
env = environment.EmptyEnvironment
61+
} else {
62+
env = *ctxEnv
63+
}
64+
tmplData := EnvironmentTemplateData{env, "", map[string]interface{}{}}
5865
r := tmpl.NewFileRenderer(ld.readFile, filepath.Dir(f), tmplData)
5966
bytes, err := r.RenderToBytes(f)
6067
if err != nil {

pkg/state/envvals_loader_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package state
22

33
import (
4-
"github.com/google/go-cmp/cmp"
5-
"github.com/roboll/helmfile/pkg/remote"
6-
"go.uber.org/zap"
74
"io/ioutil"
85
"path/filepath"
96
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/roboll/helmfile/pkg/remote"
10+
"go.uber.org/zap"
1011
)
1112

1213
func newLoader() *EnvironmentValuesLoader {
@@ -34,7 +35,7 @@ func newLoader() *EnvironmentValuesLoader {
3435
func TestEnvValsLoad_SingleValuesFile(t *testing.T) {
3536
l := newLoader()
3637

37-
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.5.yaml"})
38+
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.5.yaml"}, nil)
3839
if err != nil {
3940
t.Fatal(err)
4041
}
@@ -52,7 +53,7 @@ func TestEnvValsLoad_SingleValuesFile(t *testing.T) {
5253
func TestEnvValsLoad_OverwriteNilValue_Issue1150(t *testing.T) {
5354
l := newLoader()
5455

55-
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.1.yaml", "testdata/values.2.yaml"})
56+
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.1.yaml", "testdata/values.2.yaml"}, nil)
5657
if err != nil {
5758
t.Fatal(err)
5859
}
@@ -74,7 +75,7 @@ func TestEnvValsLoad_OverwriteNilValue_Issue1150(t *testing.T) {
7475
func TestEnvValsLoad_OverwriteWithNilValue_Issue1154(t *testing.T) {
7576
l := newLoader()
7677

77-
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.3.yaml", "testdata/values.4.yaml"})
78+
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.3.yaml", "testdata/values.4.yaml"}, nil)
7879
if err != nil {
7980
t.Fatal(err)
8081
}
@@ -97,7 +98,7 @@ func TestEnvValsLoad_OverwriteWithNilValue_Issue1154(t *testing.T) {
9798
func TestEnvValsLoad_OverwriteEmptyValue_Issue1168(t *testing.T) {
9899
l := newLoader()
99100

100-
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/issues/1168/addons.yaml", "testdata/issues/1168/addons2.yaml"})
101+
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/issues/1168/addons.yaml", "testdata/issues/1168/addons2.yaml"}, nil)
101102
if err != nil {
102103
t.Fatal(err)
103104
}

0 commit comments

Comments
 (0)