From 1ca2ab1d8d5da9592d8160ce04674ebe08a077bc Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 10 Oct 2019 13:34:24 -0500 Subject: [PATCH 1/7] Moving actionInit from cmd/helm/helm to pgk/action/action to make it easier to instantiate the configuration Signed-off-by: Aaron Mell --- cmd/helm/helm.go | 74 ++++----------------------------------- cmd/helm/install.go | 2 +- cmd/helm/lint.go | 2 +- cmd/helm/list.go | 3 +- cmd/helm/upgrade.go | 2 +- pkg/action/action.go | 83 +++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 91 insertions(+), 75 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 509ab3241..c309edf71 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -22,11 +22,9 @@ import ( "log" "os" "strings" - "sync" "github.com/spf13/cobra" "github.com/spf13/pflag" - "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/klog" // Import to initialize client auth plugins. @@ -35,18 +33,13 @@ import ( "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/gates" - "helm.sh/helm/v3/pkg/kube" - "helm.sh/helm/v3/pkg/storage" - "helm.sh/helm/v3/pkg/storage/driver" ) // FeatureGateOCI is the feature gate for checking if `helm chart` and `helm registry` commands should work const FeatureGateOCI = gates.Gate("HELM_EXPERIMENTAL_OCI") var ( - settings = cli.New() - config genericclioptions.RESTClientGetter - configOnce sync.Once + settings = cli.New() ) func init() { @@ -71,13 +64,9 @@ func initKubeLogs() { func main() { initKubeLogs() - actionConfig := new(action.Configuration) - cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:]) - - // Initialize the rest of the actionConfig - initActionConfig(actionConfig, false) + actionConfig, err := action.InitActionConfig(settings, false, os.Getenv("HELM_DRIVER"), debug) - if err := cmd.Execute(); err != nil { + if err != nil { debug("%+v", err) switch e := err.(type) { case pluginError: @@ -86,62 +75,13 @@ func main() { os.Exit(1) } } -} - -func initActionConfig(actionConfig *action.Configuration, allNamespaces bool) { - kc := kube.New(kubeConfig()) - kc.Log = debug - - clientset, err := kc.Factory.KubernetesClientSet() - if err != nil { - // TODO return error - log.Fatal(err) - } - var namespace string - if !allNamespaces { - namespace = getNamespace() - } - var store *storage.Storage - switch os.Getenv("HELM_DRIVER") { - case "secret", "secrets", "": - d := driver.NewSecrets(clientset.CoreV1().Secrets(namespace)) - d.Log = debug - store = storage.Init(d) - case "configmap", "configmaps": - d := driver.NewConfigMaps(clientset.CoreV1().ConfigMaps(namespace)) - d.Log = debug - store = storage.Init(d) - case "memory": - d := driver.NewMemory() - store = storage.Init(d) - default: - // Not sure what to do here. - panic("Unknown driver in HELM_DRIVER: " + os.Getenv("HELM_DRIVER")) - } - - actionConfig.RESTClientGetter = kubeConfig() - actionConfig.KubeClient = kc - actionConfig.Releases = store - actionConfig.Log = debug -} - -func kubeConfig() genericclioptions.RESTClientGetter { - configOnce.Do(func() { - config = kube.GetConfig(settings.KubeConfig, settings.KubeContext, settings.Namespace) - }) - return config -} - -func getNamespace() string { - if settings.Namespace != "" { - return settings.Namespace - } + cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:]) - if ns, _, err := kubeConfig().ToRawKubeConfigLoader().Namespace(); err == nil { - return ns + if err := cmd.Execute(); err != nil { + debug("%+v", err) + os.Exit(1) } - return "default" } // wordSepNormalizeFunc changes all flags that contain "_" separators diff --git a/cmd/helm/install.go b/cmd/helm/install.go index 2540558a5..c0aaad653 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -205,7 +205,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options } } - client.Namespace = getNamespace() + client.Namespace = action.GetNamespace() return client.Run(chartRequested, vals) } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 13395c6d6..11dc2dc3f 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -51,7 +51,7 @@ func newLintCmd(out io.Writer) *cobra.Command { if len(args) > 0 { paths = args } - client.Namespace = getNamespace() + client.Namespace = action.GetNamespace() vals, err := valueOpts.MergeValues(getter.All(settings)) if err != nil { return err diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 8c89425f5..50f9a7441 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -19,6 +19,7 @@ package main import ( "fmt" "io" + "os" "strconv" "github.com/gosuri/uitable" @@ -69,7 +70,7 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Args: require.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if client.AllNamespaces { - initActionConfig(cfg, true) + action.InitActionConfig(settings, true, os.Getenv("HELM_DRIVER"), debug) } client.SetStateMask() diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 33631504f..3dfc128e8 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -71,7 +71,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Long: upgradeDesc, Args: require.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - client.Namespace = getNamespace() + client.Namespace = action.GetNamespace() if client.Version == "" && client.Devel { debug("setting version to >0.0.0-0") diff --git a/pkg/action/action.go b/pkg/action/action.go index f784b3651..063f4cde7 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -19,19 +19,23 @@ package action import ( "path" "regexp" + "sync" "time" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/discovery" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/pkg/chartutil" + "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/kube" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/storage" + "helm.sh/helm/pkg/storage/driver" ) // Timestamper is a function capable of producing a timestamp.Timestamper. @@ -49,6 +53,10 @@ var ( errInvalidRevision = errors.New("invalid release revision") // errInvalidName indicates that an invalid release name was provided errInvalidName = errors.New("invalid release name, must match regex ^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])+$ and the length must not longer than 53") + + config genericclioptions.RESTClientGetter + configOnce sync.Once + settings *cli.EnvSettings ) // ValidName is a regular expression for names. @@ -82,6 +90,15 @@ type Configuration struct { Log func(string, ...interface{}) } +// RESTClientGetter gets the rest client +type RESTClientGetter interface { + ToRESTConfig() (*rest.Config, error) + ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) + ToRESTMapper() (meta.RESTMapper, error) +} + +type debug func(format string, v ...interface{}) + // capabilities builds a Capabilities from discovery information. func (c *Configuration) getCapabilities() (*chartutil.Capabilities, error) { if c.Capabilities != nil { @@ -197,8 +214,66 @@ func (c *Configuration) recordRelease(r *release.Release) { } } -type RESTClientGetter interface { - ToRESTConfig() (*rest.Config, error) - ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) - ToRESTMapper() (meta.RESTMapper, error) +// InitActionConfig initializes the action configuration +func InitActionConfig(envsettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log debug) (*Configuration, error) { + settings = envsettings + + var actionConfig Configuration + kubeconfig := kubeConfig() + + kc := kube.New(kubeconfig) + kc.Log = log + + clientset, err := kc.Factory.KubernetesClientSet() + if err != nil { + return nil, err + } + var namespace string + if !allNamespaces { + namespace = GetNamespace() + } + + var store *storage.Storage + switch helmDriver { + case "secret", "secrets", "": + d := driver.NewSecrets(clientset.CoreV1().Secrets(namespace)) + d.Log = log + store = storage.Init(d) + case "configmap", "configmaps": + d := driver.NewConfigMaps(clientset.CoreV1().ConfigMaps(namespace)) + d.Log = log + store = storage.Init(d) + case "memory": + d := driver.NewMemory() + store = storage.Init(d) + default: + // Not sure what to do here. + panic("Unknown driver in HELM_DRIVER: " + helmDriver) + } + + actionConfig.RESTClientGetter = kubeconfig + actionConfig.KubeClient = kc + actionConfig.Releases = store + actionConfig.Log = log + + return &actionConfig, nil +} + +func kubeConfig() genericclioptions.RESTClientGetter { + configOnce.Do(func() { + config = kube.GetConfig(settings.KubeConfig, settings.KubeContext, settings.Namespace) + }) + return config +} + +//GetNamespace gets the namespace from the configuration +func GetNamespace() string { + if envSettings.Namespace != "" { + return envSettings.Namespace + } + + if ns, _, err := kubeConfig(envSettings).ToRawKubeConfigLoader().Namespace(); err == nil { + return ns + } + return "default" } From 851e016e90811f03b6048b1de101bd1220bb3e3f Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Tue, 8 Oct 2019 08:36:57 -0500 Subject: [PATCH 2/7] Reverted previous commit, changes based on code review feedback. Signed-off-by: Aaron Mell --- cmd/helm/install.go | 2 +- cmd/helm/lint.go | 2 +- pkg/action/action.go | 14 ++++++-------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index c0aaad653..864a13860 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -205,7 +205,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options } } - client.Namespace = action.GetNamespace() + client.Namespace = action.GetNamespace(settings) return client.Run(chartRequested, vals) } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 11dc2dc3f..c259b1f37 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -51,7 +51,7 @@ func newLintCmd(out io.Writer) *cobra.Command { if len(args) > 0 { paths = args } - client.Namespace = action.GetNamespace() + client.Namespace = action.GetNamespace(settings) vals, err := valueOpts.MergeValues(getter.All(settings)) if err != nil { return err diff --git a/pkg/action/action.go b/pkg/action/action.go index 063f4cde7..e12698c11 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -56,7 +56,6 @@ var ( config genericclioptions.RESTClientGetter configOnce sync.Once - settings *cli.EnvSettings ) // ValidName is a regular expression for names. @@ -215,11 +214,10 @@ func (c *Configuration) recordRelease(r *release.Release) { } // InitActionConfig initializes the action configuration -func InitActionConfig(envsettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log debug) (*Configuration, error) { - settings = envsettings +func InitActionConfig(envSettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log debug) (*Configuration, error) { var actionConfig Configuration - kubeconfig := kubeConfig() + kubeconfig := kubeConfig(envSettings) kc := kube.New(kubeconfig) kc.Log = log @@ -230,7 +228,7 @@ func InitActionConfig(envsettings *cli.EnvSettings, allNamespaces bool, helmDriv } var namespace string if !allNamespaces { - namespace = GetNamespace() + namespace = GetNamespace(envSettings) } var store *storage.Storage @@ -259,15 +257,15 @@ func InitActionConfig(envsettings *cli.EnvSettings, allNamespaces bool, helmDriv return &actionConfig, nil } -func kubeConfig() genericclioptions.RESTClientGetter { +func kubeConfig(envSettings *cli.EnvSettings) genericclioptions.RESTClientGetter { configOnce.Do(func() { - config = kube.GetConfig(settings.KubeConfig, settings.KubeContext, settings.Namespace) + config = kube.GetConfig(envSettings.KubeConfig, envSettings.KubeContext, envSettings.Namespace) }) return config } //GetNamespace gets the namespace from the configuration -func GetNamespace() string { +func GetNamespace(envSettings *cli.EnvSettings) string { if envSettings.Namespace != "" { return envSettings.Namespace } From 01d7657c1e9a6f0b00ce63c95ea856a4ea67beaf Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 5 Sep 2019 21:50:15 -0500 Subject: [PATCH 3/7] Another Code review change Signed-off-by: Aaron Mell --- pkg/action/action.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/action/action.go b/pkg/action/action.go index e12698c11..3c5945d5b 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -96,7 +96,8 @@ type RESTClientGetter interface { ToRESTMapper() (meta.RESTMapper, error) } -type debug func(format string, v ...interface{}) +// DebugLog sets the logger that writes debug strings +type DebugLog func(format string, v ...interface{}) // capabilities builds a Capabilities from discovery information. func (c *Configuration) getCapabilities() (*chartutil.Capabilities, error) { @@ -214,7 +215,7 @@ func (c *Configuration) recordRelease(r *release.Release) { } // InitActionConfig initializes the action configuration -func InitActionConfig(envSettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log debug) (*Configuration, error) { +func InitActionConfig(envSettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log DebugLog) (*Configuration, error) { var actionConfig Configuration kubeconfig := kubeConfig(envSettings) From 3264b753785403e9cb6d47d7c9099e29b25cd651 Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 10 Oct 2019 13:35:23 -0500 Subject: [PATCH 4/7] Refactoring after rebasing with latest Signed-off-by: Aaron Mell --- cmd/helm/upgrade.go | 2 +- pkg/action/action.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 3dfc128e8..dbdfff439 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -71,7 +71,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Long: upgradeDesc, Args: require.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - client.Namespace = action.GetNamespace() + client.Namespace = action.GetNamespace(settings) if client.Version == "" && client.Devel { debug("setting version to >0.0.0-0") diff --git a/pkg/action/action.go b/pkg/action/action.go index 3c5945d5b..68e56bffd 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -35,7 +35,7 @@ import ( "helm.sh/helm/v3/pkg/kube" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/storage" - "helm.sh/helm/pkg/storage/driver" + "helm.sh/helm/v3/pkg/storage/driver" ) // Timestamper is a function capable of producing a timestamp.Timestamper. From 1d66a676c858c185df02a1773a6c6718ed840f15 Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 10 Oct 2019 13:35:46 -0500 Subject: [PATCH 5/7] Moved the GetNamespace and KubeConfig function from action to cli Signed-off-by: Aaron Mell --- cmd/helm/install.go | 2 +- cmd/helm/lint.go | 2 +- cmd/helm/upgrade.go | 2 +- pkg/action/action.go | 28 ++-------------------- pkg/cli/environment.go | 47 +++++++++++++++++++++++++++++++------ pkg/cli/environment_test.go | 6 ++--- 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index 864a13860..e29fbbda1 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -205,7 +205,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options } } - client.Namespace = action.GetNamespace(settings) + client.Namespace = settings.Namespace() return client.Run(chartRequested, vals) } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index c259b1f37..9a2e8d31c 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -51,7 +51,7 @@ func newLintCmd(out io.Writer) *cobra.Command { if len(args) > 0 { paths = args } - client.Namespace = action.GetNamespace(settings) + client.Namespace = settings.Namespace() vals, err := valueOpts.MergeValues(getter.All(settings)) if err != nil { return err diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index dbdfff439..22dd23970 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -71,7 +71,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Long: upgradeDesc, Args: require.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - client.Namespace = action.GetNamespace(settings) + client.Namespace = settings.Namespace() if client.Version == "" && client.Devel { debug("setting version to >0.0.0-0") diff --git a/pkg/action/action.go b/pkg/action/action.go index 68e56bffd..7cfe4a7d3 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -19,12 +19,10 @@ package action import ( "path" "regexp" - "sync" "time" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/discovery" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -53,9 +51,6 @@ var ( errInvalidRevision = errors.New("invalid release revision") // errInvalidName indicates that an invalid release name was provided errInvalidName = errors.New("invalid release name, must match regex ^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])+$ and the length must not longer than 53") - - config genericclioptions.RESTClientGetter - configOnce sync.Once ) // ValidName is a regular expression for names. @@ -218,7 +213,7 @@ func (c *Configuration) recordRelease(r *release.Release) { func InitActionConfig(envSettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log DebugLog) (*Configuration, error) { var actionConfig Configuration - kubeconfig := kubeConfig(envSettings) + kubeconfig := envSettings.KubeConfig() kc := kube.New(kubeconfig) kc.Log = log @@ -229,7 +224,7 @@ func InitActionConfig(envSettings *cli.EnvSettings, allNamespaces bool, helmDriv } var namespace string if !allNamespaces { - namespace = GetNamespace(envSettings) + namespace = envSettings.Namespace() } var store *storage.Storage @@ -257,22 +252,3 @@ func InitActionConfig(envSettings *cli.EnvSettings, allNamespaces bool, helmDriv return &actionConfig, nil } - -func kubeConfig(envSettings *cli.EnvSettings) genericclioptions.RESTClientGetter { - configOnce.Do(func() { - config = kube.GetConfig(envSettings.KubeConfig, envSettings.KubeContext, envSettings.Namespace) - }) - return config -} - -//GetNamespace gets the namespace from the configuration -func GetNamespace(envSettings *cli.EnvSettings) string { - if envSettings.Namespace != "" { - return envSettings.Namespace - } - - if ns, _, err := kubeConfig(envSettings).ToRawKubeConfigLoader().Namespace(); err == nil { - return ns - } - return "default" -} diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index bbe3e964b..a7aadf4af 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -26,18 +26,20 @@ import ( "fmt" "os" "strconv" + "sync" "github.com/spf13/pflag" "helm.sh/helm/v3/pkg/helmpath" + + "k8s.io/cli-runtime/pkg/genericclioptions" + + "helm.sh/helm/v3/pkg/kube" ) // EnvSettings describes all of the environment settings. type EnvSettings struct { - // Namespace is the namespace scope. - Namespace string - // KubeConfig is the path to the kubeconfig file. - KubeConfig string + // KubeContext is the name of the kubeconfig context. KubeContext string // Debug indicates whether or not Helm is running in Debug mode. @@ -53,9 +55,20 @@ type EnvSettings struct { PluginsDirectory string } +var ( + config genericclioptions.RESTClientGetter + configOnce sync.Once + + // Namespace is the namespace scope. + namespace string + // KubeConfig is the path to the kubeconfig file. + kubeConfig string +) + func New() *EnvSettings { + namespace = os.Getenv("HELM_NAMESPACE") + 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")), @@ -67,8 +80,8 @@ func New() *EnvSettings { // AddFlags binds flags to the given flagset. func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { - fs.StringVarP(&s.Namespace, "namespace", "n", s.Namespace, "namespace scope for this request") - fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file") + fs.StringVarP(&namespace, "namespace", "n", namespace, "namespace scope for this request") + fs.StringVar(&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", s.Debug, "enable verbose output") fs.StringVar(&s.RegistryConfig, "registry-config", s.RegistryConfig, "path to the registry config file") @@ -93,3 +106,23 @@ func (s *EnvSettings) EnvVars() map[string]string { "HELM_REPOSITORY_CONFIG": s.RepositoryConfig, } } + +//Namespace gets the namespace from the configuration +func (s *EnvSettings) Namespace() string { + if namespace != "" { + return namespace + } + + if ns, _, err := s.KubeConfig().ToRawKubeConfigLoader().Namespace(); err == nil { + return ns + } + return "default" +} + +//KubeConfig gets the kubeconfig from EnvSettings +func (s *EnvSettings) KubeConfig() genericclioptions.RESTClientGetter { + configOnce.Do(func() { + config = kube.GetConfig(kubeConfig, s.KubeContext, namespace) + }) + return config +} diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go index 58cf1c7f4..d6856dd01 100644 --- a/pkg/cli/environment_test.go +++ b/pkg/cli/environment_test.go @@ -38,7 +38,7 @@ func TestEnvSettings(t *testing.T) { }{ { name: "defaults", - ns: "", + ns: "default", }, { name: "with flags set", @@ -78,8 +78,8 @@ func TestEnvSettings(t *testing.T) { if settings.Debug != tt.debug { t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug) } - if settings.Namespace != tt.ns { - t.Errorf("expected namespace %q, got %q", tt.ns, settings.Namespace) + if settings.Namespace() != tt.ns { + t.Errorf("expected namespace %q, got %q", tt.ns, settings.Namespace()) } if settings.KubeContext != tt.kcontext { t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext) From 00249a3235cf3656709dbb4473bfa93a8ce6bdfb Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 10 Oct 2019 13:35:54 -0500 Subject: [PATCH 6/7] Moved namespace and kubeconfig variable back to original place. Signed-off-by: Aaron Mell --- pkg/cli/environment.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index a7aadf4af..eca29d591 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -30,21 +30,20 @@ import ( "github.com/spf13/pflag" - "helm.sh/helm/v3/pkg/helmpath" - "k8s.io/cli-runtime/pkg/genericclioptions" + "helm.sh/helm/v3/pkg/helmpath" "helm.sh/helm/v3/pkg/kube" ) // EnvSettings describes all of the environment settings. type EnvSettings struct { - + namespace string + kubeConfig string // KubeContext is the name of the kubeconfig context. KubeContext string // Debug indicates whether or not Helm is running in Debug mode. Debug bool - // RegistryConfig is the path to the registry config file. RegistryConfig string // RepositoryConfig is the path to the repositories file. @@ -58,17 +57,12 @@ type EnvSettings struct { var ( config genericclioptions.RESTClientGetter configOnce sync.Once - - // Namespace is the namespace scope. - namespace string - // KubeConfig is the path to the kubeconfig file. - kubeConfig string ) func New() *EnvSettings { - namespace = os.Getenv("HELM_NAMESPACE") 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")), @@ -80,8 +74,8 @@ func New() *EnvSettings { // AddFlags binds flags to the given flagset. func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { - fs.StringVarP(&namespace, "namespace", "n", namespace, "namespace scope for this request") - fs.StringVar(&kubeConfig, "kubeconfig", "", "path to the kubeconfig file") + 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", s.Debug, "enable verbose output") fs.StringVar(&s.RegistryConfig, "registry-config", s.RegistryConfig, "path to the registry config file") @@ -109,8 +103,8 @@ func (s *EnvSettings) EnvVars() map[string]string { //Namespace gets the namespace from the configuration func (s *EnvSettings) Namespace() string { - if namespace != "" { - return namespace + if s.namespace != "" { + return s.namespace } if ns, _, err := s.KubeConfig().ToRawKubeConfigLoader().Namespace(); err == nil { @@ -122,7 +116,7 @@ func (s *EnvSettings) Namespace() string { //KubeConfig gets the kubeconfig from EnvSettings func (s *EnvSettings) KubeConfig() genericclioptions.RESTClientGetter { configOnce.Do(func() { - config = kube.GetConfig(kubeConfig, s.KubeContext, namespace) + config = kube.GetConfig(s.kubeConfig, s.KubeContext, s.namespace) }) return config } From 8b8ffcdb211b72d915e067fad37fcfb5c9fd2a0b Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 10 Oct 2019 16:46:58 -0500 Subject: [PATCH 7/7] Moved config and configOnce to struct Signed-off-by: Aaron Mell --- pkg/cli/environment.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index eca29d591..76dc31bcf 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -40,6 +40,8 @@ import ( type EnvSettings struct { namespace string kubeConfig string + config genericclioptions.RESTClientGetter + configOnce sync.Once // KubeContext is the name of the kubeconfig context. KubeContext string // Debug indicates whether or not Helm is running in Debug mode. @@ -54,11 +56,6 @@ type EnvSettings struct { PluginsDirectory string } -var ( - config genericclioptions.RESTClientGetter - configOnce sync.Once -) - func New() *EnvSettings { env := EnvSettings{ @@ -115,8 +112,8 @@ func (s *EnvSettings) Namespace() string { //KubeConfig gets the kubeconfig from EnvSettings func (s *EnvSettings) KubeConfig() genericclioptions.RESTClientGetter { - configOnce.Do(func() { - config = kube.GetConfig(s.kubeConfig, s.KubeContext, s.namespace) + s.configOnce.Do(func() { + s.config = kube.GetConfig(s.kubeConfig, s.KubeContext, s.namespace) }) - return config + return s.config }