From e2bd6516155c177d4faab688ca7e54c4981ad94e Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 18 May 2016 16:19:32 -0600 Subject: [PATCH] fix(helm): allow TILLER_HOME to be used everywhere This refactors handling of the TILLER_HOME and HELM_HOME env vars so that they can be set once and used everywhere. Individual commands no longer need to handle setting. --- cmd/helm/helm.go | 36 +++++++++++++++++++++++++++++++----- cmd/helm/install.go | 25 ------------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 780182db4..3bf0a904a 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -6,11 +6,20 @@ import ( "os" "strings" + "github.com/kubernetes/helm/pkg/helm" "github.com/spf13/cobra" "google.golang.org/grpc" ) +const ( + homeEnvVar = "HELM_HOME" + defaultHome = "$HOME/.helm" // FIXME: is $HOME windows compatible? + hostEnvVar = "TILLER_HOST" + defaultHost = ":44134" +) + var helmHome string +var tillerHost string // flagVerbose is a signal that the user wants additional output. var flagVerbose bool @@ -33,18 +42,30 @@ Common actions from this point include: Environment: $HELM_HOME Set an alternative location for Helm files. By default, these are stored in ~/.helm + $TILLER_HOST Set an alternative Tiller host. The format is host:port (default ":44134"). ` // RootCommand is the top-level command for Helm. var RootCommand = &cobra.Command{ - Use: "helm", - Short: "The Helm package manager for Kubernetes.", - Long: globalUsage, + Use: "helm", + Short: "The Helm package manager for Kubernetes.", + Long: globalUsage, + PersistentPreRun: bootstrap, } func init() { - RootCommand.PersistentFlags().StringVar(&helmHome, "home", "$HOME/.helm", "location of your Helm config") - RootCommand.PersistentFlags().BoolVarP(&flagVerbose, "verbose", "v", false, "enable verbose output") + home := os.Getenv(homeEnvVar) + if home == "" { + home = "$HOME/.helm" + } + thost := os.Getenv(hostEnvVar) + if thost == "" { + thost = defaultHost + } + p := RootCommand.PersistentFlags() + p.StringVar(&helmHome, "home", home, "location of your Helm config. Overrides $HELM_HOME.") + p.StringVar(&tillerHost, "host", thost, "address of tiller. Overrides $TILLER_HOST.") + p.BoolVarP(&flagVerbose, "verbose", "v", false, "enable verbose output") } func main() { @@ -53,6 +74,11 @@ func main() { } } +func bootstrap(c *cobra.Command, args []string) { + // Set up the gRPC config. + helm.Config.ServAddr = tillerHost +} + func checkArgsLength(expectedNum, actualNum int, requiredArgs ...string) error { if actualNum != expectedNum { arg := "arguments" diff --git a/cmd/helm/install.go b/cmd/helm/install.go index c98be823f..9c3be0c4c 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "os" "github.com/spf13/cobra" @@ -19,17 +18,10 @@ path to a chart directory or the name of a chart in the current working directory. ` -const ( - hostEnvVar = "TILLER_HOST" - defaultHost = ":44134" -) - // install flags & args var ( // installArg is the name or relative path of the chart to install installArg string - // tillerHost overrides TILLER_HOST envVar - tillerHost string // installDryRun performs a dry-run install installDryRun bool ) @@ -46,7 +38,6 @@ func runInstall(cmd *cobra.Command, args []string) error { return err } installArg = args[0] - setupInstallEnv() res, err := helm.InstallRelease(installArg, installDryRun) if err != nil { @@ -72,23 +63,7 @@ func printRelease(rel *release.Release) { } } -func setupInstallEnv() { - // The 'host' flag takes precendence uber alles. - // If set, proceed with install using provided host - // address. If unset, the 'TILLER_HOST' environment - // variable (if set) is used, otherwise defaults to ":44134". - if tillerHost == "" { - tillerHost = os.Getenv(hostEnvVar) - if tillerHost == "" { - tillerHost = defaultHost - } - } - - helm.Config.ServAddr = tillerHost -} - func init() { - installCmd.Flags().StringVar(&tillerHost, "host", "", "address of tiller server (default \":44134\")") installCmd.Flags().BoolVar(&installDryRun, "dry-run", false, "simulate an install") RootCommand.AddCommand(installCmd)