If you use Google Kubernetes Engine (GKE), you can authenticate to Container Registry and Artifact Registry using Workload Identity.
The following steps assume that the Google service account is in the same project as the Container Registry and Artifact Registry image repositories.
-
Enable the GKE and Artifact Registry APIs:
gcloud services enable \ container.googleapis.com \ artifactregistry.googleapis.com
Note that enabling the GKE API also enables the Container Registry API.
-
Create a GKE cluster with Workload Identity, and assign the
cloud-platform
access scope to the nodes:PROJECT_ID=$(gcloud config get core/project) ZONE=us-central1-f gcloud container clusters create digester-webhook-test \ --enable-ip-alias \ --release-channel regular \ --scopes cloud-platform \ --workload-pool $PROJECT_ID.svc.id.goog \ --zone $ZONE
-
Create a Google service account:
GSA_NAME=digester-webhook GSA=$GSA_NAME@$PROJECT_ID.iam.gserviceaccount.com gcloud iam service-accounts create $GSA_NAME \ --display-name "Digester webhook service account"
The digester webhook Kubernetes service account impersonates this Google service account to authenticate to Container Registry and Artifact Registry.
-
Grant the Container Registry Service Agent role to the Google service account at the project level:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member "serviceAccount:$GSA" \ --role roles/containerregistry.ServiceAgent
-
Grant the Artifact Registry Reader to the Google service account at the project level:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member "serviceAccount:$GSA" \ --role roles/artifactregistry.reader
-
Grant the Workload Identity User role to the
digester-admin
Kubernetes service account in thedigester-system
namespace on the Google service account:gcloud iam service-accounts add-iam-policy-binding "$GSA" \ --member "serviceAccount:$PROJECT_ID.svc.id.goog[digester-system/digester-admin]" \ --role roles/iam.workloadIdentityUser
-
Add the Workload Identity annotation to the digester webhook Kubernetes service account:
kubectl annotate serviceaccount digester-admin --namespace digester-system \ "iam.gke.io/gcp-service-account=$GSA"
This annotation informs GKE that the Kubernetes service account
digester-admin
in the namespacedigester-system
can impersonate the Google service account$GSA
.
Workload Identity works with both online and offline authentication.
If you use Workload Identity to authenticate to Container Registry or Artifact
Registry, and if you do not rely on imagePullSecrets
to authenticate to
other container image registries, you can enable offline authentication on the
digester webhook without providing a Docker config file, see
authentication.md
.