Merge pull request #6400 from adamreese/ref/envvars

ref(pkg/cli): refactor environment variable setup
pull/6500/head
Adam Reese 5 years ago committed by GitHub
commit fef60966f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}

@ -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

@ -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

@ -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

@ -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,
}
}

@ -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)
}

@ -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)
}
}

Loading…
Cancel
Save