> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/use-gitops/gitops-entities/clusters/create-cluster-with-gke-wif.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Create a GitOps cluster with GKE Workload Identity

Connect Harness GitOps (Argo CD) to Google Kubernetes Engine (GKE) using Workload Identity for secure, keyless authentication.

In this guide, you will set up Harness GitOps to connect securely to a Google Kubernetes Engine (GKE) cluster without storing kubeconfigs or static credentials.

## What you will learn from this topic <a href="#what-you-will-learn-from-this-topic" id="what-you-will-learn-from-this-topic"></a>

* What [GKE Workload Identity](#what-is-gke-workload-identity) is and how it provides keyless authentication for GKE clusters.
* The [setup process](#overview-of-the-setup-process) for connecting a GKE cluster to Harness GitOps.
* How to create and bind GCP IAM service accounts, modify the GitOps agent YAML, and deploy the agent with Workload Identity annotations.
* How to create the [cluster secret](#step-8-create-cluster-secret) and verify the connection in the Harness UI.

## Before you begin <a href="#prerequisites" id="prerequisites"></a>

Before setting up GKE Workload Identity with GitOps, ensure you have:

* **Google Cloud Platform (GCP) project** with appropriate permissions to:
  * Create and manage IAM service accounts
  * Assign IAM roles and policies
  * Manage GKE clusters
  * Configure Workload Identity bindings
* **GKE cluster** with Workload Identity enabled
* **gcloud CLI** installed and configured
* **kubectl** installed and configured
* **Harness GitOps module** enabled in your account

{% hint style="info" %}
**Enable Workload Identity at both the cluster and node pool level**

Workload Identity must be enabled at the cluster level and configured for the specific node pools where your GitOps agent will run.
{% endhint %}

## What is GKE Workload Identity <a href="#what-is-gke-workload-identity" id="what-is-gke-workload-identity"></a>

GKE **Workload Identity** allows Kubernetes ServiceAccounts to securely access Google Cloud resources **without storing credentials or secrets** in the cluster.\
Instead, GKE binds Kubernetes ServiceAccounts to Google Cloud IAM Service Accounts, enabling workloads to authenticate using short-lived tokens.

With Workload Identity:

* No kubeconfigs or static service account keys are stored
* Authentication is short-lived and automatically rotated
* Access is scoped to a specific Kubernetes ServiceAccount
* Follows Google Cloud's security best practices

Go to the [GKE Workload Identity documentation](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity) to learn more about how Workload Identity works in GKE, and go to [Argo CD's GKE Workload Identity setup guide](https://argo-cd.readthedocs.io/en/stable/operator-manual/declarative-setup/#gke) to see Argo CD's native configuration guidance.

## Overview of the setup process <a href="#overview-of-the-setup-process" id="overview-of-the-setup-process"></a>

The GKE Workload Identity setup involves these key steps:

1. **Enable Workload Identity on the GKE cluster**: Configure your cluster to support Workload Identity.
2. **Create a GCP IAM service account**: Create a service account for GitOps agent authentication.
3. **Grant IAM permissions**: Assign the necessary permissions to the service account.
4. **Establish an IAM binding**: Link the Kubernetes service account to the GCP IAM service account.
5. **Download and modify the GitOps agent YAML**: Download the agent YAML and configure service account annotations.
6. **Annotate an existing service account**: If you are reusing an existing Kubernetes service account, annotate it directly.
7. **Deploy the GitOps agent**: Install the modified agent in your cluster.
8. **Create the cluster secret**: Configure the cluster connection with Workload Identity authentication.
9. **Verify setup**: Ensure the cluster appears healthy in the Harness UI.

## Enable Workload Identity on the GKE cluster <a href="#step-1-enable-workload-identity-on-gke-cluster" id="step-1-enable-workload-identity-on-gke-cluster"></a>

Workload Identity must be enabled at both the cluster level and node pool level.

**Check if Workload Identity is already enabled**

First, check if your cluster already has Workload Identity enabled:

```bash
# Set your project and cluster details
export PROJECT_ID="your-gcp-project-id"
export CLUSTER_NAME="your-gke-cluster"
export CLUSTER_ZONE="us-central1-a"  # Replace with your cluster zone/region

# Check cluster configuration
gcloud container clusters describe "$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID" \
  --format="value(workloadIdentityConfig.workloadPool)"
```

If the output shows `<PROJECT_ID>.svc.id.goog`, Workload Identity is already enabled. Skip to the next step.

**Enable Workload Identity on an existing cluster**

If Workload Identity is not enabled, enable it on your cluster:

```bash
gcloud container clusters update "$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID" \
  --workload-pool="${PROJECT_ID}.svc.id.goog"
```

{% hint style="warning" %}
**Node pools require a separate update**

Enabling Workload Identity on an existing cluster does not automatically enable it on existing node pools. You must update node pools separately.
{% endhint %}

**Enable Workload Identity on node pools**

Update your node pools to enable Workload Identity:

```bash
# List all node pools
gcloud container node-pools list \
  --cluster="$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID"

# Enable Workload Identity on each node pool
gcloud container node-pools update <NODE_POOL_NAME> \
  --cluster="$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID" \
  --workload-metadata=GKE_METADATA
```

Replace `<NODE_POOL_NAME>` with your actual node pool name. Repeat for all node pools.

**Create a new cluster with Workload Identity (optional)**

If you are creating a new cluster, enable Workload Identity during creation:

```bash
gcloud container clusters create "$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID" \
  --workload-pool="${PROJECT_ID}.svc.id.goog" \
  --enable-autoscaling \
  --min-nodes=1 \
  --max-nodes=3
```

## Create a GCP IAM service account <a href="#step-2-create-gcp-iam-service-account" id="step-2-create-gcp-iam-service-account"></a>

Create a Google Cloud IAM service account that will be used by your GitOps agent to authenticate to the GKE cluster.

**Set environment variables**

```bash
export GCP_SA_NAME="gitops-wi-sa"
export GCP_SA_EMAIL="${GCP_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
export GCP_SA_DISPLAY_NAME="GitOps Workload Identity Service Account"
```

{% hint style="info" %}
**You can reuse an existing service account**

If you already have a GCP IAM service account, you can use it instead of creating a new one. Just set the `GCP_SA_EMAIL` variable to your existing service account email.
{% endhint %}

**Create the IAM service account**

```bash
gcloud iam service-accounts create "$GCP_SA_NAME" \
  --project="$PROJECT_ID" \
  --display-name="$GCP_SA_DISPLAY_NAME" \
  --description="Service account for Harness GitOps agent to access GKE cluster via Workload Identity"
```

**Verify service account creation**

```bash
gcloud iam service-accounts describe "$GCP_SA_EMAIL" \
  --project="$PROJECT_ID"
```

## Grant IAM permissions to the service account <a href="#step-3-grant-iam-permissions-to-service-account" id="step-3-grant-iam-permissions-to-service-account"></a>

Grant the necessary IAM permissions to your service account to access the GKE cluster.

**Grant the Container Cluster Viewer role (required)**

The service account needs permissions to interact with GKE clusters. At minimum, grant the **Container Cluster Viewer** role:

```bash
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${GCP_SA_EMAIL}" \
  --role="roles/container.clusterViewer"
```

This role provides:

* Get and list GKE clusters
* Get cluster credentials
* View cluster resources

{% hint style="info" %}
**This permission is required for agent authentication**

This permission is **required** for the GitOps agent to authenticate and connect to the cluster. Without it, you will see errors like:

* `Permission 'iam.serviceAccounts.getAccessToken' denied`
* `User cannot list resource "secrets" in API group ""`
  {% endhint %}

**Grant additional roles (optional)**

Depending on your requirements, you may need additional roles:

```bash
# For more extensive cluster management capabilities
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${GCP_SA_EMAIL}" \
  --role="roles/container.developer"
```

{% hint style="info" %}
**Consider custom IAM roles for production**

For production environments, consider creating custom IAM roles with minimal required permissions instead of using predefined roles.
{% endhint %}

## Establish the IAM binding <a href="#step-4-establish-iam-binding" id="step-4-establish-iam-binding"></a>

Create an IAM policy binding that allows the Kubernetes service account to impersonate the GCP IAM service account.

**Set Kubernetes service account variables**

```bash
export K8S_NAMESPACE="your-namespace"
export K8S_SA_NAME="default"
```

{% hint style="info" %}
**Choose the namespace and service account carefully**

Choose a namespace where you will deploy your GitOps agent. The agent YAML will need to be configured to use this namespace and service account.
{% endhint %}

**Create the IAM policy binding**

```bash
gcloud iam service-accounts add-iam-policy-binding "$GCP_SA_EMAIL" \
  --project="$PROJECT_ID" \
  --role="roles/iam.workloadIdentityUser" \
  --member="serviceAccount:${PROJECT_ID}.svc.id.goog[${K8S_NAMESPACE}/${K8S_SA_NAME}]"
```

This command creates a binding that allows the Kubernetes service account `<K8S_SA_NAME>` in namespace `<K8S_NAMESPACE>` to act as the GCP service account.

**Verify the IAM binding**

```bash
gcloud iam service-accounts get-iam-policy "$GCP_SA_EMAIL" \
  --project="$PROJECT_ID"
```

You should see a binding similar to:

```yaml
bindings:
- members:
  - serviceAccount:your-gcp-project-id.svc.id.goog[your-namespace/default]
  role: roles/iam.workloadIdentityUser
```

## Download and modify the GitOps agent YAML <a href="#step-5-download-and-modify-gitops-agent-yaml" id="step-5-download-and-modify-gitops-agent-yaml"></a>

Before you can configure workload identity authentication, you need to download the GitOps agent YAML from Harness and make specific modifications to enable Workload Identity.

**Download the GitOps agent YAML from Harness**

First, get the agent YAML file from your Harness account:

1. **Log in to your Harness account** and navigate to the GitOps module.
2. Go to **GitOps** -> **Settings** -> **GitOps Agents**.
3. Click **+ New GitOps Agent**.
4. Fill in the agent details:
   * **Name**: Enter a descriptive name for your agent (for example, `gke-wif-agent`)
   * **Agent Type**: Select **Argo CD**
   * **Namespace**: Enter your namespace (this must match the namespace in your IAM binding)
5. Click **Continue** or **Next**.
6. **Download the agent YAML file.** This is a file named something like `gitops-agent.yaml`.
7. **Save the file locally.** You need to edit it before applying it to your cluster.

{% hint style="info" %}
**Do not apply the YAML file until you finish the modifications**

Do not apply the agent YAML to your cluster yet. You must first modify it with the Workload Identity service account annotation as described in the next steps.
{% endhint %}

**Annotate the service account in the agent YAML**

Now that you have the agent YAML file, annotate the service account to enable Workload Identity.

Open the downloaded YAML file in your preferred text editor. Find the application controller service account section. Look for this section in the file:

```yaml
# Source: gitops-helm/charts/argo-cd/templates/argocd-application-controller/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
  name: argocd-application-controller
  namespace: your-namespace
  labels:
    # ... existing labels
```

Add the GCP Workload Identity annotation to the `metadata.annotations` section:

```yaml
# Source: gitops-helm/charts/argo-cd/templates/argocd-application-controller/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
  annotations:
    iam.gke.io/gcp-service-account: your-gcp-sa@your-gcp-project-id.iam.gserviceaccount.com
  name: argocd-application-controller
  namespace: your-namespace
  labels:
    # ... existing labels
```

Replace `your-gcp-sa@your-gcp-project-id.iam.gserviceaccount.com` with your actual GCP service account email.

{% hint style="info" %}
**Using an existing service account**

If you are using an existing Kubernetes service account (like `default` in your case), you have two options:

**Option A: Modify the agent to use the existing service account** Change the agent YAML to reference your existing service account instead of creating a new one:

```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: argocd-application-controller
  namespace: your-namespace
spec:
  template:
    spec:
      serviceAccountName: default  # Use your existing annotated service account
```

**Option B: Annotate the agent's service account** If you want to use the service account created by the agent YAML, add the annotation as shown above.
{% endhint %}

**Annotate additional service accounts (if using option B)**

If you are creating new service accounts via the agent YAML, you may also need to annotate the `argocd-server` service account for displaying logs in the Harness UI.

Find the Argo CD server service account section:

```yaml
# Source: gitops-helm/charts/argo-cd/templates/argocd-server/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
  annotations:
    iam.gke.io/gcp-service-account: your-gcp-sa@your-gcp-project-id.iam.gserviceaccount.com
  name: argocd-server
  namespace: your-namespace
  labels:
    # ... existing labels
```

**Verify your modifications**

Before proceeding, double-check that you have made these changes:

* Added `iam.gke.io/gcp-service-account` annotation with your GCP service account email
* Updated the namespace to match your target namespace
* If using an existing service account, updated the `serviceAccountName` references in deployments/statefulsets
* Saved the modified YAML file

{% hint style="info" %}
**Keep a backup of the original file**

Keep the original downloaded file as a backup in case you need to start over with the modifications.
{% endhint %}

## Annotate an existing service account <a href="#step-6-annotate-existing-service-account-if-using-existing-sa" id="step-6-annotate-existing-service-account-if-using-existing-sa"></a>

If you are using an existing Kubernetes service account that is already being used by other workloads, you need to annotate it with the Workload Identity binding.

**Create the namespace (if it does not exist)**

```bash
kubectl create namespace "$K8S_NAMESPACE" --dry-run=client -o yaml | kubectl apply -f -
```

**Annotate the Kubernetes service account**

```bash
kubectl annotate serviceaccount "$K8S_SA_NAME" \
  --namespace="$K8S_NAMESPACE" \
  iam.gke.io/gcp-service-account="$GCP_SA_EMAIL" \
  --overwrite
```

This is the command you have already run. The annotation establishes the link between the Kubernetes service account and the GCP IAM service account.

**Verify the service account annotation**

```bash
kubectl describe serviceaccount "$K8S_SA_NAME" \
  --namespace="$K8S_NAMESPACE"
```

You should see the annotation:

```
iam.gke.io/gcp-service-account: your-gcp-sa@your-gcp-project-id.iam.gserviceaccount.com
```

## Deploy the GitOps agent <a href="#step-7-deploy-gitops-agent" id="step-7-deploy-gitops-agent"></a>

Apply the modified GitOps agent YAML to your cluster.

**Ensure the kubectl context is correct**

Before applying, ensure you are connected to the correct cluster:

```bash
# Configure kubectl to use your GKE cluster
gcloud container clusters get-credentials "$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID"

# Verify context
kubectl config current-context
```

**Apply the GitOps agent YAML**

```bash
kubectl apply -f gitops-agent.yaml
```

**Verify agent deployment**

Check that all GitOps agent pods are running:

```bash
kubectl get pods -n "$K8S_NAMESPACE"
```

Wait for all pods to be in `Running` state. The main pods to watch for are:

* `argocd-application-controller-*`
* `argocd-repo-server-*`
* `argocd-server-*`
* `gitops-agent-*` (if using Harness-specific components)

**Check pod logs for Workload Identity**

Verify that Workload Identity is working by checking the application controller logs:

```bash
kubectl logs -n "$K8S_NAMESPACE" -l app.kubernetes.io/name=argocd-application-controller --tail=50
```

You should not see any authentication errors related to GCP credentials.

**Verify the agent in the Harness UI**

1. Navigate to **GitOps** -> **Settings** -> **GitOps Agents** in your Harness account.
2. Verify that your agent shows a **Healthy** status.

## Create the cluster secret <a href="#step-8-create-cluster-secret" id="step-8-create-cluster-secret"></a>

Create a Kubernetes secret that defines how to connect to your GKE cluster using Workload Identity authentication.

**Gather cluster information**

Get your cluster's API server endpoint and CA certificate:

```bash
# Get cluster endpoint
export CLUSTER_ENDPOINT=$(gcloud container clusters describe "$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID" \
  --format="value(endpoint)")

echo "Cluster endpoint: https://${CLUSTER_ENDPOINT}"

# Get CA certificate (base64 encoded)
export CA_CERT=$(gcloud container clusters describe "$CLUSTER_NAME" \
  --zone="$CLUSTER_ZONE" \
  --project="$PROJECT_ID" \
  --format="value(masterAuth.clusterCaCertificate)")

echo "CA Certificate (first 50 chars): ${CA_CERT:0:50}..."
```

**Create the cluster secret**

Create the secret that tells Harness GitOps how to authenticate to the cluster:

```bash
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: gke-wif-cluster
  namespace: ${K8S_NAMESPACE}
  labels:
    argocd.argoproj.io/secret-type: cluster
type: Opaque
stringData:
  name: gke-wif-cluster
  server: https://${CLUSTER_ENDPOINT}
  config: |
    {
      "execProviderConfig": {
        "command": "argocd-k8s-auth",
        "args": ["gcp"],
        "apiVersion": "client.authentication.k8s.io/v1beta1"
      },
      "tlsClientConfig": {
        "insecure": false,
        "caData": "${CA_CERT}"
      }
    }
EOF
```

{% hint style="info" %}
**This command uses the Workload Identity binding automatically**

The cluster secret uses `argocd-k8s-auth gcp` command which leverages the Workload Identity setup. This command automatically uses the annotated service account to obtain credentials from the GCP metadata server.
{% endhint %}

**Alternative: Create the secret from a YAML file**

If you prefer to create a separate YAML file:

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: gke-wif-cluster
  namespace: your-namespace
  labels:
    argocd.argoproj.io/secret-type: cluster
type: Opaque
stringData:
  name: gke-wif-cluster
  server: https://<CLUSTER_ENDPOINT>
  config: |
    {
      "execProviderConfig": {
        "command": "argocd-k8s-auth",
        "args": ["gcp"],
        "apiVersion": "client.authentication.k8s.io/v1beta1"
      },
      "tlsClientConfig": {
        "insecure": false,
        "caData": "<BASE64_ENCODED_CA_CERT>"
      }
    }
```

Replace the placeholders:

* `<CLUSTER_ENDPOINT>`: Your cluster's API server endpoint
* `<BASE64_ENCODED_CA_CERT>`: The base64-encoded CA certificate

Then apply:

```bash
kubectl apply -f gke-cluster-secret.yaml
```

**Verify the secret**

Check that the secret was created correctly:

```bash
kubectl get secret gke-wif-cluster -n "$K8S_NAMESPACE"
kubectl describe secret gke-wif-cluster -n "$K8S_NAMESPACE"
```

## Verify setup in the Harness UI <a href="#step-9-verify-setup-in-harness-ui" id="step-9-verify-setup-in-harness-ui"></a>

**Check cluster status**

1. Navigate to **GitOps** -> **Settings** -> **Clusters** in your Harness account.
2. You should see your cluster listed with an **Active** status.
3. The cluster should show as **Connected**.

![Cluster connected and healthy in Harness](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-de54d77b5c1a33b9283ae685e7242a273b4b9fd2%2Fgke-wif-cluster-healthy.png?alt=media)

**Test the connection**

Create a simple test application to verify the connection:

1. Go to **GitOps** -> **Applications**.
2. Click **+ New Application**.
3. Configure a simple application pointing to your cluster.
4. Verify that the application can successfully sync to the cluster.

![Application successfully synced and healthy](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-8269066d65cfc4dc34ce00af30b1c540e32e93bd%2Fgke-wif-app-synced.png?alt=media)

## Summary <a href="#summary" id="summary"></a>

In this guide, you have learned how to:

1. Enable Workload Identity on your GKE cluster and node pools
2. Create and configure GCP IAM service accounts with the required Container Cluster Viewer permissions
3. Establish Workload Identity bindings between Kubernetes and GCP service accounts
4. Modify and deploy Harness GitOps agents with Workload Identity annotations
5. Create cluster secrets for secure authentication using `argocd-k8s-auth`
6. Verify the setup and deploy applications to your cluster

Your GKE cluster is now securely connected to Harness GitOps using Workload Identity, eliminating the need for static credentials and following Google Cloud security best practices.

## Next steps <a href="#next-steps" id="next-steps"></a>

* [GitOps Agent metrics with Prometheus](https://developer.harness.io/continuous-delivery/use-gitops/gitops-entities/agents/gitops-agent-metrics.md): Monitor the health and performance of the agent connected to this cluster.
* [Use secrets for cluster credentials](https://developer.harness.io/continuous-delivery/use-gitops/gitops-entities/clusters/use-secrets-for-cluster-credentials.md): Review alternative, secret-based authentication methods for other clusters.
* [Manage GitOps Applications](https://developer.harness.io/continuous-delivery/use-gitops/application/manage-gitops-applications.md): Deploy applications to your newly connected cluster.

{% @harness-feedback/feedback %}
