Skip to content

Commit cccabcb

Browse files
authored
feat(safer-cluster): add create_service_account variable (#2138)
1 parent 373c969 commit cccabcb

File tree

9 files changed

+104
-15
lines changed

9 files changed

+104
-15
lines changed

autogen/safer-cluster/main.tf.tmpl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,16 @@ module "gke" {
119119
// All applications should run with an identity defined via Workload Identity anyway.
120120
// - Use a service account passed as a parameter to the module, in case the user
121121
// wants to maintain control of their service accounts.
122-
create_service_account = var.compute_engine_service_account == "" ? true : false
123122
service_account = var.compute_engine_service_account
124123
registry_project_ids = var.registry_project_ids
125124
grant_registry_access = var.grant_registry_access
126125

126+
// If create_service_account is explicitly set to false we short-circuit the
127+
// compute_engine_service_account check to potentially avoid an error (see variables.tf documentation).
128+
// Otherwise if true (the default), we check if compute_engine_service_account is set for backwards compatability
129+
// before the create_service_account variable was added.
130+
create_service_account = var.create_service_account == false ? var.create_service_account : (var.compute_engine_service_account == "" ? true : false)
131+
127132
issue_client_certificate = false
128133

129134
cluster_resource_labels = var.cluster_resource_labels
@@ -209,7 +214,7 @@ module "gke" {
209214
// Enabling vulnerability and audit for workloads
210215
workload_vulnerability_mode = var.workload_vulnerability_mode
211216
workload_config_audit_mode = var.workload_config_audit_mode
212-
217+
213218
// Enabling security posture
214219
security_posture_mode = var.security_posture_mode
215220
security_posture_vulnerability_mode = var.security_posture_vulnerability_mode

autogen/safer-cluster/variables.tf.tmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,16 @@ variable "authenticator_security_group" {
411411

412412
variable "compute_engine_service_account" {
413413
type = string
414-
description = "Use the given service account for nodes rather than creating a new dedicated service account."
414+
description = "Use the given service account for nodes rather than creating a new dedicated service account. If set then also set var.create_service_account to false to avoid 'value depends on resource attributes that cannot be determined until apply' errors."
415415
default = ""
416416
}
417417

418+
variable "create_service_account" {
419+
type = bool
420+
description = "Defines if service account specified to run nodes should be created. Explicitly set to false if var.compute_engine_service_account is set to avoid 'value depends on resource attributes that cannot be determined until apply' errors."
421+
default = true
422+
}
423+
418424
variable "enable_shielded_nodes" {
419425
type = bool
420426
description = "Enable Shielded Nodes features on all nodes in this cluster."

docs/upgrading_to_v34.0.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Upgrading to v34.0
2+
3+
The v34.0 release of _kubernetes-engine_ is a backwards incompatible release.
4+
5+
### safer-cluster modules: Added create_service_account variable
6+
7+
This only affects users of the `safer-cluster` modules that have set `var.compute_engine_service_account` to something other than the default `""`.
8+
9+
A variable `var.create_service_account` was added to the `safer-cluster` modules that when explicitly set to `false` avoids the following error withing the `private-cluster` modules:
10+
11+
```sh
12+
Error: Invalid count argument
13+
14+
on .terraform/modules/gke_cluster.gke/modules/beta-private-cluster/sa.tf line 35, in resource "random_string" "cluster_service_account_suffix":
15+
35: count = var.create_service_account && var.service_account_name == "" ? 1 : 0
16+
17+
The "count" value depends on resource attributes that cannot be determined
18+
until apply, so Terraform cannot predict how many instances will be created.
19+
To work around this, use the -target argument to first apply only the
20+
resources that the count depends on.
21+
```
22+
23+
This seems to happen if `var.compute_engine_service_account` is passed in, and the externally created service account is being created at the same time, so the name/email is not computed yet:
24+
25+
```terraform
26+
resource "google_service_account" "cluster_service_account" {
27+
project = var.project_id
28+
account_id = "tf-gke-${var.cluster_name}-${random_string.cluster_service_account_suffix.result}"
29+
display_name = "Terraform-managed service account for cluster ${var.cluster_name}"
30+
}
31+
32+
module "gke" {
33+
source = "terraform-google-modules/kubernetes-engine/google//modules/safer-cluster"
34+
version = "~> 33.0"
35+
36+
project_id = var.project_id
37+
name = var.cluster_name
38+
39+
create_service_account = false
40+
compute_engine_service_account = google_service_account.cluster_service_account.email
41+
}
42+
```
43+
44+
By explicitly passing a `var.create_service_account = false` it short circuits the calculations dependent on `var.service_account_name`:
45+
46+
```terraform
47+
resource "random_string" "cluster_service_account_suffix" {
48+
count = var.create_service_account && var.service_account_name == "" ? 1 : 0
49+
upper = false
50+
lower = true
51+
special = false
52+
length = 4
53+
}
54+
```

modules/safer-cluster-update-variant/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ For simplicity, we suggest using `roles/container.admin` and
209209
| cluster\_dns\_provider | Which in-cluster DNS provider should be used. PROVIDER\_UNSPECIFIED (default) or PLATFORM\_DEFAULT or CLOUD\_DNS. | `string` | `"PROVIDER_UNSPECIFIED"` | no |
210210
| cluster\_dns\_scope | The scope of access to cluster DNS records. DNS\_SCOPE\_UNSPECIFIED (default) or CLUSTER\_SCOPE or VPC\_SCOPE. | `string` | `"DNS_SCOPE_UNSPECIFIED"` | no |
211211
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | `map(string)` | `{}` | no |
212-
| compute\_engine\_service\_account | Use the given service account for nodes rather than creating a new dedicated service account. | `string` | `""` | no |
212+
| compute\_engine\_service\_account | Use the given service account for nodes rather than creating a new dedicated service account. If set then also set var.create\_service\_account to false to avoid 'value depends on resource attributes that cannot be determined until apply' errors. | `string` | `""` | no |
213213
| config\_connector | Whether ConfigConnector is enabled for this cluster. | `bool` | `false` | no |
214+
| create\_service\_account | Defines if service account specified to run nodes should be created. Explicitly set to false if var.compute\_engine\_service\_account is set to avoid 'value depends on resource attributes that cannot be determined until apply' errors. | `bool` | `true` | no |
214215
| database\_encryption | Application-layer Secrets Encryption settings. The object format is {state = string, key\_name = string}. Valid values of state are: "ENCRYPTED"; "DECRYPTED". key\_name is the name of a CloudKMS key. | `list(object({ state = string, key_name = string }))` | <pre>[<br> {<br> "key_name": "",<br> "state": "DECRYPTED"<br> }<br>]</pre> | no |
215216
| datapath\_provider | The desired datapath provider for this cluster. By default, `ADVANCED_DATAPATH` enables Dataplane-V2 feature. `DATAPATH_PROVIDER_UNSPECIFIED` enables the IPTables-based kube-proxy implementation as a fallback since upgrading to V2 requires a cluster re-creation. | `string` | `"ADVANCED_DATAPATH"` | no |
216217
| default\_max\_pods\_per\_node | The maximum number of pods to schedule per node | `number` | `110` | no |

modules/safer-cluster-update-variant/main.tf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ module "gke" {
115115
// All applications should run with an identity defined via Workload Identity anyway.
116116
// - Use a service account passed as a parameter to the module, in case the user
117117
// wants to maintain control of their service accounts.
118-
create_service_account = var.compute_engine_service_account == "" ? true : false
119-
service_account = var.compute_engine_service_account
120-
registry_project_ids = var.registry_project_ids
121-
grant_registry_access = var.grant_registry_access
118+
service_account = var.compute_engine_service_account
119+
registry_project_ids = var.registry_project_ids
120+
grant_registry_access = var.grant_registry_access
121+
122+
// If create_service_account is explicitly set to false we short-circuit the
123+
// compute_engine_service_account check to potentially avoid an error (see variables.tf documentation).
124+
// Otherwise if true (the default), we check if compute_engine_service_account is set for backwards compatability
125+
// before the create_service_account variable was added.
126+
create_service_account = var.create_service_account == false ? var.create_service_account : (var.compute_engine_service_account == "" ? true : false)
122127

123128
issue_client_certificate = false
124129

modules/safer-cluster-update-variant/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,16 @@ variable "authenticator_security_group" {
411411

412412
variable "compute_engine_service_account" {
413413
type = string
414-
description = "Use the given service account for nodes rather than creating a new dedicated service account."
414+
description = "Use the given service account for nodes rather than creating a new dedicated service account. If set then also set var.create_service_account to false to avoid 'value depends on resource attributes that cannot be determined until apply' errors."
415415
default = ""
416416
}
417417

418+
variable "create_service_account" {
419+
type = bool
420+
description = "Defines if service account specified to run nodes should be created. Explicitly set to false if var.compute_engine_service_account is set to avoid 'value depends on resource attributes that cannot be determined until apply' errors."
421+
default = true
422+
}
423+
418424
variable "enable_shielded_nodes" {
419425
type = bool
420426
description = "Enable Shielded Nodes features on all nodes in this cluster."

modules/safer-cluster/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ For simplicity, we suggest using `roles/container.admin` and
209209
| cluster\_dns\_provider | Which in-cluster DNS provider should be used. PROVIDER\_UNSPECIFIED (default) or PLATFORM\_DEFAULT or CLOUD\_DNS. | `string` | `"PROVIDER_UNSPECIFIED"` | no |
210210
| cluster\_dns\_scope | The scope of access to cluster DNS records. DNS\_SCOPE\_UNSPECIFIED (default) or CLUSTER\_SCOPE or VPC\_SCOPE. | `string` | `"DNS_SCOPE_UNSPECIFIED"` | no |
211211
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | `map(string)` | `{}` | no |
212-
| compute\_engine\_service\_account | Use the given service account for nodes rather than creating a new dedicated service account. | `string` | `""` | no |
212+
| compute\_engine\_service\_account | Use the given service account for nodes rather than creating a new dedicated service account. If set then also set var.create\_service\_account to false to avoid 'value depends on resource attributes that cannot be determined until apply' errors. | `string` | `""` | no |
213213
| config\_connector | Whether ConfigConnector is enabled for this cluster. | `bool` | `false` | no |
214+
| create\_service\_account | Defines if service account specified to run nodes should be created. Explicitly set to false if var.compute\_engine\_service\_account is set to avoid 'value depends on resource attributes that cannot be determined until apply' errors. | `bool` | `true` | no |
214215
| database\_encryption | Application-layer Secrets Encryption settings. The object format is {state = string, key\_name = string}. Valid values of state are: "ENCRYPTED"; "DECRYPTED". key\_name is the name of a CloudKMS key. | `list(object({ state = string, key_name = string }))` | <pre>[<br> {<br> "key_name": "",<br> "state": "DECRYPTED"<br> }<br>]</pre> | no |
215216
| datapath\_provider | The desired datapath provider for this cluster. By default, `ADVANCED_DATAPATH` enables Dataplane-V2 feature. `DATAPATH_PROVIDER_UNSPECIFIED` enables the IPTables-based kube-proxy implementation as a fallback since upgrading to V2 requires a cluster re-creation. | `string` | `"ADVANCED_DATAPATH"` | no |
216217
| default\_max\_pods\_per\_node | The maximum number of pods to schedule per node | `number` | `110` | no |

modules/safer-cluster/main.tf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ module "gke" {
115115
// All applications should run with an identity defined via Workload Identity anyway.
116116
// - Use a service account passed as a parameter to the module, in case the user
117117
// wants to maintain control of their service accounts.
118-
create_service_account = var.compute_engine_service_account == "" ? true : false
119-
service_account = var.compute_engine_service_account
120-
registry_project_ids = var.registry_project_ids
121-
grant_registry_access = var.grant_registry_access
118+
service_account = var.compute_engine_service_account
119+
registry_project_ids = var.registry_project_ids
120+
grant_registry_access = var.grant_registry_access
121+
122+
// If create_service_account is explicitly set to false we short-circuit the
123+
// compute_engine_service_account check to potentially avoid an error (see variables.tf documentation).
124+
// Otherwise if true (the default), we check if compute_engine_service_account is set for backwards compatability
125+
// before the create_service_account variable was added.
126+
create_service_account = var.create_service_account == false ? var.create_service_account : (var.compute_engine_service_account == "" ? true : false)
122127

123128
issue_client_certificate = false
124129

modules/safer-cluster/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,16 @@ variable "authenticator_security_group" {
411411

412412
variable "compute_engine_service_account" {
413413
type = string
414-
description = "Use the given service account for nodes rather than creating a new dedicated service account."
414+
description = "Use the given service account for nodes rather than creating a new dedicated service account. If set then also set var.create_service_account to false to avoid 'value depends on resource attributes that cannot be determined until apply' errors."
415415
default = ""
416416
}
417417

418+
variable "create_service_account" {
419+
type = bool
420+
description = "Defines if service account specified to run nodes should be created. Explicitly set to false if var.compute_engine_service_account is set to avoid 'value depends on resource attributes that cannot be determined until apply' errors."
421+
default = true
422+
}
423+
418424
variable "enable_shielded_nodes" {
419425
type = bool
420426
description = "Enable Shielded Nodes features on all nodes in this cluster."

0 commit comments

Comments
 (0)