How to programmatically get the network tags for a GKE cluster

9/2/2019

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?

-- Andrew Ridout
google-cloud-platform
google-compute-engine
google-kubernetes-engine

3 Answers

9/2/2019

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.

-- Ernesto U
Source: StackOverflow

9/29/2019

Does anyone know of a way of doing this using Terraform

-- Adam
Source: StackOverflow

9/3/2019

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"
-- Andrew Ridout
Source: StackOverflow