Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make workload identity k8s SA configurable #430

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions modules/workload-identity/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
*/

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}]"
gcp_sa_email = google_service_account.cluster_service_account.email

# 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 +42,22 @@ resource "kubernetes_service_account" "main" {
}
}

module "annotate-sa" {
source = "terraform-google-modules/gcloud/google"
version = "~> 0.5"

platform = "linux"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be parameterized to support darwin gcloud?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I'm disabling download, it doesn't.

additional_components = ["kubectl"]
enabled = var.use_existing_k8s_sa
skip_download = true

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

destroy_cmd_entrypoint = "kubectl"
destroy_cmd_body = "annotate sa -n ${local.output_k8s_namespace} ${local.k8s_given_name} iam.gke.io/gcp-service-account-"
}

resource "google_service_account_iam_member" "main" {
service_account_id = google_service_account.cluster_service_account.name
role = "roles/iam.workloadIdentityUser"
Expand Down
2 changes: 1 addition & 1 deletion modules/workload-identity/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ output "k8s_service_account_namespace" {

output "gcp_service_account_email" {
description = "Email address of GCP service account."
value = google_service_account.cluster_service_account.email
value = local.gcp_sa_email
}

output "gcp_service_account_fqn" {
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