From 65ee6a21240b2ff480cbe26f21676115e55e6b82 Mon Sep 17 00:00:00 2001 From: KUOKA Yusuke Date: Tue, 4 Jun 2019 13:23:38 +0900 Subject: [PATCH] fix: "cannot unmarshal !!str `` into bool" errors in state templates (#645) Seems like we are affected by https://github.com/golang/go/issues/24963. That is, even though we internally use the template option `missingkey=zero`, in some cases it still prints `` instead of zero values, which has been confusing the state yaml parsing. This fixes the issue by naively replacing all the remaining occurrences of `` 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 --- go.mod | 2 +- pkg/app/app_test.go | 2 +- pkg/app/two_pass_renderer.go | 13 ++++++++++++- pkg/tmpl/context_tmpl.go | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 78615183..a887beab 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 8321a159..4fa19032 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -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: diff --git a/pkg/app/two_pass_renderer.go b/pkg/app/two_pass_renderer.go index 623a0961..51d6b061 100644 --- a/pkg/app/two_pass_renderer.go +++ b/pkg/app/two_pass_renderer.go @@ -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" @@ -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, "", "") + + if len(yamlData) != len(sanitized) { + msg := "replaced 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: diff --git a/pkg/tmpl/context_tmpl.go b/pkg/tmpl/context_tmpl.go index 70445bf6..f8dd5369 100644 --- a/pkg/tmpl/context_tmpl.go +++ b/pkg/tmpl/context_tmpl.go @@ -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 }