Skip to content

Commit

Permalink
feat: All the paths are relative to helmfile.yaml (roboll#261)
Browse files Browse the repository at this point in the history
`helmfile lint` works with relative chart reference (roboll#252)
The tempalte function `readFile` accepts the path relative to helmfile.yaml

Resolves roboll#246
Fixes roboll#252
  • Loading branch information
mumoshu authored Aug 30, 2018
1 parent 79f0e70 commit 421299c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion helmexec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (helm *execer) DecryptSecret(name string) (string, error) {

// os.Rename seems to results in "cross-device link` errors in some cases
// Instead of moving, copy it to the destination temp file as a work-around
// See https://github.com/roboll/helmfile/issues/251#issuecomment-417166296
// See https://github.com/roboll/helmfile/issues/251#issuecomment-417166296f
decFile, err := os.Open(name + ".dec")
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/urfave/cli"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io/ioutil"
)

const (
Expand Down Expand Up @@ -400,7 +401,7 @@ func eachDesiredStateDo(c *cli.Context, converge func(*state.HelmState, helmexec
}
allSelectorNotMatched := true
for _, f := range desiredStateFiles {
yamlBuf, err := tmpl.RenderTemplateFileToBuffer(f)
yamlBuf, err := tmpl.NewFileRenderer(ioutil.ReadFile, "").RenderTemplateFileToBuffer(f)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (state *HelmState) LintReleases(helm helmexec.Interface, additionalValues [
}

chartPath := ""
if isLocalChart(release.Chart) {
if pathExists(normalizeChart(state.BaseChartPath, release.Chart)) {
chartPath = normalizeChart(state.BaseChartPath, release.Chart)
} else {
fetchFlags := []string{}
Expand Down Expand Up @@ -586,14 +586,18 @@ func (state *HelmState) UpdateDeps(helm helmexec.Interface) []error {
// be constructed relative to the `base path`.
// - Everything else is assumed to be an absolute path or an actual <repository>/<chart> reference.
func normalizeChart(basePath, chart string) string {
regex, _ := regexp.Compile("^[.]?./")
if !regex.MatchString(chart) {
if !isLocalChart(chart) {
return chart
}
return filepath.Join(basePath, chart)
}

func isLocalChart(chart string) bool {
regex, _ := regexp.Compile("^[.]?./")
return regex.MatchString(chart)
}

func pathExists(chart string) bool {
_, err := os.Stat(chart)
return err == nil
}
Expand Down Expand Up @@ -680,7 +684,7 @@ func (state *HelmState) namespaceAndValuesFlags(helm helmexec.Interface, basePat
}
defer valfile.Close()

r := valuesfile.NewRenderer(ioutil.ReadFile)
r := valuesfile.NewRenderer(ioutil.ReadFile, state.BaseChartPath)
yamlBytes, err := r.RenderToBytes(path)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func Test_isLocalChart(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLocalChart(tt.args.chart); got != tt.want {
t.Errorf("isLocalChart() = %v, want %v", got, tt.want)
t.Errorf("pathExists() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -879,7 +879,7 @@ func TestHelmState_UpdateDeps(t *testing.T) {
Chart: "published/deeper",
},
{
Chart: "./error",
Chart: ".error",
},
},
}
Expand Down
21 changes: 8 additions & 13 deletions tmpl/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package tmpl

import (
"bytes"
"io/ioutil"
"path/filepath"
)

var DefaultFileRenderer *templateFileRenderer

func init() {
DefaultFileRenderer = NewFileRenderer(ioutil.ReadFile)
}

type templateFileRenderer struct {
basePath string
ReadFile func(string) ([]byte, error)
Context *Context
}
Expand All @@ -20,8 +15,9 @@ type FileRenderer interface {
RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error)
}

func NewFileRenderer(readFile func(filename string) ([]byte, error)) *templateFileRenderer {
func NewFileRenderer(readFile func(filename string) ([]byte, error), basePath string) *templateFileRenderer {
return &templateFileRenderer{
basePath: basePath,
ReadFile: readFile,
Context: &Context{
readFile: readFile,
Expand All @@ -30,14 +26,13 @@ func NewFileRenderer(readFile func(filename string) ([]byte, error)) *templateFi
}

func (r *templateFileRenderer) RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error) {
content, err := r.ReadFile(file)
// path to the file relative to the helmfile.yaml
path := filepath.Join(r.basePath, file)

content, err := r.ReadFile(path)
if err != nil {
return nil, err
}

return r.Context.RenderTemplateToBuffer(string(content))
}

func RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error) {
return DefaultFileRenderer.RenderTemplateFileToBuffer(file)
}
4 changes: 2 additions & 2 deletions valuesfile/valuesfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type renderer struct {
tmplFileRenderer tmpl.FileRenderer
}

func NewRenderer(readFile func(filename string) ([]byte, error)) *renderer {
func NewRenderer(readFile func(filename string) ([]byte, error), basePath string) *renderer {
return &renderer{
readFile: readFile,
tmplFileRenderer: tmpl.NewFileRenderer(readFile),
tmplFileRenderer: tmpl.NewFileRenderer(readFile, basePath),
}
}

Expand Down
4 changes: 2 additions & 2 deletions valuesfile/valuesfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestRenderToBytes_Gotmpl(t *testing.T) {
return []byte(dataFileContent), nil
}
return nil, fmt.Errorf("unexpected filename: expected=%v or %v, actual=%s", dataFile, valuesTmplFile, filename)
})
}, "")
buf, err := r.RenderToBytes(valuesTmplFile)
if err != nil {
t.Errorf("unexpected error: %v", err)
Expand All @@ -49,7 +49,7 @@ func TestRenderToBytes_Yaml(t *testing.T) {
return []byte(valuesYamlContent), nil
}
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", valuesFile, filename)
})
}, "")
buf, err := r.RenderToBytes(valuesFile)
if err != nil {
t.Errorf("unexpected error: %v", err)
Expand Down

0 comments on commit 421299c

Please sign in to comment.