Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use default client config loading rules #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
}
2 changes: 1 addition & 1 deletion cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down