diff --git a/cmd/helm/env.go b/cmd/helm/env.go index 6a0cb8055..4078a000b 100644 --- a/cmd/helm/env.go +++ b/cmd/helm/env.go @@ -19,7 +19,6 @@ package main import ( "fmt" "io" - "sort" "helm.sh/helm/pkg/cli" @@ -56,16 +55,8 @@ type envOptions struct { } func (o *envOptions) run(out io.Writer) error { - - // Sorting keys to display in alphabetical order - var keys []string - for k := range o.settings.EnvironmentVariables { - keys = append(keys, k) - } - sort.Strings(keys) - - for _, k := range keys { - fmt.Printf("%s=\"%s\" \n", k, o.settings.EnvironmentVariables[k]) + for k, v := range o.settings.EnvVars() { + fmt.Printf("%s=\"%s\" \n", k, v) } return nil } diff --git a/cmd/helm/load_plugins.go b/cmd/helm/load_plugins.go index 568adb265..b74bc22ea 100644 --- a/cmd/helm/load_plugins.go +++ b/cmd/helm/load_plugins.go @@ -89,8 +89,13 @@ func loadPlugins(baseCmd *cobra.Command, out io.Writer) { return errors.Errorf("plugin %q exited with error", md.Name) } + env := os.Environ() + for k, v := range settings.EnvVars() { + env = append(env, fmt.Sprintf("%s=%s", k, v)) + } + prog := exec.Command(main, argv...) - prog.Env = os.Environ() + prog.Env = env prog.Stdin = os.Stdin prog.Stdout = out prog.Stderr = os.Stderr diff --git a/cmd/helm/root.go b/cmd/helm/root.go index 74080ef0c..b7b9cbe3b 100644 --- a/cmd/helm/root.go +++ b/cmd/helm/root.go @@ -216,9 +216,6 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string flags.ParseErrorsWhitelist.UnknownFlags = true flags.Parse(args) - // set defaults from environment - settings.Init(flags) - // Add subcommands cmd.AddCommand( // chart commands diff --git a/cmd/helm/testdata/helmhome/helm/plugins/fullenv/fullenv.sh b/cmd/helm/testdata/helmhome/helm/plugins/fullenv/fullenv.sh index 2170c3686..2efad9b3c 100755 --- a/cmd/helm/testdata/helmhome/helm/plugins/fullenv/fullenv.sh +++ b/cmd/helm/testdata/helmhome/helm/plugins/fullenv/fullenv.sh @@ -1,7 +1,7 @@ #!/bin/sh echo $HELM_PLUGIN_NAME echo $HELM_PLUGIN_DIR -echo $HELM_PLUGIN +echo $HELM_PLUGINS echo $HELM_REPOSITORY_CONFIG echo $HELM_REPOSITORY_CACHE echo $HELM_BIN diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index 1b292a47c..f2483dd19 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -25,6 +25,7 @@ package cli import ( "fmt" "os" + "strconv" "github.com/spf13/pflag" @@ -50,72 +51,45 @@ type EnvSettings struct { RepositoryCache string // PluginsDirectory is the path to the plugins directory. PluginsDirectory string - - // Environment Variables Store. - EnvironmentVariables map[string]string } func New() *EnvSettings { - envSettings := EnvSettings{ - PluginsDirectory: helmpath.DataPath("plugins"), - RegistryConfig: helmpath.ConfigPath("registry.json"), - RepositoryConfig: helmpath.ConfigPath("repositories.yaml"), - RepositoryCache: helmpath.CachePath("repository"), - EnvironmentVariables: make(map[string]string), + env := EnvSettings{ + Namespace: os.Getenv("HELM_NAMESPACE"), + PluginsDirectory: envOr("HELM_PLUGINS", helmpath.DataPath("plugins")), + RegistryConfig: envOr("HELM_REGISTRY_CONFIG", helmpath.ConfigPath("registry.json")), + RepositoryConfig: envOr("HELM_REPOSITORY_CONFIG", helmpath.ConfigPath("repositories.yaml")), + RepositoryCache: envOr("HELM_REPOSITORY_CACHE", helmpath.CachePath("repository")), } - envSettings.setHelmEnvVars() - return &envSettings + env.Debug, _ = strconv.ParseBool(os.Getenv("HELM_DEBUG")) + return &env } // AddFlags binds flags to the given flagset. func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { - fs.StringVarP(&s.Namespace, "namespace", "n", "", "namespace scope for this request") + fs.StringVarP(&s.Namespace, "namespace", "n", s.Namespace, "namespace scope for this request") fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file") fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use") - fs.BoolVar(&s.Debug, "debug", false, "enable verbose output") - + fs.BoolVar(&s.Debug, "debug", s.Debug, "enable verbose output") fs.StringVar(&s.RegistryConfig, "registry-config", s.RegistryConfig, "path to the registry config file") fs.StringVar(&s.RepositoryConfig, "repository-config", s.RepositoryConfig, "path to the file containing repository names and URLs") fs.StringVar(&s.RepositoryCache, "repository-cache", s.RepositoryCache, "path to the file containing cached repository indexes") } -// envMap maps flag names to envvars -var envMap = map[string]string{ - "debug": "HELM_DEBUG", - "namespace": "HELM_NAMESPACE", - "registry-config": "HELM_REGISTRY_CONFIG", - "repository-config": "HELM_REPOSITORY_CONFIG", -} - -func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) { - if fs.Changed(name) { - return - } - if v, ok := os.LookupEnv(envar); ok { - fs.Set(name, v) +func envOr(name, def string) string { + if v, ok := os.LookupEnv(name); ok { + return v } + return def } -func (s *EnvSettings) setHelmEnvVars() { - for key, val := range map[string]string{ - "HELM_HOME": helmpath.DataPath(), - "HELM_PATH_STARTER": helmpath.DataPath("starters"), +func (s *EnvSettings) EnvVars() map[string]string { + return map[string]string{ + "HELM_BIN": os.Args[0], "HELM_DEBUG": fmt.Sprint(s.Debug), + "HELM_PLUGINS": s.PluginsDirectory, "HELM_REGISTRY_CONFIG": s.RegistryConfig, - "HELM_REPOSITORY_CONFIG": s.RepositoryConfig, "HELM_REPOSITORY_CACHE": s.RepositoryCache, - "HELM_PLUGIN": s.PluginsDirectory, - } { - if eVal := os.Getenv(key); len(eVal) > 0 { - val = eVal - } - s.EnvironmentVariables[key] = val - } -} - -// Init sets values from the environment. -func (s *EnvSettings) Init(fs *pflag.FlagSet) { - for name, envar := range envMap { - setFlagFromEnv(name, envar, fs) + "HELM_REPOSITORY_CONFIG": s.RepositoryConfig, } } diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go index a6204a1f7..58cf1c7f4 100644 --- a/pkg/cli/environment_test.go +++ b/pkg/cli/environment_test.go @@ -71,12 +71,10 @@ func TestEnvSettings(t *testing.T) { flags := pflag.NewFlagSet("testing", pflag.ContinueOnError) - settings := &EnvSettings{} + settings := New() settings.AddFlags(flags) flags.Parse(strings.Split(tt.args, " ")) - settings.Init(flags) - if settings.Debug != tt.debug { t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug) } @@ -94,7 +92,7 @@ func resetEnv() func() { origEnv := os.Environ() // ensure any local envvars do not hose us - for _, e := range envMap { + for e := range New().EnvVars() { os.Unsetenv(e) } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 35da606ea..cfa665d21 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -26,7 +26,6 @@ import ( "sigs.k8s.io/yaml" "helm.sh/helm/pkg/cli" - "helm.sh/helm/pkg/helmpath" ) const pluginFileName = "plugin.yaml" @@ -217,20 +216,10 @@ func FindPlugins(plugdirs string) ([]*Plugin, error) { // the plugin subsystem itself needs access to the environment variables // created here. func SetupPluginEnv(settings *cli.EnvSettings, name, base string) { - for key, val := range map[string]string{ - "HELM_PLUGIN_NAME": name, - "HELM_PLUGIN_DIR": base, - "HELM_BIN": os.Args[0], - "HELM_PLUGIN": settings.PluginsDirectory, - - // Set vars that convey common information. - "HELM_REGISTRY_CONFIG": settings.RegistryConfig, - "HELM_REPOSITORY_CONFIG": settings.RepositoryConfig, - "HELM_REPOSITORY_CACHE": settings.RepositoryCache, - "HELM_PATH_STARTER": helmpath.DataPath("starters"), - "HELM_HOME": helmpath.DataPath(), // for backwards compatibility with Helm 2 plugins - "HELM_DEBUG": fmt.Sprint(settings.Debug), - } { + env := settings.EnvVars() + env["HELM_PLUGIN_NAME"] = name + env["HELM_PLUGIN_DIR"] = base + for key, val := range env { os.Setenv(key, val) } }