Search code examples
pythongoogle-cloud-platformgoogle-kubernetes-enginefirewallgoogle-apis-explorer

List firewall for specific gke cluster using python api?


I was trying to list the firewall for a specific GKE cluster under Project. (Say, If my cluster name was "democluster_1" under the project "myproject").How to list the firewall specific to "democluster_1"?

I used this python API to list all the firewalls under the project "myproject".I need to know how to list it for a specific cluster..? https://cloud.google.com/compute/docs/reference/rest/v1/firewalls/list

I explored about filter parameter in this python-API but I'm not clear how to use this parameter. Can you please explain with an example of how to filter a firewall for a specific GKE cluster. Or if there is any other method available to list it..?

Thanks in advance.


Solution

  • I am not familiar with Python but if you would like to list them using gcloud CLI as per GKE documentation.

    $ gcloud compute firewall-rules list

    In my project I have about 46 Firewall rules:

    $ gcloud compute firewall-rules list | wc -l
    
    To show all fields of the firewall, please show in JSON format: --format=json
    To show all fields in table format, please see the examples in --help.
    
    46
    

    If you want to get Firewall rules specific for your cluster you need to use something like:

    $ gcloud compute firewall-rules list --project <project-name> --filter="name~gke-<your-cluster-name>-[0-9a-z]*"
    

    In my case it was only 3 rules specified for this test cluster with name cluster.

    $ gcloud compute firewall-rules list --project <myproject> --filter="name~gke-cluster-[0-9a-z]*"
    
    NAME                      NETWORK  DIRECTION  PRIORITY  ALLOW                         DENY  DISABLED
    gke-cluster-XXX-all  default  INGRESS    1000      tcp,udp,XXX                         False
    gke-cluster-XXX-ssh  default  INGRESS    1000      tcp:XX                              False
    gke-cluster-XXX-vms  default  INGRESS    1000      icmp,tcp:1-XXXXX,udp:1-XXXXX        False