ref(*): move kubeconfig flags to helm/environment

pull/3945/head
Adam Reese 6 years ago
parent 2a97768b5c
commit 1a508ccdd1
No known key found for this signature in database
GPG Key ID: 06F35E60A7A18DD6

@ -62,9 +62,7 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
return errReleaseRequired
}
get.release = args[0]
if get.client == nil {
get.client = newClient()
}
get.client = ensureHelmClient(get.client)
return get.run()
},
}

@ -21,6 +21,7 @@ import (
"log"
"os"
"strings"
"sync"
"github.com/spf13/cobra"
// Import to initialize client auth plugins.
@ -34,8 +35,9 @@ import (
)
var (
settings helm_env.EnvSettings
config clientcmd.ClientConfig
settings helm_env.EnvSettings
config clientcmd.ClientConfig
configOnce sync.Once
)
var globalUsage = `The Kubernetes package manager
@ -70,8 +72,6 @@ func newRootCmd(args []string) *cobra.Command {
settings.AddFlags(flags)
config = kube.GetConfig(flags)
out := cmd.OutOrStdout()
cmd.AddCommand(
@ -159,7 +159,7 @@ func ensureHelmClient(h helm.Interface) helm.Interface {
}
func newClient() helm.Interface {
kc := kube.New(config)
kc := kube.New(kubeConfig())
kc.Log = logf
clientset, err := kc.KubernetesClientSet()
@ -178,8 +178,15 @@ func newClient() helm.Interface {
)
}
func kubeConfig() clientcmd.ClientConfig {
configOnce.Do(func() {
config = kube.GetConfig(settings.KubeConfig, settings.KubeContext, settings.Namespace)
})
return config
}
func getNamespace() string {
if ns, _, err := config.Namespace(); err == nil {
if ns, _, err := kubeConfig().Namespace(); err == nil {
return ns
}
return "default"

@ -74,12 +74,10 @@ func newHistoryCmd(c helm.Interface, w io.Writer) *cobra.Command {
Short: "fetch release history",
Aliases: []string{"hist"},
RunE: func(cmd *cobra.Command, args []string) error {
switch {
case len(args) == 0:
if len(args) == 0 {
return errReleaseRequired
case his.helmc == nil:
his.helmc = newClient()
}
his.helmc = ensureHelmClient(his.helmc)
his.rls = args[0]
return his.run()
},

@ -24,6 +24,7 @@ import (
"testing"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
)

@ -90,9 +90,7 @@ func newListCmd(client helm.Interface, out io.Writer) *cobra.Command {
if len(args) > 0 {
list.filter = strings.Join(args, " ")
}
if list.client == nil {
list.client = newClient()
}
list.client = ensureHelmClient(list.client)
return list.run()
},
}

@ -67,9 +67,7 @@ func newStatusCmd(client helm.Interface, out io.Writer) *cobra.Command {
return errReleaseRequired
}
status.release = args[0]
if status.client == nil {
status.client = newClient()
}
status.client = ensureHelmClient(status.client)
return status.run()
},
}

@ -32,20 +32,29 @@ import (
"k8s.io/helm/pkg/helm/helmpath"
)
// DefaultHelmHome is the default HELM_HOME.
var DefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
// defaultHelmHome is the default HELM_HOME.
var defaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
// EnvSettings describes all of the environment settings.
type EnvSettings struct {
// Home is the local path to the Helm home directory.
Home helmpath.Home
// 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.
Debug bool
}
// AddFlags binds flags to the given flagset.
func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
fs.StringVar((*string)(&s.Home), "home", DefaultHelmHome, "location of your Helm config. Overrides $HELM_HOME")
fs.StringVar((*string)(&s.Home), "home", defaultHelmHome, "location of your Helm config. Overrides $HELM_HOME")
fs.StringVarP(&s.Namespace, "namespace", "n", "", "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", false, "enable verbose output")
}
@ -66,8 +75,9 @@ func (s EnvSettings) PluginDirs() string {
// envMap maps flag names to envvars
var envMap = map[string]string{
"debug": "HELM_DEBUG",
"home": "HELM_HOME",
"debug": "HELM_DEBUG",
"home": "HELM_HOME",
"namespace": "HELM_NAMESPACE",
}
func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) {

@ -31,23 +31,22 @@ func TestEnvSettings(t *testing.T) {
name string
// input
args []string
args string
envars map[string]string
// expected values
home, ns, plugins string
debug bool
home, ns, kcontext, plugins string
debug bool
}{
{
name: "defaults",
args: []string{},
home: DefaultHelmHome,
plugins: helmpath.Home(DefaultHelmHome).Plugins(),
ns: "kube-system",
home: defaultHelmHome,
plugins: helmpath.Home(defaultHelmHome).Plugins(),
ns: "",
},
{
name: "with flags set",
args: []string{"--home", "/foo", "--debug"},
args: "--home /foo --debug --namespace=myns",
home: "/foo",
plugins: helmpath.Home("/foo").Plugins(),
ns: "myns",
@ -55,8 +54,7 @@ func TestEnvSettings(t *testing.T) {
},
{
name: "with envvars set",
args: []string{},
envars: map[string]string{"HELM_HOME": "/bar", "HELM_DEBUG": "1"},
envars: map[string]string{"HELM_HOME": "/bar", "HELM_DEBUG": "1", "HELM_NAMESPACE": "yourns"},
home: "/bar",
plugins: helmpath.Home("/bar").Plugins(),
ns: "yourns",
@ -64,8 +62,8 @@ func TestEnvSettings(t *testing.T) {
},
{
name: "with flags and envvars set",
args: []string{"--home", "/foo", "--debug"},
envars: map[string]string{"HELM_HOME": "/bar", "HELM_DEBUG": "1", "HELM_PLUGIN": "glade"},
args: "--home /foo --debug --namespace=myns",
envars: map[string]string{"HELM_HOME": "/bar", "HELM_DEBUG": "1", "HELM_NAMESPACE": "yourns", "HELM_PLUGIN": "glade"},
home: "/foo",
plugins: "glade",
ns: "myns",
@ -86,7 +84,7 @@ func TestEnvSettings(t *testing.T) {
settings := &EnvSettings{}
settings.AddFlags(flags)
flags.Parse(tt.args)
flags.Parse(strings.Split(tt.args, " "))
settings.Init(flags)
@ -99,6 +97,12 @@ 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.KubeContext != tt.kcontext {
t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext)
}
cleanup()
})

@ -16,21 +16,17 @@ limitations under the License.
package kube // import "k8s.io/helm/pkg/kube"
import (
"github.com/spf13/pflag"
"k8s.io/client-go/tools/clientcmd"
)
import "k8s.io/client-go/tools/clientcmd"
// GetConfig returns a Kubernetes client config for a given context.
func GetConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
// GetConfig returns a Kubernetes client config.
func GetConfig(kubeconfig, context, namespace string) clientcmd.ClientConfig {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.DefaultClientConfig = &clientcmd.DefaultClientConfig
flags.StringVar(&rules.ExplicitPath, "kubeconfig", "", "path to the kubeconfig file to use for CLI requests")
rules.ExplicitPath = kubeconfig
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
flags.StringVarP(&overrides.Context.Namespace, "namespace", "n", "", "if present, the namespace scope for this CLI request")
flags.StringVar(&overrides.CurrentContext, "context", "", "the name of the kubeconfig context to use")
overrides.CurrentContext = context
overrides.Context.Namespace = namespace
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides)
}

Loading…
Cancel
Save