Skip to content

Commit 53f0f58

Browse files
feat: Workload Identity module, to bind roles in various projects for the service account created (#1574)
1 parent d012313 commit 53f0f58

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

modules/workload-identity/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ Kubernetes accounts.
1818

1919
```hcl
2020
module "my-app-workload-identity" {
21-
source = "terraform-google-modules/kubernetes-engine/google//modules/workload-identity"
22-
name = "my-application-name"
23-
namespace = "default"
24-
project_id = "my-gcp-project-name"
25-
roles = ["roles/storage.admin", "roles/compute.admin"]
21+
source = "terraform-google-modules/kubernetes-engine/google//modules/workload-identity"
22+
name = "my-application-name"
23+
namespace = "default"
24+
project_id = "my-gcp-project-name"
25+
roles = ["roles/storage.admin", "roles/compute.admin"]
26+
additional_projects = {"my-gcp-project-name1" : ["roles/storage.admin", "roles/compute.admin"],
27+
"my-gcp-project-name2" : ["roles/storage.admin", "roles/compute.admin"]}
2628
}
2729
```
2830

@@ -97,6 +99,7 @@ already bear the `"iam.gke.io/gcp-service-account"` annotation.
9799

98100
| Name | Description | Type | Default | Required |
99101
|------|-------------|------|---------|:--------:|
102+
| additional\_projects | A list of roles to be added to the created service account for additional projects | `map(list(string))` | `{}` | no |
100103
| annotate\_k8s\_sa | Annotate the kubernetes service account with 'iam.gke.io/gcp-service-account' annotation. Valid in cases when an existing SA is used. | `bool` | `true` | no |
101104
| automount\_service\_account\_token | Enable automatic mounting of the service account token | `bool` | `false` | no |
102105
| cluster\_name | Cluster name. Required if using existing KSA. | `string` | `""` | no |

modules/workload-identity/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ locals {
2828

2929
k8s_sa_project_id = var.k8s_sa_project_id != null ? var.k8s_sa_project_id : var.project_id
3030
k8s_sa_gcp_derived_name = "serviceAccount:${local.k8s_sa_project_id}.svc.id.goog[${var.namespace}/${local.output_k8s_name}]"
31+
32+
sa_binding_additional_project = distinct(flatten([for project, roles in var.additional_projects : [for role in roles : { project_id = project, role_name = role }]]))
3133
}
3234

3335
data "google_service_account" "cluster_service_account" {
@@ -89,3 +91,11 @@ resource "google_project_iam_member" "workload_identity_sa_bindings" {
8991
role = each.value
9092
member = local.gcp_sa_fqn
9193
}
94+
95+
resource "google_project_iam_member" "workload_identity_sa_bindings_additional_projects" {
96+
for_each = { for entry in local.sa_binding_additional_project : "${entry.project_id}.${entry.role_name}" => entry }
97+
98+
project = each.value.project_id
99+
role = each.value.role_name
100+
member = local.gcp_sa_fqn
101+
}

modules/workload-identity/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ variable "module_depends_on" {
107107
type = list(any)
108108
default = []
109109
}
110+
111+
variable "additional_projects" {
112+
description = "A list of roles to be added to the created service account for additional projects"
113+
type = map(list(string))
114+
default = {}
115+
}

0 commit comments

Comments
 (0)