Skip to content

Commit

Permalink
Fix panic while loading environment secrets (helm#1164)
Browse files Browse the repository at this point in the history
This fixes a regression in helm#1160
  • Loading branch information
mumoshu committed Mar 30, 2020
1 parent 6643a41 commit 51ecd12
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
8 changes: 2 additions & 6 deletions pkg/app/desired_state_file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

const (
DefaultHelmBinary = "helm"
DefaultHelmBinary = state.DefaultHelmBinary
)

type desiredStateLoader struct {
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/state/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"gopkg.in/yaml.v2"
)

const (
DefaultHelmBinary = "helm"
)

type StateLoadError struct {
msg string
Cause error
Expand Down Expand Up @@ -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,
Expand All @@ -58,6 +64,8 @@ func NewCreator(logger *zap.SugaredLogger, readFile func(string) ([]byte, error)
Strict: true,
valsRuntime: valsRuntime,
getHelm: getHelm,

overrideHelmBinary: overrideHelmBinary,
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/state/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 51ecd12

Please sign in to comment.