婷婷同学_
我的集群外配置示例代码 var kubeconfig *string kubeconfig = flag.String("kubeconfig", "./config", "(optional) relative path to the kubeconfig file") flag.Parse() // kubernetes config loaded from ./config or whatever the flag was set to config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) if err != nil { panic(err) } // instantiate our client with config clientset, err := kubernetes.NewForConfig(config) if err != nil { panic(err) } // get a list of our CRs pl := PingerList{} d, err := clientset.RESTClient().Get().AbsPath("/apis/pinger.hel.lo/v1/pingers").DoRaw(context.TODO()) if err != nil { panic(err) } if err := json.Unmarshal(d, &pl); err != nil { panic(err) }PingerList{} 是一个从 Kubebuilder 生成的对象,我稍后在代码中将其解组。但是,您可以直接println(string(d))获取该 json。AbsPath() 中的组件是"/apis/group/verison/plural version of resource name"如果您使用的是 minikube,您可以使用以下命令获取配置文件kubectl config viewKubernetes相关的导入如下"k8s.io/client-go/tools/clientcmd""k8s.io/client-go/kubernetes"