gatus/k8s/client.go
2020-10-30 21:01:12 +05:30

67 lines
1.3 KiB
Go

package k8s
import (
"flag"
"os"
"path/filepath"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func NewClient(clusterMode string) *kubernetes.Clientset {
var kubeConfig *rest.Config
switch clusterMode {
case "in":
kubeConfig = getInclusterConfig()
case "out":
kubeConfig = getOutClusterConfig()
default:
panic("invalid cluster mode")
}
clientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
panic(err.Error())
}
return clientset
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func getOutClusterConfig() *rest.Config {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
return config
}
func getInclusterConfig() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
return config
}