Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Last active March 25, 2024 20:55
Show Gist options
  • Save DazWilkin/9506c0b9677d53a3e11b7457ed21cbe7 to your computer and use it in GitHub Desktop.
Save DazWilkin/9506c0b9677d53a3e11b7457ed21cbe7 to your computer and use it in GitHub Desktop.
Stackoverflow: 73548332
ctx := context.Background()
containerService, _ := container.NewService(ctx)
name := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", clusterProject, clusterLocation, clusterName)
rqst := containerService.Projects.Locations.Clusters.Get(name)
resp, _ := rqst.Do()
cert, _ := base64.StdEncoding.DecodeString(resp.MasterAuth.ClusterCaCertificate)
// Create Config for Cluster
// Equivalent to ~/.kubeconfig
// apiVersion: v1
// kind: Config
// clusters:
// - name: gke_{project}_{location}_{cluster}
// cluster:
// certificate-authority-data: DATA+OMITTED
// server: https://...
// contexts:
// - name: gke_{project}_{location}_{cluster}
// context:
// cluster: gke_{project}_{location}_{cluster}
// user: gke_{project}_{location}_{cluster}
// users:
// - name: gke_{project}_{location}_{cluster}
// user:
// auth-provider:
// name: gcp
// config:
// scopes:
server := fmt.Sprintf("https://%s", resp.Endpoint)
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,
},
},
AuthInfos: map[string]*api.AuthInfo{
clusterName: {
AuthProvider: &api.AuthProviderConfig{
Name: "gcp",
Config: map[string]string{
"scopes": "https://www.googleapis.com/auth/cloud-platform",
},
},
},
},
}
return apiConfig, nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment