A small utility to generate a kubectl
configuration file for all clusters you have access to in GKE.
$ gke-config-helper
The basic invocation will check each project you have access to, list clusters and output a kubeconfig with a cluster
and a context
entry for each of them to stdout.
Since you might call this utility on a semi-regular basis, when there are new clusters to interact with, it is suggested to store the output in a separate configuration file if you also connect to non-GKE clusters (eg minikube). kubectl
supports merging multiple configuration files by using the KUBECONFIG
environment variable:
$ gke-config-helper > ~/.kube/gke-config
$ export KUBECONFIG=~/.kube/config:~/.kube/gke-config
In this way, other cluster, context and user configurations are not affected by updating the GKE configuration. kubectl
also stores written values in the first of the files, meaning that current-context
will remain in the default file and be preserved when regenerating the gke-config
file.
If you have access to a lot of projects, iterating over all of them might be slow. The flag --search-root
allows you to restrict the search to projects that are children of a folder.
$ gke-config-helper --search-root my-folder
This works to any depth, not only direct decendants of my-folder
.
By default, the name of each context will be <projectId>-<clusterName>
. You can control this with the --context-name-template
flag, which takes a Go template as an argument. Both the built-in Go functions as well as the sprig library functions are available.
The properties available for templating are:
ProjectId
: The project idName
: Name of the clusterLocation
: Region name
For example:
$ gke-config-helper --context-name-template '{{ slice (.Name | splitList "-") 0 2 | join "-" }}_{{ .Location | substr 0 2 }}'
With a cluster foo-bar-baz
in europe-west3
, this would generate a context name of foo-bar_eu
.