Review network policy enforcement in GKE

Deploy required NetworkPolicies in GKE and verify that traffic restrictions take effect.

Description

Calico on GKE Standard requires both the control-plane add-on and node enforcement. Autopilot and Dataplane V2 include enforcement.

Potential impact

Even when enforcement is available, other workloads may reach services they do not need if no NetworkPolicy selects the target Pods.

Remediation

For Calico on a new Standard cluster, set provider: CALICO, network_policy.enabled: yes and addons_config.network_policy_config.disabled: no. On an existing cluster, use gcloud container clusters update with --update-addons=NetworkPolicy=ENABLED first, then with --enable-network-policy for the nodes. Schedule the change because it recreates the nodes.

Examples

The tasks create differently named clusters. Supply the project and path to the service account JSON file.

Before

yaml
- name: create a cluster2
  google.cloud.gcp_container_cluster:
    name: my-cluster2
    initial_node_count: 2
    node_config:
      machine_type: n1-standard-4
      disk_size_gb: 500
    location: us-central1-a
    project: "{{ gcp_project }}"
    auth_kind: serviceaccount
    service_account_file: "{{ gcp_service_account_file }}"
    state: present
    network_policy:
      provider: CALICO
      enabled: yes

- name: create a cluster5
  google.cloud.gcp_container_cluster:
    name: my-cluster5
    initial_node_count: 2
    node_config:
      machine_type: n1-standard-4
      disk_size_gb: 500
    location: us-central1-a
    project: "{{ gcp_project }}"
    auth_kind: serviceaccount
    service_account_file: "{{ gcp_service_account_file }}"
    state: present
    network_policy:
      provider: CALICO
      enabled: yes
    addons_config:
      network_policy_config:
        disabled: yes

Both my-cluster2 and my-cluster5 request node enforcement. The first omits the add-on setting and the second explicitly disables it.

After

yaml
- name: create a cluster
  google.cloud.gcp_container_cluster:
    name: my-cluster
    initial_node_count: 2
    node_config:
      machine_type: n1-standard-4
      disk_size_gb: 500
    location: us-central1-a
    project: "{{ gcp_project }}"
    auth_kind: serviceaccount
    service_account_file: "{{ gcp_service_account_file }}"
    state: present
    network_policy:
      provider: CALICO
      enabled: yes
    addons_config:
      network_policy_config:
        disabled: no

my-cluster also enables the add-on. Deploy a NetworkPolicy selecting the target Pods and test a permitted and a blocked connection. On an existing cluster, run the tests after node recreation finishes.

References