If I create a new GKE cluster called cluster-1
, the VMs in the cluster will all have an auto-generated network tag, e.g. gke-cluster-1-d4732bcc-node
.
Is it possible, using gcloud
CLI or something else, to programmatically retrieve this network tag using the cluster name?
You can only get a VM network tag with gcloud using the command
gcloud compute instances describe INSTANCE-NAME --project=PROJECT-ID --zone=INSTANCE-ZONE
The network tag information will be at the bottom and the output will be similar to:
tags:
fingerprint: xxxx
items:
- tag1
- tag2
- tag3
All the VMs created by the cluster will have the same prefix. gke-CLUSTER_NAME-NODE_POOL_NAME-RANDOM_STRING.
For example I created cluster “test-cluster” and I’m using only “default-pool”. One of my instances is [gke-test-cluster-default-pool-xxxxxxx-xxxxxxx]
You can get all the instances names created by your clusters and put them in a variable similar to
name=`gcloud compute instances list --project=PROJECT-ID | grep gke | awk '{print $1}'`
Now you can use a FOR loop to run the command
for tags in $name; do gcloud compute instances describe $tags --project=PROJECT-ID --zone=ZONE; done
You can add a GREP at the end of the command just to fetch the network tag information , store the output in a file or parsed anyway you need it.
Does anyone know of a way of doing this using Terraform
I achieved this by getting one of the auto-generated firewall rules for the GKE cluster and pulling out the target tag:
CLUSTER_NAME=<cluster-name>
PROJECT_NAME=<project-name>
NODE_NETWORK_TAG=$(gcloud compute firewall-rules list --project $PROJECT_NAME --filter="name~gke-$CLUSTER_NAME-[0-9a-z]*-master" --format="value(targetTags[0])")
echo "$NODE_NETWORK_TAG"