Skip to content

Commit

Permalink
WI: add configurable k8s name
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Feb 10, 2020
1 parent 064f308 commit 6622005
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions modules/workload-identity/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
*/

locals {
k8s_sa_gcp_derived_name = "serviceAccount:${var.project_id}.svc.id.goog[${var.namespace}/${var.name}]"
k8s_sa_gcp_derived_name = "serviceAccount:${var.project_id}.svc.id.goog[${var.namespace}/${local.output_k8s_name}]"

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

resource "google_service_account" "cluster_service_account" {
account_id = var.name
display_name = substr("GCP SA bound to K8S SA ${local.k8s_sa_gcp_derived_name}", 0, 100)
display_name = substr("GCP SA bound to K8S SA ${local.k8s_given_name}", 0, 100)
project = var.project_id
}

Expand All @@ -40,6 +41,8 @@ resource "kubernetes_service_account" "main" {
}
}

# TODO: add the annotation to existing service accounts automatically

resource "google_service_account_iam_member" "main" {
service_account_id = google_service_account.cluster_service_account.name
role = "roles/iam.workloadIdentityUser"
Expand Down
6 changes: 6 additions & 0 deletions modules/workload-identity/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ variable "name" {
type = string
}

variable "k8s_sa_name" {
description = "Name for the existing Kubernetes service account"
type = string
default = null
}

variable "namespace" {
description = "Namespace for k8s service account"
default = "default"
Expand Down

0 comments on commit 6622005

Please sign in to comment.