(feat) Add auto-discovery in k8s | Adarsh
This commit is contained in:
66
k8s/client.go
Normal file
66
k8s/client.go
Normal file
@ -0,0 +1,66 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user