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

@ -43,11 +43,7 @@ func NewOptions(flags *flag.FlagSet) Options {
func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) { func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) {
var err error var err error
cli := &KubeCli{ cli := &KubeCli{
Cli: dockerCli, Cli: dockerCli,
kubeNamespace: "default",
}
if opts.Namespace != "" {
cli.kubeNamespace = opts.Namespace
} }
kubeConfig := opts.Config kubeConfig := opts.Config
if kubeConfig == "" { if kubeConfig == "" {
@ -57,7 +53,19 @@ func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) {
kubeConfig = filepath.Join(homedir.Get(), ".kube/config") 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 { if err != nil {
return nil, err return nil, err
} }

View File

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

View File

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