Skip to content

Commit d597d4a

Browse files
authored
Merge pull request #111 from terraform-google-modules/adrienthebo/bugfix/service-account-suffix
Add suffix to cluster service account
2 parents 567c586 + 1c7fda7 commit d597d4a

File tree

17 files changed

+89
-4
lines changed

17 files changed

+89
-4
lines changed

autogen/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,8 @@ output "node_pools_versions" {
107107
description = "List of node pools versions"
108108
value = "${local.cluster_node_pools_versions}"
109109
}
110+
111+
output "service_account" {
112+
description = "The service account to default running nodes as if not overridden in `node_pools`."
113+
value = "${local.service_account}"
114+
}

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

examples/deploy_service/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/node_pool/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/shared_vpc/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/simple_regional/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/simple_regional_private/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/simple_zonal/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/simple_zonal_private/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

examples/stub_domains/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "client_token" {
2727
output "ca_certificate" {
2828
value = "${module.gke.ca_certificate}"
2929
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

modules/private-cluster/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,9 @@ output "node_pools_names" {
106106
output "node_pools_versions" {
107107
description = "List of node pools versions"
108108
value = "${local.cluster_node_pools_versions}"
109+
}
110+
111+
output "service_account" {
112+
description = "The service account to default running nodes as if not overridden in `node_pools`."
113+
value = "${local.service_account}"
109114
}

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

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,9 @@ output "node_pools_names" {
106106
output "node_pools_versions" {
107107
description = "List of node pools versions"
108108
value = "${local.cluster_node_pools_versions}"
109+
}
110+
111+
output "service_account" {
112+
description = "The service account to default running nodes as if not overridden in `node_pools`."
113+
value = "${local.service_account}"
109114
}

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

test/fixtures/shared/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ output "ca_certificate" {
7777
description = "The cluster CA certificate"
7878
value = "${module.example.ca_certificate}"
7979
}
80+
81+
output "service_account" {
82+
description = "The service account to default running nodes as if not overridden in `node_pools`."
83+
value = "${module.example.service_account}"
84+
}

test/integration/simple_zonal/controls/gcloud.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
project_id = attribute('project_id')
1616
location = attribute('location')
1717
cluster_name = attribute('cluster_name')
18+
service_account = attribute('service_account')
1819

1920
credentials_path = attribute('credentials_path')
2021
ENV['CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE'] = credentials_path
@@ -83,7 +84,7 @@
8384
expect(node_pools).to include(
8485
including(
8586
"config" => including(
86-
"serviceAccount" => starting_with("tf-gke-simple-zonal-cluster@"),
87+
"serviceAccount" => service_account,
8788
),
8889
),
8990
)

test/integration/simple_zonal/inspec.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ attributes:
2121
- name: client_token
2222
required: true
2323
type: string
24+
- name: service_account
25+
required: true
26+
type: string

0 commit comments

Comments
 (0)