Workload Identity Federation for GKE (WIF) is a mechanism that lets your cluster's
workloads access external cloud resources with Kubernetes service accounts. WIF
is the recommended way for applications running within GKE attached clusters
to access Google Cloud services. When you register your cluster, the
GKE attached clusters API creates all the infrastructure required for WIF, with
support for clusters that use either public or private OIDC issuers.

All GKE components that connect back to Google Cloud,
such as Connect and telemetry agents, use Workload Identity Federation for GKE.

## Kubernetes service accounts and Workload Identity Federation for GKE

When a Pod runs, it uses a
[Kubernetes service account](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/)
to authorize its access to resources hosted within its cluster. By using
WIF, the Pod can use the same Kubernetes service
account (KSA) to obtain a short-lived token to access resources hosted outside
the cluster.

WIF requires a trust relationship between the OIDC identity provider inside the
cluster and the external resource provider, such as Google Cloud IAM. When you register
an attached cluster, the registration process automatically creates this trust
relationship by adding your cluster's OIDC service to the default workload
identity pool bound to your Google Cloud project.

This workload identity pool is your Google Cloud project's pool of trusted identity
providers and is named `PROJECT_ID.svc.id.goog`, where `PROJECT_ID` is the
Google Cloud project ID associated with your attached cluster.

You can use regular IAM policies to control access by Kubernetes service
accounts to Google Cloud services. Kubernetes service account principals are the
representation of the KSA within the workload identity pool. They're
formatted as follows:

    principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/WORKLOAD_IDENTITY_POOL_NAME/subject/ns/KSA_NAMESPACE/sa/KSA_NAME

To grant a role to the Kubernetes service account, use the following command:

```bash
gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/KSA_NAMESPACE/sa/KSA_NAME" \
  --role=IAM_ROLE
```

Replace the following:

- <var translate="no">PROJECT_ID</var>: your Google Cloud project ID
- <var translate="no">PROJECT_NUMBER</var>: your Google Cloud project number
- <var translate="no">KSA_NAMESPACE</var>: the cluster namespace that this service account resides in
- <var translate="no">KSA_NAME</var>: the name of the Kubernetes service account you want to grant permissions to
- <var translate="no">IAM_ROLE</var>: the IAM role you want to grant to this KSA