Skip to content

Instantly share code, notes, and snippets.

@augustohp
Created August 23, 2023 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustohp/95a894f676a664e22f50291ed13578c5 to your computer and use it in GitHub Desktop.
Save augustohp/95a894f676a664e22f50291ed13578c5 to your computer and use it in GitHub Desktop.
Logins into all clusters, for all projects in `gcloud`
#!/usr/bin/env sh
#
# Will add k8s (cluster) credentials for all existing projects for current
# gcloud account.
#
# Author: augusto.hp+oss@gmail.com
# shellcheck disable=SC3043
set -e
OPTION_SEPARATOR=","
for dep in gcloud kubectl awk sed
do
if [ -z "$(command -v $dep)" ]
then
echo "Error: $dep missing." >&2
exit 2
fi
done
list_projects()
{
gcloud projects list \
| grep -v sys \
| grep -v "PROJECT" \
| awk '{ print $1 }'
}
list_k8s_clusters()
{
gcloud container clusters list \
| grep -v "^NAME" \
| awk -v OFS="$OPTION_SEPARATOR" '{ print $1, $2 }'
}
switch_project()
{
local project="$1"
gcloud config set project "$project" > /dev/null 2>&1
}
indent()
{
sed 's/^/ /'
}
main()
{
local current_project=""
local cluster=""
local location=""
current_project="$(gcloud config get project)"
for project in $(list_projects)
do
echo "Project: $project"
switch_project "$project"
for cluster_location in $(list_k8s_clusters)
do
cluster="$(echo "$cluster_location" | cut -f1 -d$OPTION_SEPARATOR )"
location="$(echo "$cluster_location" | cut -f2 -d$OPTION_SEPARATOR )"
echo "Cluster: $cluster" | indent
gcloud container clusters get-credentials "$cluster" --location="$location" 2>&1 | indent | indent
done
done
switch_project "$current_project"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment