一、创建连接
func K8sClient() *kubernetes.Clientset { // 使用当前上下文环境 var kubeconfig *string if home := homedir.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()) } // 根据指定的 config 创建一个新的 clientSet clientSet, err := kubernetes.NewForConfig(config) if err != nil { panic(err.Error()) } return clientSet }