diff --git a/cmd/deployment.go b/cmd/deployment.go index 86c0d28..5f4682b 100644 --- a/cmd/deployment.go +++ b/cmd/deployment.go @@ -72,7 +72,7 @@ func printDeploymentView() { } func deploymentInfo() []DeploymentInfo { - clientset := util.GetClientset(kubeconfig) + clientset := util.GetClientset(kubeconfig, context) depList, err := clientset.AppsV1beta2().Deployments(namespace).List(metav1.ListOptions{}) if err != nil { diff --git a/cmd/nodes.go b/cmd/nodes.go index 27a02d6..fedfc3d 100644 --- a/cmd/nodes.go +++ b/cmd/nodes.go @@ -81,7 +81,7 @@ func NewPodStatus(pod v1.Pod) PodStatus { } func nodeMap() map[string]NodePodInfo { - clientset := util.GetClientset(kubeconfig) + clientset := util.GetClientset(kubeconfig, context) podList, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{}) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 6f612a7..98aafe5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -65,13 +65,4 @@ func initConfig() { fmt.Println("Using config file:", viper.ConfigFileUsed()) } - if kubeconfig == "" { - if kenv := os.Getenv("KUBECONFIG"); kenv != "" { - kubeconfig = kenv - } else if h := os.Getenv("HOME"); h != "" { - kubeconfig = fmt.Sprintf("%v/.kube/config", h) - } else { - panic(fmt.Errorf("error setting default kubeconfig. $HOME not set")) - } - } } diff --git a/cmd/service.go b/cmd/service.go index 1dd2063..21da646 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -53,7 +53,7 @@ func showServiceView() { } func getServiceInfo() []ServiceInfo { - clientset := util.GetClientset(kubeconfig) + clientset := util.GetClientset(kubeconfig, context) serviceList, err := clientset.CoreV1().Services(namespace).List(metav1.ListOptions{}) if err != nil { diff --git a/util/k8s.go b/util/k8s.go index 71164e2..a30e18d 100644 --- a/util/k8s.go +++ b/util/k8s.go @@ -90,9 +90,13 @@ func K8sCommandArgs(args []string, namespace string, context string, labels stri return args } -func GetClientset(kubeconfig string) *kubernetes.Clientset { - // use the current context in kubeconfig - config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) +func GetClientset(kubeconfig, context string) *kubernetes.Clientset { + + rules := clientcmd.NewDefaultClientConfigLoadingRules() + rules.ExplicitPath = kubeconfig + overrides := &clientcmd.ConfigOverrides{CurrentContext: context} + + config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides).ClientConfig() if err != nil { panic(err.Error()) }