terraform-google-kubernetes-engine
terraform-google-kubernetes-engine copied to clipboard
fix: handle random suffix only when `create_service_account` is false - Issue #1808
Description
This pull request addresses issue #1808, which pertains to the generation of random suffixes for service account names even when create_service_account is set to false. The changes ensure that the random suffix logic is conditioned on the create_service_account setting.
Changes Made
- Updated the
service_account_default_namelocal variable in sa.tf to conditionally include the random suffix only whencreate_service_accountis true. - Modified the
random_stringresource to have a count condition, ensuring it is created only whencreate_service_accountis true.
Testing
- [x] Ensure the
create_service_accountvariable is set tofalse. - [x] Verify that the
random_string.cluster_service_account_suffixresource is not created whencreate_service_accountisfalse. - [x] Confirm that the service account name is generated correctly without a random suffix in the absence of
random_string.cluster_service_account_suffix.
Testing codes
variable "name" {
type = string
default = ""
}
variable "create_service_account" {
type = bool
default = false
}
variable "service_account_name" {
type = string
default = ""
}
locals {
service_account_default_name = "tf-gke-${substr(var.name, 0, min(15, length(var.name)))}-${var.create_service_account ? random_string.cluster_service_account_suffix[0].result : ""}"
}
resource "random_string" "cluster_service_account_suffix" {
count = var.create_service_account ? 1 : 0
upper = false
lower = true
special = false
length = 4
}
resource "google_service_account" "cluster_service_account" {
count = var.create_service_account ? 1 : 0
project = var.project_id
account_id = var.service_account_name == "" ? local.service_account_default_name : var.service_account_name
display_name = "Terraform-managed service account for cluster ${var.name}"
}
Related Issue
- #1808
/gcbrun
Thanks - I was looking for the same thing.
Though I think you need to modify autogen/main/sa.tf.impl instead and then run make build.
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days