我为此主题创建了一篇博文。它包括代码和演示的视频演练。本质上,您可以从 Google Container Engine API 获取 Kubernetes 凭据。以下是如何在 golang 中执行此操作:
func newKubernetesClient(clstr *container.Cluster) (*kubernetes.Clientset, error) {
cert, err := base64.StdEncoding.DecodeString(clstr.MasterAuth.ClientCertificate)
if err != nil {
return nil, err
}
key, err := base64.StdEncoding.DecodeString(clstr.MasterAuth.ClientKey)
if err != nil {
return nil, err
}
ca, err := base64.StdEncoding.DecodeString(clstr.MasterAuth.ClusterCaCertificate)
if err != nil {
return nil, err
}
config := &rest.Config{
Host: clstr.Endpoint,
TLSClientConfig: rest.TLSClientConfig{CertData: cert, KeyData: key, CAData: ca},
Username: clstr.MasterAuth.Username,
Password: clstr.MasterAuth.Password,
// Insecure: true,
}
kbrnts, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return kbrnts, nil
}