Fix using the namespace specified in the kubeconfig file.

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2018-03-30 18:04:38 +02:00 committed by Mathieu Champlon
parent c31b61bff4
commit f0aa126b03
3 changed files with 24 additions and 13 deletions

View File

@ -44,10 +44,6 @@ func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) {
var err error
cli := &KubeCli{
Cli: dockerCli,
kubeNamespace: "default",
}
if opts.Namespace != "" {
cli.kubeNamespace = opts.Namespace
}
kubeConfig := opts.Config
if kubeConfig == "" {
@ -57,7 +53,19 @@ func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) {
kubeConfig = filepath.Join(homedir.Get(), ".kube/config")
}
}
config, err := kubernetes.NewKubernetesConfig(kubeConfig)
clientConfig := kubernetes.NewKubernetesConfig(kubeConfig)
cli.kubeNamespace = opts.Namespace
if opts.Namespace == "default" {
configNamespace, _, err := clientConfig.Namespace()
if err != nil {
return nil, err
}
cli.kubeNamespace = configNamespace
}
config, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}

View File

@ -228,7 +228,8 @@ func getKubernetesVersion(dockerCli command.Cli, kubeConfig string) *kubernetesV
Kubernetes: "Unknown",
StackAPI: "Unknown",
}
config, err := kubernetes.NewKubernetesConfig(kubeConfig)
clientConfig := kubernetes.NewKubernetesConfig(kubeConfig)
config, err := clientConfig.ClientConfig()
if err != nil {
logrus.Debugf("failed to get Kubernetes configuration: %s", err)
return &version

View File

@ -5,14 +5,13 @@ import (
"path/filepath"
"github.com/docker/docker/pkg/homedir"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// NewKubernetesConfig resolves the path to the desired Kubernetes configuration file, depending
// environment variable and command line flag.
func NewKubernetesConfig(configFlag string) (*restclient.Config, error) {
kubeConfig := configFlag
// NewKubernetesConfig resolves the path to the desired Kubernetes configuration file based on
// the KUBECONFIG environment variable and command line flags.
func NewKubernetesConfig(configPath string) clientcmd.ClientConfig {
kubeConfig := configPath
if kubeConfig == "" {
if config := os.Getenv("KUBECONFIG"); config != "" {
kubeConfig = config
@ -20,5 +19,8 @@ func NewKubernetesConfig(configFlag string) (*restclient.Config, error) {
kubeConfig = filepath.Join(homedir.Get(), ".kube/config")
}
}
return clientcmd.BuildConfigFromFlags("", kubeConfig)
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig},
&clientcmd.ConfigOverrides{})
}