在对Google Cloud进行OAuth身份验证后,通过kubectl(或client go)访问GKE资源



我已经使用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,

最新更新