Relative path resolution changes

This aims to improve on relative, absolute and <repo>/<chart>
resolution for several of Helmfile manifest attributes as well as
command line arguments.

- Uusers may now utilize the `-f <filepath>` flag to reference
a helmfile manifest outside of their PWD and have relative paths
in the the `charts[*].values` and `charts[*].chart` attribute resolve
relative to the manfiest's location properly

- Values provided in the `--values` argument are no longer automatically
assumed as relative path references from PWD but instead are
conditionally joined with the PWD if it is determined they are relative
file paths

- Users may still signify a chart in the `.charts[*].chart` attribute
as they do now by having an explicit `<repo>/<chart>` value
This commit is contained in:
Justin Nauman 2017-09-01 21:56:08 -05:00
parent 7a5b32fdea
commit 2e914c652a
2 changed files with 30 additions and 46 deletions

View File

@ -6,8 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"regexp"
"strings" "strings"
) )
@ -48,27 +46,7 @@ func (helm *execer) UpdateRepo() error {
return err return err
} }
func normalizeChart(chart string) (string, error) {
regex, err := regexp.Compile("^[.]?./")
if err != nil {
return "", err
}
if !regex.MatchString(chart) {
return chart, nil
}
wd, err := os.Getwd()
if err != nil {
return "", err
}
path := filepath.Join(wd, chart)
return path, nil
}
func (helm *execer) SyncChart(name, chart string, flags ...string) error { func (helm *execer) SyncChart(name, chart string, flags ...string) error {
chart, err := normalizeChart(chart)
if err != nil {
return err
}
out, err := helm.exec(append([]string{"upgrade", "--install", name, chart}, flags...)...) out, err := helm.exec(append([]string{"upgrade", "--install", name, chart}, flags...)...)
if helm.writer != nil { if helm.writer != nil {
helm.writer.Write(out) helm.writer.Write(out)
@ -77,10 +55,6 @@ func (helm *execer) SyncChart(name, chart string, flags ...string) error {
} }
func (helm *execer) DiffChart(name, chart string, flags ...string) error { func (helm *execer) DiffChart(name, chart string, flags ...string) error {
chart, err := normalizeChart(chart)
if err != nil {
return err
}
out, err := helm.exec(append([]string{"diff", name, chart}, flags...)...) out, err := helm.exec(append([]string{"diff", name, chart}, flags...)...)
if helm.writer != nil { if helm.writer != nil {
helm.writer.Write(out) helm.writer.Write(out)

View File

@ -12,11 +12,14 @@ import (
"github.com/roboll/helmfile/helmexec" "github.com/roboll/helmfile/helmexec"
yaml "gopkg.in/yaml.v1" yaml "gopkg.in/yaml.v1"
"path"
"regexp"
) )
type HelmState struct { type HelmState struct {
Repositories []RepositorySpec `yaml:"repositories"` BaseChartPath string
Charts []ChartSpec `yaml:"charts"` Repositories []RepositorySpec `yaml:"repositories"`
Charts []ChartSpec `yaml:"charts"`
} }
type RepositorySpec struct { type RepositorySpec struct {
@ -48,6 +51,7 @@ func ReadFromFile(file string) (*HelmState, error) {
} }
var state HelmState var state HelmState
state.BaseChartPath = path.Dir(file)
if err := yaml.Unmarshal(content, &state); err != nil { if err := yaml.Unmarshal(content, &state); err != nil {
return nil, err return nil, err
} }
@ -86,20 +90,19 @@ func (state *HelmState) SyncCharts(helm helmexec.Interface, additonalValues []st
for _, chart := range state.Charts { for _, chart := range state.Charts {
wg.Add(1) wg.Add(1)
go func(wg *sync.WaitGroup, chart ChartSpec) { go func(wg *sync.WaitGroup, chart ChartSpec) {
flags, flagsErr := flagsForChart(&chart) flags, flagsErr := flagsForChart(state.BaseChartPath, &chart)
if flagsErr != nil { if flagsErr != nil {
errs = append(errs, flagsErr) errs = append(errs, flagsErr)
} }
for _, value := range additonalValues { for _, value := range additonalValues {
wd, wdErr := os.Getwd() valfile, err := filepath.Abs(value)
if wdErr != nil { if err != nil {
errs = append(errs, wdErr) errs = append(errs, err)
} }
valfile := filepath.Join(wd, value)
flags = append(flags, "--values", valfile) flags = append(flags, "--values", valfile)
} }
if len(errs) == 0 { if len(errs) == 0 {
if err := helm.SyncChart(chart.Name, chart.Chart, flags...); err != nil { if err := helm.SyncChart(chart.Name, normalizeChart(state.BaseChartPath, chart.Chart), flags...); err != nil {
errs = append(errs, err) errs = append(errs, err)
} }
} }
@ -124,20 +127,19 @@ func (state *HelmState) DiffCharts(helm helmexec.Interface, additonalValues []st
go func(wg *sync.WaitGroup, chart ChartSpec) { go func(wg *sync.WaitGroup, chart ChartSpec) {
// Plugin command doesn't support explicit namespace // Plugin command doesn't support explicit namespace
chart.Namespace = "" chart.Namespace = ""
flags, flagsErr := flagsForChart(&chart) flags, flagsErr := flagsForChart(state.BaseChartPath, &chart)
if flagsErr != nil { if flagsErr != nil {
errs = append(errs, flagsErr) errs = append(errs, flagsErr)
} }
for _, value := range additonalValues { for _, value := range additonalValues {
wd, wdErr := os.Getwd() valfile, err := filepath.Abs(value)
if wdErr != nil { if err != nil {
errs = append(errs, wdErr) errs = append(errs, err)
} }
valfile := filepath.Join(wd, value)
flags = append(flags, "--values", valfile) flags = append(flags, "--values", valfile)
} }
if len(errs) == 0 { if len(errs) == 0 {
if err := helm.DiffChart(chart.Name, chart.Chart, flags...); err != nil { if err := helm.DiffChart(chart.Name, normalizeChart(state.BaseChartPath, chart.Chart), flags...); err != nil {
errs = append(errs, err) errs = append(errs, err)
} }
} }
@ -175,7 +177,19 @@ func (state *HelmState) DeleteCharts(helm helmexec.Interface) []error {
return nil return nil
} }
func flagsForChart(chart *ChartSpec) ([]string, error) { // normalizeChart allows for the distinction between a file path reference and repository references.
// - Any single (or double character) followed by a `/` will be considered a local file reference and
// 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) {
return chart
}
return filepath.Join(basePath, chart)
}
func flagsForChart(basePath string, chart *ChartSpec) ([]string, error) {
flags := []string{} flags := []string{}
if chart.Version != "" { if chart.Version != "" {
flags = append(flags, "--version", chart.Version) flags = append(flags, "--version", chart.Version)
@ -187,11 +201,7 @@ func flagsForChart(chart *ChartSpec) ([]string, error) {
flags = append(flags, "--namespace", chart.Namespace) flags = append(flags, "--namespace", chart.Namespace)
} }
for _, value := range chart.Values { for _, value := range chart.Values {
wd, err := os.Getwd() valfile := filepath.Join(basePath, value)
if err != nil {
return nil, err
}
valfile := filepath.Join(wd, value)
flags = append(flags, "--values", valfile) flags = append(flags, "--values", valfile)
} }
if len(chart.SetValues) > 0 { if len(chart.SetValues) > 0 {