feat: All the paths are relative to helmfile.yaml (#261)
`helmfile lint` works with relative chart reference (#252) The tempalte function `readFile` accepts the path relative to helmfile.yaml Resolves #246 Fixes #252
This commit is contained in:
parent
79f0e70ce8
commit
421299c883
|
|
@ -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
|
||||
|
|
|
|||
3
main.go
3
main.go
|
|
@ -18,6 +18,7 @@ import (
|
|||
"github.com/urfave/cli"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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{}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -879,7 +879,7 @@ func TestHelmState_UpdateDeps(t *testing.T) {
|
|||
Chart: "published/deeper",
|
||||
},
|
||||
{
|
||||
Chart: "./error",
|
||||
Chart: ".error",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
21
tmpl/file.go
21
tmpl/file.go
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue