From a772076456d5a2747a3dfec23f805e07aab59254 Mon Sep 17 00:00:00 2001 From: "mike.du" Date: Thu, 21 Nov 2019 15:49:36 +0800 Subject: [PATCH] feat expose .namespace public as Namespace, to provide easier integration for non-cli projects Signed-off-by: mike.du --- cmd/helm/helm.go | 2 +- cmd/helm/install.go | 2 +- cmd/helm/install_test.go | 13 ++++++++++ cmd/helm/lint.go | 2 +- cmd/helm/release_testing.go | 2 +- .../output/install-with-ns-setting.txt | 6 +++++ cmd/helm/upgrade.go | 2 +- pkg/cli/environment.go | 25 ++++++++----------- pkg/cli/environment_test.go | 4 +-- 9 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 cmd/helm/testdata/output/install-with-ns-setting.txt diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 2c3c9e401..566bf944a 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.RESTClientGetter(), settings.Namespace(), 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) os.Exit(1) } diff --git a/cmd/helm/install.go b/cmd/helm/install.go index 40935db17..a04ff5fec 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.Namespace return client.Run(chartRequested, vals) } diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index 9ab25417b..880377270 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -186,4 +186,17 @@ func TestInstall(t *testing.T) { } runTestActionCmd(t, tests) + + settings.Namespace = "halo" + nsTests := []cmdTestCase{ + // Install to halo ns without --namespace flag + { + name: "install to halo ns without --namespace flag", + cmd: "install halo testdata/testcharts/empty", + //wantError: true, + golden: "output/install-with-ns-setting.txt", + }, + } + + runTestActionCmd(t, nsTests) } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 9a2e8d31c..407da1ea4 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.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 7190ec736..9221d5529 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.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/testdata/output/install-with-ns-setting.txt b/cmd/helm/testdata/output/install-with-ns-setting.txt new file mode 100644 index 000000000..8c56a98e1 --- /dev/null +++ b/cmd/helm/testdata/output/install-with-ns-setting.txt @@ -0,0 +1,6 @@ +NAME: halo +LAST DEPLOYED: Fri Sep 2 22:04:05 1977 +NAMESPACE: halo +STATUS: deployed +REVISION: 1 +TEST SUITE: None diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index eadc3b63d..fc9b40f8b 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.Namespace if client.Version == "" && client.Devel { debug("setting version to >0.0.0-0") diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index c63e420ea..0691ac9f4 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -38,7 +38,7 @@ import ( // EnvSettings describes all of the environment settings. type EnvSettings struct { - namespace string + Namespace string config genericclioptions.RESTClientGetter configOnce sync.Once @@ -61,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")), @@ -74,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") @@ -98,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.Namespace(), + "HELM_NAMESPACE": s.Namespace, "HELM_KUBECONTEXT": s.KubeContext, } @@ -109,27 +109,24 @@ func (s *EnvSettings) EnvVars() map[string]string { return envvars } -//Namespace gets the namespace from the configuration -func (s *EnvSettings) Namespace() string { - if s.namespace != "" { - return s.namespace +//GetNamespace return the namespace from configuration +func (s *EnvSettings) GetNamespace() string { + + if s.Namespace != "" { + return s.Namespace } if ns, _, err := s.RESTClientGetter().ToRawKubeConfigLoader().Namespace(); err == nil { return ns } - return "default" -} -//set the namespace of envsetting, provide easier integration for non-cli projects -func (s *EnvSettings) SetNameSpace(ns string) { - s.namespace = ns + return "default" } //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)