Skip to content

Commit f3d0f16

Browse files
committed
feat: Allow workload identity submodule to update existing k8s SA.
1 parent 064f308 commit f3d0f16

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

modules/workload-identity/main.tf

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
*/
1616

1717
locals {
18-
k8s_sa_gcp_derived_name = "serviceAccount:${var.project_id}.svc.id.goog[${var.namespace}/${var.name}]"
18+
k8s_sa_gcp_derived_name = "serviceAccount:${var.project_id}.svc.id.goog[${var.namespace}/${local.output_k8s_name}]"
19+
gcp_sa_email = google_service_account.cluster_service_account.email
1920

2021
# This will cause terraform to block returning outputs until the service account is created
21-
output_k8s_name = var.use_existing_k8s_sa ? var.name : kubernetes_service_account.main[0].metadata[0].name
22+
k8s_given_name = var.k8s_sa_name != null ? var.k8s_sa_name : var.name
23+
output_k8s_name = var.use_existing_k8s_sa ? local.k8s_given_name : kubernetes_service_account.main[0].metadata[0].name
2224
output_k8s_namespace = var.use_existing_k8s_sa ? var.namespace : kubernetes_service_account.main[0].metadata[0].namespace
2325
}
2426

2527
resource "google_service_account" "cluster_service_account" {
2628
account_id = var.name
27-
display_name = substr("GCP SA bound to K8S SA ${local.k8s_sa_gcp_derived_name}", 0, 100)
29+
display_name = substr("GCP SA bound to K8S SA ${local.k8s_given_name}", 0, 100)
2830
project = var.project_id
2931
}
3032

@@ -40,6 +42,23 @@ resource "kubernetes_service_account" "main" {
4042
}
4143
}
4244

45+
# TODO: add the annotation to existing service accounts automatically
46+
module "annotate-sa" {
47+
source = "terraform-google-modules/gcloud/google"
48+
version = "~> 0.5"
49+
50+
platform = "linux"
51+
additional_components = ["kubectl"]
52+
enabled = var.use_existing_k8s_sa
53+
skip_download = true
54+
55+
create_cmd_entrypoint = "kubectl"
56+
create_cmd_body = "annotate sa -n ${local.output_k8s_namespace} ${local.k8s_given_name} iam.gke.io/gcp-service-account=${local.gcp_sa_email}"
57+
58+
destroy_cmd_entrypoint = "kubectl"
59+
destroy_cmd_body = "annotate sa -n ${local.output_k8s_namespace} ${local.k8s_given_name} iam.gke.io/gcp-service-account-"
60+
}
61+
4362
resource "google_service_account_iam_member" "main" {
4463
service_account_id = google_service_account.cluster_service_account.name
4564
role = "roles/iam.workloadIdentityUser"

modules/workload-identity/output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ output "k8s_service_account_namespace" {
2626

2727
output "gcp_service_account_email" {
2828
description = "Email address of GCP service account."
29-
value = google_service_account.cluster_service_account.email
29+
value = local.gcp_sa_email
3030
}
3131

3232
output "gcp_service_account_fqn" {

modules/workload-identity/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ variable "name" {
1919
type = string
2020
}
2121

22+
variable "k8s_sa_name" {
23+
description = "Name for the existing Kubernetes service account"
24+
type = string
25+
default = null
26+
}
27+
2228
variable "namespace" {
2329
description = "Namespace for k8s service account"
2430
default = "default"

0 commit comments

Comments
 (0)