Moved the GetNamespace and KubeConfig function from action to cli

Signed-off-by: Aaron Mell <amell@lumindigital.com>
pull/6341/head
Aaron Mell 5 years ago
parent 3264b75378
commit 1d66a676c8

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

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

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

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

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

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

Loading…
Cancel
Save