From 1d66a676c858c185df02a1773a6c6718ed840f15 Mon Sep 17 00:00:00 2001 From: Aaron Mell Date: Thu, 10 Oct 2019 13:35:46 -0500 Subject: [PATCH] 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)