8000 Make .Environment values available in .gotmpl files. by paichinger · Pull Request #2000 · roboll/helmfile · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make .Environment values available in .gotmpl files. #2000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of serv 8000 ice and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/app/desired_state_file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (ld *desiredStateLoader) Load(f string, opts LoadOpts) (*state.HelmState, e
storage := state.NewStorage(opts.CalleePath, ld.logger, ld.glob)
envld := state.NewEnvironmentValuesLoader(storage, ld.readFile, ld.logger, ld.remote)
handler := state.MissingFileHandlerError
vals, err := envld.LoadEnvironmentValues(&handler, args)
vals, err := envld.LoadEnvironmentValues(&handler, args, &environment.EmptyEnvironment)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/state/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (c *StateCreator) LoadEnvValues(target *HelmState, env string, ctxEnv *envi
return nil, &StateLoadError{fmt.Sprintf("failed to read %s", state.FilePath), err}
}

newDefaults, err := state.loadValuesEntries(nil, state.DefaultValues, c.remote)
newDefaults, err := state.loadValuesEntries(nil, state.DefaultValues, c.remote, ctxEnv)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *StateCreator) loadEnvValues(st *HelmState, name string, failOnMissingEn
envSpec, ok := st.Environments[name]
if ok {
var err error
envVals, err = st.loadValuesEntries(envSpec.MissingFileHandler, envSpec.Values, c.remote)
envVals, err = st.loadValuesEntries(envSpec.MissingFileHandler, envSpec.Values, c.remote, ctxEnv)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -363,13 +363,13 @@ func (c *StateCreator) scatterGatherEnvSecretFiles(st *HelmState, envSecretFiles
return nil
}

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

valuesEntries := append([]interface{}{}, entries...)
ld := NewEnvironmentValuesLoader(st.storage(), st.readFile, st.logger, remote)
var err error
envVals, err = ld.LoadEnvironmentValues(missingFileHandler, valuesEntries)
envVals, err = ld.LoadEnvironmentValues(missingFileHandler, valuesEntries, ctxEnv)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/state/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"testing"

"github.com/roboll/helmfile/pkg/environment"
"github.com/roboll/helmfile/pkg/remote"

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

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

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

r := remote.NewRemote(logger, testFs.Cwd, testFs.ReadFile, testFs.DirectoryExistsAt, testFs.FileExistsAt)
env := environment.Environment{
Name: "production",
}
state, err := NewCreator(logger, testFs.ReadFile, testFs.FileExists, testFs.Abs, testFs.Glob, testFs.DirectoryExistsAt, nil, nil, "", r).
ParseAndLoad(yamlContent, filepath.Dir(yamlFile), yamlFile, "production", true, nil)
ParseAndLoad(yamlContent, filepath.Dir(yamlFile), yamlFile, "production", true, &env)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/state/envvals_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package state

import (
"fmt"
"path/filepath"

< 8000 /td>
"github.com/imdario/mergo"
"github.com/roboll/helmfile/pkg/environment"
"github.com/roboll/helmfile/pkg/maputil"
"github.com/roboll/helmfile/pkg/remote"
"github.com/roboll/helmfile/pkg/tmpl"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"path/filepath"
)

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

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

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

for _, f := range files {
tmplData := EnvironmentTemplateData{environment.EmptyEnvironment, "", map[string]interface{}{}}
var env environment.Environment
if ctxEnv == nil {
env = environment.EmptyEnvironment
} else {
env = *ctxEnv
}
tmplData := EnvironmentTemplateData{env, "", map[string]interface{}{}}
r := tmpl.NewFileRenderer(ld.readFile, filepath.Dir(f), tmplData)
bytes, err := r.RenderToBytes(f)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions pkg/state/envvals_loader_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package state

import (
"github.com/google/go-cmp/cmp"
"github.com/roboll/helmfile/pkg/remote"
"go.uber.org/zap"
"io/ioutil"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/roboll/helmfile/pkg/remote"
"go.uber.org/zap"
)

func newLoader() *EnvironmentValuesLoader {
Expand Down Expand Up @@ -34,7 +35,7 @@ func newLoader() *EnvironmentValuesLoader {
func TestEnvValsLoad_SingleValuesFile(t *testing.T) {
l := newLoader()

actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.5.yaml"})
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.5.yaml"}, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -52,7 +53,7 @@ func TestEnvValsLoad_SingleValuesFile(t *testing.T) {
func TestEnvValsLoad_OverwriteNilValue_Issue1150(t *testing.T) {
l := newLoader()

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

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

actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/issues/1168/addons.yaml", "testdata/issues/1168/addons2.yaml"})
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/issues/1168/addons.yaml", "testdata/issues/1168/addons2.yaml"}, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
0