Skip to content

Commit 51fba38

Browse files
authored
feat: Allow workload identity submodule to update existing k8s SA. (#430)
1 parent 2cc64c8 commit 51fba38

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Diff for: modules/workload-identity/main.tf

+21-3
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,22 @@ resource "kubernetes_service_account" "main" {
4042
}
4143
}
4244

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

Diff for: modules/workload-identity/output.tf

+1-1
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" {

Diff for: modules/workload-identity/variables.tf

+6
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)