Search code examples
google-cloud-platformgoogle-compute-enginegoogle-kubernetes-engine

How to programmatically get the network tags for a GKE cluster


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?


Solution

  • 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_ID=<project-id>
    
    NODE_NETWORK_TAG=$(gcloud compute firewall-rules list --project $PROJECT_ID --filter="name~gke-$CLUSTER_NAME-[0-9a-z]*-master" --format="value(targetTags[0])")
    
    echo "$NODE_NETWORK_TAG"