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 }