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 關注
- 139 瀏覽
添加回答
舉報