2 回答
TA贡献1828条经验 获得超3个赞
ClientCertificate、ClientKey 和 ClusterCaCertificate 需要按照此处所述进行解码
func CreateK8sClientFromCluster(cluster *gkev1.Cluster) {
decodedClientCertificate, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClientCertificate)
if err != nil {
fmt.Println("decode client certificate error:", err)
return
}
decodedClientKey, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClientKey)
if err != nil {
fmt.Println("decode client key error:", err)
return
}
decodedClusterCaCertificate, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClusterCaCertificate)
if err != nil {
fmt.Println("decode cluster CA certificate error:", err)
return
}
config := &rest.Config{
Username: cluster.MasterAuth.Username,
Password: cluster.MasterAuth.Password,
Host: "https://" + cluster.Endpoint,
TLSClientConfig: rest.TLSClientConfig{
Insecure: false,
CertData: decodedClientCertificate,
KeyData: decodedClientKey,
CAData: decodedClusterCaCertificate,
},
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("failed to get k8s client set from config: %s\n", err)
return
}
}
TA贡献1883条经验 获得超3个赞
基本上,简而言之,推荐的方法是:
创建 Google Cloud IAM 服务帐户 + 下载其 json 密钥
将
GOOGLE_APPLICATION_CREDENTIALS
env var 设置为那个 key.json从中查找集群的 IP 地址和 CA 证书
gcloud container clusters describe
(或者只是.kube/config
从中获取文件gcloud get-credentials
将这些值传递给 client-go 并使用环境变量运行您的程序。
- 2 回答
- 0 关注
- 95 浏览
添加回答
举报