Skip to content

Commit

Permalink
Improve kubeconfig loading to use built-in client-go methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Barrick committed Feb 20, 2019
1 parent 737df46 commit 1b1a2cd
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions pkg/kube/clientset.go
Expand Up @@ -15,10 +15,6 @@
package kube

import (
"fmt"
"os"
"path/filepath"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -49,28 +45,8 @@ func NewMetricsClientSet() (*metrics.Clientset, error) {
}

func getKubeConfig() (*rest.Config, error) {
var kubeconfig string
if os.Getenv("KUBECONFIG") != "" {
kubeconfig = os.Getenv("KUBECONFIG")
} else if home := homeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
} else {
fmt.Println("Parsing kubeconfig failed, please set KUBECONFIG env var")
os.Exit(1)
}

if _, err := os.Stat(kubeconfig); err != nil {
// kubeconfig doesn't exist
fmt.Printf("%s does not exist - please make sure you have a kubeconfig configured.\n", kubeconfig)
panic(err.Error())
}

return clientcmd.BuildConfigFromFlags("", kubeconfig)
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
).ClientConfig()
}

0 comments on commit 1b1a2cd

Please sign in to comment.