Repository: roboll/helmfile
Pull request #1164 (2020-03-30 10:47:57)

Fix panic while loading environment secrets
This fixes a regression in #1160


Patch:
diff --git a/pkg/app/desired_state_file_loader.go b/pkg/app/desired_state_file_loader.go
index 11c8ba3..ad00cb1 100644
--- a/pkg/app/desired_state_file_loader.go
+++ b/pkg/app/desired_state_file_loader.go
@@ -16,7 +16,7 @@ import (
 )
 
 const (
-	DefaultHelmBinary = "helm"
+	DefaultHelmBinary = state.DefaultHelmBinary
 )
 
 type desiredStateLoader struct {
@@ -79,10 +79,6 @@ func (ld *desiredStateLoader) Load(f string, opts LoadOpts) (*state.HelmState, e
 		st.HelmDefaults.KubeContext = ld.overrideKubeContext
 	}
 
-	if ld.overrideHelmBinary != DefaultHelmBinary || st.DefaultHelmBinary == "" {
-		st.DefaultHelmBinary = ld.overrideHelmBinary
-	}
-
 	if ld.namespace != "" {
 		if st.OverrideNamespace != "" {
 			return nil, errors.New("err: Cannot use option --namespace and set attribute namespace.")
@@ -151,7 +147,7 @@ func (ld *desiredStateLoader) loadFileWithOverrides(inheritedEnv, overrodeEnv *e
 }
 
 func (a *desiredStateLoader) underlying() *state.StateCreator {
-	c := state.NewCreator(a.logger, a.readFile, a.fileExists, a.abs, a.glob, a.valsRuntime, a.getHelm)
+	c := state.NewCreator(a.logger, a.readFile, a.fileExists, a.abs, a.glob, a.valsRuntime, a.getHelm, a.overrideHelmBinary)
 	c.LoadFile = a.loadFile
 	return c
 }
diff --git a/pkg/state/create.go b/pkg/state/create.go
index 1c8d068..3315d74 100644
--- a/pkg/state/create.go
+++ b/pkg/state/create.go
@@ -16,6 +16,10 @@ import (
 	"gopkg.in/yaml.v2"
 )
 
+const (
+	DefaultHelmBinary = "helm"
+)
+
 type StateLoadError struct {
 	msg   string
 	Cause error
@@ -46,9 +50,11 @@ type StateCreator struct {
 	LoadFile func(inheritedEnv *environment.Environment, baseDir, file string, evaluateBases bool) (*HelmState, error)
 
 	getHelm func(*HelmState) helmexec.Interface
+
+	overrideHelmBinary string
 }
 
-func NewCreator(logger *zap.SugaredLogger, readFile func(string) ([]byte, error), fileExists func(string) (bool, error), abs func(string) (string, error), glob func(string) ([]string, error), valsRuntime vals.Evaluator, getHelm func(*HelmState) helmexec.Interface) *StateCreator {
+func NewCreator(logger *zap.SugaredLogger, readFile func(string) ([]byte, error), fileExists func(string) (bool, error), abs func(string) (string, error), glob func(string) ([]string, error), valsRuntime vals.Evaluator, getHelm func(*HelmState) helmexec.Interface, overrideHelmBinary string) *StateCreator {
 	return &StateCreator{
 		logger:      logger,
 		readFile:    readFile,
@@ -58,6 +64,8 @@ func NewCreator(logger *zap.SugaredLogger, readFile func(string) ([]byte, error)
 		Strict:      true,
 		valsRuntime: valsRuntime,
 		getHelm:     getHelm,
+
+		overrideHelmBinary: overrideHelmBinary,
 	}
 }
 
@@ -104,6 +112,13 @@ func (c *StateCreator) Parse(content []byte, baseDir, file string) (*HelmState,
 		state.HelmDefaults.KubeContext = state.DeprecatedContext
 	}
 
+	if c.overrideHelmBinary != "" && c.overrideHelmBinary != DefaultHelmBinary {
+		state.DefaultHelmBinary = c.overrideHelmBinary
+	} else if state.DefaultHelmBinary == "" {
+		// Let `helmfile --helm-binary ""` not break this helmfile run
+		state.DefaultHelmBinary = DefaultHelmBinary
+	}
+
 	state.logger = c.logger
 
 	state.readFile = c.readFile


Test patch:
diff --git a/pkg/state/create_test.go b/pkg/state/create_test.go
index d42db9b..3b9a791 100644
--- a/pkg/state/create_test.go
+++ b/pkg/state/create_test.go
@@ -108,7 +108,7 @@ bar: {{ readFile "bar.txt" }}
 	})
 	testFs.Cwd = "/example/path/to"
 
-	state, err := NewCreator(logger, testFs.ReadFile, testFs.FileExists, testFs.Abs, testFs.Glob, nil, nil).ParseAndLoad(yamlContent, filepath.Dir(yamlFile), yamlFile, "production", true, nil)
+	state, err := NewCreator(logger, testFs.ReadFile, testFs.FileExists, testFs.Abs, testFs.Glob, nil, nil, "").ParseAndLoad(yamlContent, filepath.Dir(yamlFile), yamlFile, "production", true, nil)
 	if err != nil {
 		t.Fatalf("unexpected error: %v", err)
 	}