Skip to content

Commit fc589dd

Browse files
committed
Add suffix to cluster service account
The service account ID truncation may drop appended to the cluster name; this means that multiple clusters with a common prefix but different name may try to create service accounts with identical IDs and then fail. This commit resolves the issue by adding a suffix to the service account name to ensure uniqueness.
1 parent 567c586 commit fc589dd

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

autogen/sa.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ locals {
2121
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
2222
}
2323

24+
resource "random_string" "cluster_service_account_suffix" {
25+
upper = "false"
26+
lower = "true"
27+
special = "false"
28+
length = 4
29+
}
30+
2431
resource "google_service_account" "cluster_service_account" {
2532
count = "${var.service_account == "create" ? 1 : 0}"
2633
project = "${var.project_id}"
27-
account_id = "tf-gke-${substr(var.name, 0, min(20, length(var.name)))}"
34+
account_id = "tf-gke-${substr(var.name, 0, min(15, length(var.name)))}-${random_string.cluster_service_account_suffix.result}"
2835
display_name = "Terraform-managed service account for cluster ${var.name}"
2936
}
3037

modules/private-cluster/sa.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ locals {
2121
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
2222
}
2323

24+
resource "random_string" "cluster_service_account_suffix" {
25+
upper = "false"
26+
lower = "true"
27+
special = "false"
28+
length = 4
29+
}
30+
2431
resource "google_service_account" "cluster_service_account" {
2532
count = "${var.service_account == "create" ? 1 : 0}"
2633
project = "${var.project_id}"
27-
account_id = "tf-gke-${substr(var.name, 0, min(20, length(var.name)))}"
34+
account_id = "tf-gke-${substr(var.name, 0, min(15, length(var.name)))}-${random_string.cluster_service_account_suffix.result}"
2835
display_name = "Terraform-managed service account for cluster ${var.name}"
2936
}
3037

sa.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ locals {
2121
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
2222
}
2323

24+
resource "random_string" "cluster_service_account_suffix" {
25+
upper = "false"
26+
lower = "true"
27+
special = "false"
28+
length = 4
29+
}
30+
2431
resource "google_service_account" "cluster_service_account" {
2532
count = "${var.service_account == "create" ? 1 : 0}"
2633
project = "${var.project_id}"
27-
account_id = "tf-gke-${substr(var.name, 0, min(20, length(var.name)))}"
34+
account_id = "tf-gke-${substr(var.name, 0, min(15, length(var.name)))}-${random_string.cluster_service_account_suffix.result}"
2835
display_name = "Terraform-managed service account for cluster ${var.name}"
2936
}
3037

0 commit comments

Comments
 (0)