From 01fe5b42136ac9b5dac4f029114af26416527d01 Mon Sep 17 00:00:00 2001 From: rmartinez3 Date: Wed, 18 Jul 2018 21:53:09 -0500 Subject: [PATCH] Adding merging for helmDefaults args and --args, adding tillerNamspace and kubeContext to helmDefaults (#186) Add tillerNamespace and kubeContext to helmDefaults. Also add merging of args capability. People can start adding additional fields under helmDefaults on top of this. The additional fields are merged to args as long as they are implemented using the framework introduced by this commit. Changelog: * adding merging for helm defaults args and --args, adding tillernamspace, kubecontext to helmdefaults, adding getargs to lint command * adding docs for helmdefaults * checking if empty default values --- README.md | 11 +++++++++ main.go | 62 +++++++++++++++++++++++++++++++++++++++++++++----- state/state.go | 4 +++- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 69e2d040..1dd510dd 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,17 @@ repositories: context: kube-context # kube-context (--kube-context) +#default values to set for args along with dedicated keys that can be set by contributers, cli args take precedence overe these +helmDefaults: + tillerNamespace: tiller-namespace #dedicated default key for tiller-namespace + kubeContext: kube-context #dedicated default key for kube-context + args: + - "--wait" + - "--recreate-pods" + - "--timeout=600" + - "--force" + - "--reset-values" + releases: # Published chart example - name: vault # name of this release diff --git a/main.go b/main.go index d2895598..4a92c41c 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,10 @@ import ( "log" "os" "os/signal" - "strings" - "syscall" - "path/filepath" "sort" + "strings" + "syscall" "github.com/roboll/helmfile/helmexec" "github.com/roboll/helmfile/state" @@ -218,9 +217,9 @@ func main() { }, Action: func(c *cli.Context) error { return eachDesiredStateDo(c, func(state *state.HelmState, helm helmexec.Interface) []error { - args := c.String("args") + args := getArgs(c, state) if len(args) > 0 { - helm.SetExtraArgs(strings.Split(args, " ")...) + helm.SetExtraArgs(args...) } if c.GlobalString("helm-binary") != "" { helm.SetHelmBinary(c.GlobalString("helm-binary")) @@ -518,9 +517,60 @@ func clean(state *state.HelmState, errs []error) error { func getArgs(c *cli.Context, state *state.HelmState) []string { args := c.String("args") + argsMap := map[string]string{} + if len(args) > 0 { - state.HelmDefaults.Args = strings.Split(args, " ") + argsVals := strings.Split(args, " ") + for _, arg := range argsVals { + argVal := strings.SplitN(arg, "=", 2) + if len(argVal) > 1 { + arg := argVal[0] + value := argVal[1] + argsMap[arg] = value + } else { + arg := argVal[0] + argsMap[arg] = "" + } + } } + if len(state.HelmDefaults.Args) > 0 { + for _, arg := range state.HelmDefaults.Args { + argVal := strings.SplitN(arg, "=", 2) + arg := argVal[0] + if _, exists := argsMap[arg]; !exists { + if len(argVal) > 1 { + argsMap[arg] = argVal[1] + } else { + argsMap[arg] = "" + } + } + } + } + + if state.HelmDefaults.TillerNamespace != "" { + setDefaultValue(argsMap, "--tiller-namespace", state.HelmDefaults.TillerNamespace) + } + if state.HelmDefaults.KubeContext != "" { + setDefaultValue(argsMap, "--kube-context", state.HelmDefaults.KubeContext) + } + + var argArr []string + + for key, val := range argsMap { + if val != "" { + argArr = append(argArr, fmt.Sprintf("%s=%s", key, val)) + } else { + argArr = append(argArr, fmt.Sprintf("%s", key)) + } + } + + state.HelmDefaults.Args = argArr return state.HelmDefaults.Args } + +func setDefaultValue(argsMap map[string]string, flag string, value string) { + if _, exists := argsMap[flag]; !exists { + argsMap[flag] = value + } +} diff --git a/state/state.go b/state/state.go index be927d2a..135663db 100644 --- a/state/state.go +++ b/state/state.go @@ -35,7 +35,9 @@ type HelmState struct { // HelmSpec to defines helmDefault values type HelmSpec struct { - Args []string `yaml:"args"` + KubeContext string `yaml:"kubeContext"` + TillerNamespace string `yaml:"tillerNamespace"` + Args []string `yaml:"args"` } // RepositorySpec that defines values for a helm repo