我已经使用https://www.googleapis.com/auth/cloud-platform
作用域成功地完成了到Google Cloud的OAuth 2.0流。
我现在拥有一个代币。
我希望能够使用kubectl
或(最好(通过client-go
库访问GKE中的kubernetes资源。
我如何将此代币用于
- 是否获取驻留在特定项目中的特定集群的凭据
- 运行一些类似于创建/访问秘密的东西,例如
createdSecret, _ := clientset.CoreV1().Secrets(secret.Namespace).Create(ctx, secret, metav1.CreateOptions{})
这个答案补充了@DazWilkin 提出的答案
假设token
是已经通过OAuth2.0
流检索到的*oauth2.Token
对象。内存中的动态kube配置对象应按照以下进行构建
apiConfig := api.Config{
APIVersion: "v1",
Kind: "Config",
Clusters: map[string]*api.Cluster{
clusterName: {
CertificateAuthorityData: cert,
Server: server,
},
},
Contexts: map[string]*api.Context{
clusterName: {
Cluster: clusterName,
AuthInfo: clusterName,
},
},
// CurrentContext: clusterName,
AuthInfos: map[string]*api.AuthInfo{
Token: token.AccessToken,
clusterName: {
AuthProvider: &api.AuthProviderConfig{
Name: "gcp",
Config: map[string]string{
"scopes": scopes,
},
},
},
},
}
请注意在api.AuthInfo
结构中添加了Token: token.AccessToken,
。