From bc2cd3c794aa50e4401a64bc499e2b914064f787 Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Sat, 26 Oct 2019 23:16:28 -0500 Subject: [PATCH 1/3] Allow namespace to be set by programs consuming helm. Signed-off-by: Aaron Mell --- cmd/helm/install.go | 2 +- cmd/helm/lint.go | 2 +- cmd/helm/release_testing.go | 2 +- cmd/helm/upgrade.go | 2 +- pkg/action/action.go | 2 +- pkg/cli/environment.go | 17 +++++++++-------- pkg/cli/environment_test.go | 4 ++-- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index c80f8875d..32719aba6 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 = settings.Namespace() + client.Namespace = settings.GetNamespace() return client.Run(chartRequested, vals) } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 9a2e8d31c..302a4b227 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 = settings.Namespace() + client.Namespace = settings.GetNamespace() vals, err := valueOpts.MergeValues(getter.All(settings)) if err != nil { return err diff --git a/cmd/helm/release_testing.go b/cmd/helm/release_testing.go index 7190ec736..4c962ccd1 100644 --- a/cmd/helm/release_testing.go +++ b/cmd/helm/release_testing.go @@ -46,7 +46,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command Long: releaseTestHelp, Args: require.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - client.Namespace = settings.Namespace() + client.Namespace = settings.GetNamespace() rel, runErr := client.Run(args[0]) // We only return an error if we weren't even able to get the // release, otherwise we keep going so we can print status and logs diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 282ddff0a..caaa6aca7 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 = settings.Namespace() + client.Namespace = settings.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 653a57830..e037eb971 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -222,7 +222,7 @@ func (c *Configuration) Init(envSettings *cli.EnvSettings, allNamespaces bool, h } var namespace string if !allNamespaces { - namespace = envSettings.Namespace() + namespace = envSettings.GetNamespace() } var store *storage.Storage diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index 28e7873be..57c8e2c0c 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -38,10 +38,11 @@ import ( // EnvSettings describes all of the environment settings. type EnvSettings struct { - namespace string config genericclioptions.RESTClientGetter configOnce sync.Once + // Namespace is the namespace used by storage drivers + Namespace string // KubeConfig is the path to the kubeconfig file KubeConfig string // KubeContext is the name of the kubeconfig context. @@ -61,7 +62,7 @@ type EnvSettings struct { func New() *EnvSettings { env := EnvSettings{ - namespace: os.Getenv("HELM_NAMESPACE"), + Namespace: os.Getenv("HELM_NAMESPACE"), KubeContext: os.Getenv("HELM_KUBECONTEXT"), PluginsDirectory: envOr("HELM_PLUGINS", helmpath.DataPath("plugins")), RegistryConfig: envOr("HELM_REGISTRY_CONFIG", helmpath.ConfigPath("registry.json")), @@ -74,7 +75,7 @@ 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.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", s.KubeContext, "name of the kubeconfig context to use") fs.BoolVar(&s.Debug, "debug", s.Debug, "enable verbose output") @@ -98,7 +99,7 @@ func (s *EnvSettings) EnvVars() map[string]string { "HELM_REGISTRY_CONFIG": s.RegistryConfig, "HELM_REPOSITORY_CACHE": s.RepositoryCache, "HELM_REPOSITORY_CONFIG": s.RepositoryConfig, - "HELM_NAMESPACE": s.Namespace(), + "HELM_NAMESPACE": s.GetNamespace(), "HELM_KUBECONTEXT": s.KubeContext, } @@ -110,9 +111,9 @@ func (s *EnvSettings) EnvVars() map[string]string { } //Namespace gets the namespace from the configuration -func (s *EnvSettings) Namespace() string { - if s.namespace != "" { - return s.namespace +func (s *EnvSettings) GetNamespace() string { + if s.Namespace != "" { + return s.Namespace } if ns, _, err := s.RESTClientGetter().ToRawKubeConfigLoader().Namespace(); err == nil { @@ -124,7 +125,7 @@ func (s *EnvSettings) Namespace() string { //RESTClientGetter gets the kubeconfig from EnvSettings func (s *EnvSettings) RESTClientGetter() genericclioptions.RESTClientGetter { s.configOnce.Do(func() { - s.config = kube.GetConfig(s.KubeConfig, s.KubeContext, s.namespace) + s.config = kube.GetConfig(s.KubeConfig, s.KubeContext, s.Namespace) }) return s.config } diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go index d6856dd01..60fe477a5 100644 --- a/pkg/cli/environment_test.go +++ b/pkg/cli/environment_test.go @@ -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.GetNamespace() != tt.ns { + t.Errorf("expected namespace %q, got %q", tt.ns, settings.GetNamespace()) } if settings.KubeContext != tt.kcontext { t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext) From eab9d2817db1975a92fa1c50d3f154e00d34e445 Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Mon, 28 Oct 2019 12:15:53 -0500 Subject: [PATCH 2/3] Revert "Allow namespace to be set by programs consuming helm." bc2cd3c794aa50e4401a64bc499e2b914064f787 Signed-off-by: Aaron Mell --- cmd/helm/install.go | 2 +- cmd/helm/lint.go | 2 +- cmd/helm/release_testing.go | 2 +- cmd/helm/upgrade.go | 2 +- pkg/action/action.go | 2 +- pkg/cli/environment.go | 17 ++++++++--------- pkg/cli/environment_test.go | 4 ++-- 7 files changed, 15 insertions(+), 16 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index 32719aba6..c80f8875d 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 = settings.GetNamespace() + client.Namespace = settings.Namespace() return client.Run(chartRequested, vals) } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 302a4b227..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 = settings.GetNamespace() + client.Namespace = settings.Namespace() vals, err := valueOpts.MergeValues(getter.All(settings)) if err != nil { return err diff --git a/cmd/helm/release_testing.go b/cmd/helm/release_testing.go index 4c962ccd1..7190ec736 100644 --- a/cmd/helm/release_testing.go +++ b/cmd/helm/release_testing.go @@ -46,7 +46,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command Long: releaseTestHelp, Args: require.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - client.Namespace = settings.GetNamespace() + client.Namespace = settings.Namespace() rel, runErr := client.Run(args[0]) // We only return an error if we weren't even able to get the // release, otherwise we keep going so we can print status and logs diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index caaa6aca7..282ddff0a 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 = settings.GetNamespace() + 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 e037eb971..653a57830 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -222,7 +222,7 @@ func (c *Configuration) Init(envSettings *cli.EnvSettings, allNamespaces bool, h } var namespace string if !allNamespaces { - namespace = envSettings.GetNamespace() + namespace = envSettings.Namespace() } var store *storage.Storage diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index 57c8e2c0c..28e7873be 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -38,11 +38,10 @@ import ( // EnvSettings describes all of the environment settings. type EnvSettings struct { + namespace string config genericclioptions.RESTClientGetter configOnce sync.Once - // Namespace is the namespace used by storage drivers - Namespace string // KubeConfig is the path to the kubeconfig file KubeConfig string // KubeContext is the name of the kubeconfig context. @@ -62,7 +61,7 @@ type EnvSettings struct { func New() *EnvSettings { env := EnvSettings{ - Namespace: os.Getenv("HELM_NAMESPACE"), + namespace: os.Getenv("HELM_NAMESPACE"), KubeContext: os.Getenv("HELM_KUBECONTEXT"), PluginsDirectory: envOr("HELM_PLUGINS", helmpath.DataPath("plugins")), RegistryConfig: envOr("HELM_REGISTRY_CONFIG", helmpath.ConfigPath("registry.json")), @@ -75,7 +74,7 @@ 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.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", s.KubeContext, "name of the kubeconfig context to use") fs.BoolVar(&s.Debug, "debug", s.Debug, "enable verbose output") @@ -99,7 +98,7 @@ func (s *EnvSettings) EnvVars() map[string]string { "HELM_REGISTRY_CONFIG": s.RegistryConfig, "HELM_REPOSITORY_CACHE": s.RepositoryCache, "HELM_REPOSITORY_CONFIG": s.RepositoryConfig, - "HELM_NAMESPACE": s.GetNamespace(), + "HELM_NAMESPACE": s.Namespace(), "HELM_KUBECONTEXT": s.KubeContext, } @@ -111,9 +110,9 @@ func (s *EnvSettings) EnvVars() map[string]string { } //Namespace gets the namespace from the configuration -func (s *EnvSettings) GetNamespace() string { - if s.Namespace != "" { - return s.Namespace +func (s *EnvSettings) Namespace() string { + if s.namespace != "" { + return s.namespace } if ns, _, err := s.RESTClientGetter().ToRawKubeConfigLoader().Namespace(); err == nil { @@ -125,7 +124,7 @@ func (s *EnvSettings) GetNamespace() string { //RESTClientGetter gets the kubeconfig from EnvSettings func (s *EnvSettings) RESTClientGetter() genericclioptions.RESTClientGetter { s.configOnce.Do(func() { - s.config = kube.GetConfig(s.KubeConfig, s.KubeContext, s.Namespace) + s.config = kube.GetConfig(s.KubeConfig, s.KubeContext, s.namespace) }) return s.config } diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go index 60fe477a5..d6856dd01 100644 --- a/pkg/cli/environment_test.go +++ b/pkg/cli/environment_test.go @@ -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.GetNamespace() != tt.ns { - t.Errorf("expected namespace %q, got %q", tt.ns, settings.GetNamespace()) + 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 31f2fea06130c4c18e9bb303956ed35f14242b9a Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Mon, 28 Oct 2019 12:33:55 -0500 Subject: [PATCH 3/3] Passing the namespace to actionconfig.Init instead of the entire object. Signed-off-by: Aaron Mell --- cmd/helm/helm.go | 2 +- cmd/helm/list.go | 2 +- pkg/action/action.go | 10 ++-------- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 0e935addb..078cc3c5e 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -67,7 +67,7 @@ func main() { actionConfig := new(action.Configuration) cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:]) - if err := actionConfig.Init(settings, false, os.Getenv("HELM_DRIVER"), debug); err != nil { + if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), debug); err != nil { debug("%+v", err) switch e := err.(type) { case pluginError: diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 2200c153b..f80a81b9a 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -70,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 { - if err := cfg.Init(settings, true, os.Getenv("HELM_DRIVER"), debug); err != nil { + if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil { return err } } diff --git a/pkg/action/action.go b/pkg/action/action.go index 653a57830..48d6bf742 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -22,13 +22,13 @@ import ( "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" @@ -210,9 +210,7 @@ func (c *Configuration) recordRelease(r *release.Release) { } // InitActionConfig initializes the action configuration -func (c *Configuration) Init(envSettings *cli.EnvSettings, allNamespaces bool, helmDriver string, log DebugLog) error { - getter := envSettings.RESTClientGetter() - +func (c *Configuration) Init(getter genericclioptions.RESTClientGetter, namespace string, helmDriver string, log DebugLog) error { kc := kube.New(getter) kc.Log = log @@ -220,10 +218,6 @@ func (c *Configuration) Init(envSettings *cli.EnvSettings, allNamespaces bool, h if err != nil { return err } - var namespace string - if !allNamespaces { - namespace = envSettings.Namespace() - } var store *storage.Storage switch helmDriver {