fix first pass rendering crash on syntax error (#374)
This commit is contained in:
parent
429c42d429
commit
942f9a20b0
|
|
@ -402,7 +402,7 @@ environments:
|
||||||
|
|
||||||
releases:
|
releases:
|
||||||
|
|
||||||
{{ if (eq .Environment.Name "production" }}
|
{{ if eq .Environment.Name "production" }}
|
||||||
- name: newrelic-agent
|
- name: newrelic-agent
|
||||||
# snip
|
# snip
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
||||||
14
main.go
14
main.go
|
|
@ -3,16 +3,15 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/roboll/helmfile/args"
|
"github.com/roboll/helmfile/args"
|
||||||
"github.com/roboll/helmfile/environment"
|
"github.com/roboll/helmfile/environment"
|
||||||
|
|
@ -22,7 +21,6 @@ import (
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
"os/exec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -726,8 +724,12 @@ func (r *twoPassRenderer) renderEnvironment(content []byte) environment.Environm
|
||||||
|
|
||||||
// parse as much as we can, tolerate errors, this is a preparse
|
// parse as much as we can, tolerate errors, this is a preparse
|
||||||
yamlBuf, err := firstPassRenderer.RenderTemplateContentToBuffer(content)
|
yamlBuf, err := firstPassRenderer.RenderTemplateContentToBuffer(content)
|
||||||
if err != nil && logger != nil {
|
if err != nil && r.logger != nil {
|
||||||
r.logger.Debugf("first-pass rendering input of \"%s\":\n%s", r.filename, prependLineNumbers(string(content)))
|
r.logger.Debugf("first-pass rendering input of \"%s\":\n%s", r.filename, prependLineNumbers(string(content)))
|
||||||
|
if yamlBuf == nil { // we have a template syntax error, let the second parse report
|
||||||
|
r.logger.Debugf("template syntax error: %v", err)
|
||||||
|
return firstPassEnv
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c := state.NewCreator(r.logger, r.reader, r.abs)
|
c := state.NewCreator(r.logger, r.reader, r.abs)
|
||||||
c.Strict = false
|
c.Strict = false
|
||||||
|
|
|
||||||
28
main_test.go
28
main_test.go
|
|
@ -2,10 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/roboll/helmfile/helmexec"
|
||||||
"github.com/roboll/helmfile/state"
|
"github.com/roboll/helmfile/state"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
@ -16,7 +18,7 @@ func makeRenderer(readFile func(string) ([]byte, error), env string) *twoPassRen
|
||||||
env: env,
|
env: env,
|
||||||
namespace: "namespace",
|
namespace: "namespace",
|
||||||
filename: "",
|
filename: "",
|
||||||
logger: logger,
|
logger: helmexec.NewLogger(os.Stdout, "debug"),
|
||||||
abs: filepath.Abs,
|
abs: filepath.Abs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -228,3 +230,27 @@ func TestReadFromYaml_RenderTemplateWithNamespace(t *testing.T) {
|
||||||
t.Errorf("release name should be namespace-myrelease")
|
t.Errorf("release name should be namespace-myrelease")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadFromYaml_HelfileShouldBeResilentToTemplateErrors(t *testing.T) {
|
||||||
|
yamlContent := []byte(`environments:
|
||||||
|
staging:
|
||||||
|
production:
|
||||||
|
|
||||||
|
releases:
|
||||||
|
{{ if (eq .Environment.Name "production" }} # notice syntax error: unclosed left paren
|
||||||
|
- name: prod-myrelease
|
||||||
|
{{ else }}
|
||||||
|
- name: myapp
|
||||||
|
{{ end }}
|
||||||
|
chart: mychart
|
||||||
|
`)
|
||||||
|
fileReader := func(filename string) ([]byte, error) {
|
||||||
|
return yamlContent, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r := makeRenderer(fileReader, "staging")
|
||||||
|
_, err := r.renderTemplate(yamlContent)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("wanted error, none returned")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue