Skip to content

Commit

Permalink
fix: "cannot unmarshal !!str <no value> into bool" errors in state …
Browse files Browse the repository at this point in the history
…templates

Seems like we are affected by golang/go#24963. That is, even though we internally use the template option `missingkey=zero`, in some cases it still prints `<no value>` instead of zero values, which has been confusing the state yaml parsing.

This fixes the issue by naively replacing all the remaining occurrences of `<no value>` in the rendered text, while printing debug logs to ease debugging in the future when there is unexpected side-effects introduced by this native method.

Fixes #553
  • Loading branch information
mumoshu committed Jun 4, 2019
1 parent 1d3f5f8 commit bce1572
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Masterminds/semver v1.4.1
github.com/Masterminds/sprig v2.16.0+incompatible
github.com/aokoli/goutils v1.0.1 // indirect
github.com/google/go-cmp v0.3.0 // indirect
github.com/google/go-cmp v0.3.0
github.com/google/uuid v0.0.0-20161128191214-064e2069ce9c // indirect
github.com/huandu/xstrings v1.0.0 // indirect
github.com/imdario/mergo v0.3.6
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app_test.go
Expand Up @@ -838,7 +838,7 @@ releases:
}
}

func TestVisitDesiredStatesWithReleasesFiltered_StateValueOverrides(t *testing.T) {
func TestVisitDesiredStatesWithReleasesFiltered_EnvironmentValueOverrides(t *testing.T) {
files := map[string]string{
"/path/to/helmfile.yaml": `
environments:
Expand Down
13 changes: 12 additions & 1 deletion pkg/app/two_pass_renderer.go
Expand Up @@ -3,6 +3,7 @@ package app
import (
"bytes"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/roboll/helmfile/pkg/environment"
"github.com/roboll/helmfile/pkg/state"
"github.com/roboll/helmfile/pkg/tmpl"
Expand Down Expand Up @@ -31,10 +32,20 @@ func (r *desiredStateLoader) renderEnvironment(firstPassEnv *environment.Environ
return firstPassEnv
}
}
yamlData := yamlBuf.String()

// Work-around for https://github.com/golang/go/issues/24963
sanitized := strings.ReplaceAll(yamlData, "<no value>", "")

if len(yamlData) != len(sanitized) {
msg := "replaced <no value>s to workaround https://github.com/golang/go/issues/24963 to address https://github.com/roboll/helmfile/issues/553:\n%s"
r.logger.Debugf(msg, cmp.Diff(yamlData, sanitized))
}

c := r.underlying()
c.Strict = false
// create preliminary state, as we may have an environment. Tolerate errors.
prestate, err := c.ParseAndLoad(yamlBuf.Bytes(), baseDir, filename, r.env, false, firstPassEnv)
prestate, err := c.ParseAndLoad([]byte(sanitized), baseDir, filename, r.env, false, firstPassEnv)
if err != nil && r.logger != nil {
switch err.(type) {
case *state.StateLoadError:
Expand Down
4 changes: 2 additions & 2 deletions pkg/tmpl/context_tmpl.go
Expand Up @@ -13,9 +13,9 @@ func (c *Context) stringTemplate() *template.Template {
}
tmpl := template.New("stringTemplate").Funcs(funcMap)
if c.preRender {
tmpl.Option("missingkey=zero")
tmpl = tmpl.Option("missingkey=zero")
} else {
tmpl.Option("missingkey=error")
tmpl = tmpl.Option("missingkey=error")
}
return tmpl
}
Expand Down

0 comments on commit bce1572

Please sign in to comment.