Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: All the paths are relative to helmfile.yaml #261

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("^[.]?./")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mumoshu Is there a reason a / is required to be a local chart? Its seems valid to have the helmfile.yaml in the same folder as a local chart:

├── Chart.yaml
├── charts
│   ├── grafana-1.14.7.tgz
│   ├── influxdb-0.11.0.tgz
│   ├── kapacitor-1.1.0.tgz
│   ├── telegraf-ds-1.7.4.tgz
│   └── telegraf-s-1.7.4.tgz
├── helmfile.d
│   └── helmfile.yaml

And the helmfile.yaml:

releases:
- name: myrelease
  chart: ..

I'd expect the regex to just be ^[.].

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I can work around this by adding a trailing slash / to my chart path.

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