Skip to content

Commit 4f5dded

Browse files
ykyrbharathkkb
andauthored
feat: WorkloadIdenity allow to use k8s sa from the different project (#1275)
* feat: allow to use k8s sa from the different project * chore: generate docs Co-authored-by: Bharath KKB <[email protected]>
1 parent 71e7067 commit 4f5dded

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

modules/workload-identity/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ already bear the `"iam.gke.io/gcp-service-account"` annotation.
101101
| gcp\_sa\_name | Name for the Google service account; overrides `var.name`. | `string` | `null` | no |
102102
| impersonate\_service\_account | An optional service account to impersonate for gcloud commands. If this service account is not specified, the module will use Application Default Credentials. | `string` | `""` | no |
103103
| k8s\_sa\_name | Name for the Kubernetes service account; overrides `var.name`. `cluster_name` and `location` must be set when this input is specified. | `string` | `null` | no |
104+
| k8s\_sa\_project\_id | GCP project ID of the k8s service account; overrides `var.project_id`. | `string` | `null` | no |
104105
| location | Cluster location (region if regional cluster, zone if zonal cluster). Required if using existing KSA. | `string` | `""` | no |
105106
| name | Name for both service accounts. The GCP SA will be truncated to the first 30 chars if necessary. | `string` | n/a | yes |
106107
| namespace | Namespace for the Kubernetes service account | `string` | `"default"` | no |

modules/workload-identity/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ locals {
2626
output_k8s_name = var.use_existing_k8s_sa ? local.k8s_given_name : kubernetes_service_account.main[0].metadata[0].name
2727
output_k8s_namespace = var.use_existing_k8s_sa ? var.namespace : kubernetes_service_account.main[0].metadata[0].namespace
2828

29-
k8s_sa_gcp_derived_name = "serviceAccount:${var.project_id}.svc.id.goog[${var.namespace}/${local.output_k8s_name}]"
29+
k8s_sa_project_id = var.k8s_sa_project_id != null ? var.k8s_sa_project_id : var.project_id
30+
k8s_sa_gcp_derived_name = "serviceAccount:${local.k8s_sa_project_id}.svc.id.goog[${var.namespace}/${local.output_k8s_name}]"
3031
}
3132

3233
data "google_service_account" "cluster_service_account" {
@@ -40,7 +41,7 @@ resource "google_service_account" "cluster_service_account" {
4041
count = var.use_existing_gcp_sa ? 0 : 1
4142

4243
account_id = local.gcp_given_name
43-
display_name = substr("GCP SA bound to K8S SA ${local.k8s_given_name}", 0, 100)
44+
display_name = substr("GCP SA bound to K8S SA ${local.k8s_sa_project_id}[${local.k8s_given_name}]", 0, 100)
4445
project = var.project_id
4546
}
4647

@@ -65,7 +66,7 @@ module "annotate-sa" {
6566
skip_download = true
6667
cluster_name = var.cluster_name
6768
cluster_location = var.location
68-
project_id = var.project_id
69+
project_id = local.k8s_sa_project_id
6970
impersonate_service_account = var.impersonate_service_account
7071

7172
kubectl_create_command = "kubectl annotate --overwrite sa -n ${local.output_k8s_namespace} ${local.k8s_given_name} iam.gke.io/gcp-service-account=${local.gcp_sa_email}"

modules/workload-identity/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ variable "k8s_sa_name" {
5454
default = null
5555
}
5656

57+
variable "k8s_sa_project_id" {
58+
description = "GCP project ID of the k8s service account; overrides `var.project_id`."
59+
type = string
60+
default = null
61+
}
62+
5763
variable "namespace" {
5864
description = "Namespace for the Kubernetes service account"
5965
type = string

0 commit comments

Comments
 (0)