Skip to content

Commit 9f84cec

Browse files
feat: Hub submodule - add option to use existing service account to register clusters. (terraform-google-modules#678)
* use existing service account * count. use index * format and docs * add encoded to docs
1 parent 0b5bd3d commit 9f84cec

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

modules/hub/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ To deploy this config:
4141
| gke\_hub\_sa\_name | Name for the GKE Hub SA stored as a secret `creds-gcp` in the `gke-connect` namespace. | string | `"gke-hub-sa"` | no |
4242
| location | The location (zone or region) this cluster has been created in. | string | n/a | yes |
4343
| project\_id | The project in which the resource belongs. | string | n/a | yes |
44+
| sa\_private\_key | Private key for service account base64 encoded. Required only if `use_existing_sa` is set to `true`. | string | `"null"` | no |
4445
| skip\_gcloud\_download | Whether to skip downloading gcloud (assumes gcloud and kubectl already available outside the module) | bool | `"true"` | no |
46+
| use\_existing\_sa | Uses an existing service account to register membership. Requires sa_private_key | bool | `"false"` | no |
4547
| use\_tf\_google\_credentials\_env\_var | Optional GOOGLE_CREDENTIALS environment variable to be activated. | bool | `"false"` | no |
4648

4749
## Outputs

modules/hub/main.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,29 @@
1515
*/
1616

1717
locals {
18-
gke_hub_sa_key = google_service_account_key.gke_hub_key.private_key
18+
gke_hub_sa_key = var.use_existing_sa ? var.sa_private_key : google_service_account_key.gke_hub_key[0].private_key
1919
}
2020

2121
data "google_client_config" "default" {
2222
}
2323

2424
resource "google_service_account" "gke_hub_sa" {
25+
count = var.use_existing_sa ? 0 : 1
2526
account_id = var.gke_hub_sa_name
2627
project = var.project_id
2728
display_name = "Service Account for GKE Hub Registration"
2829
}
2930

3031
resource "google_project_iam_member" "gke_hub_member" {
32+
count = var.use_existing_sa ? 0 : 1
3133
project = var.project_id
3234
role = "roles/gkehub.connect"
33-
member = "serviceAccount:${google_service_account.gke_hub_sa.email}"
35+
member = "serviceAccount:${google_service_account.gke_hub_sa[0].email}"
3436
}
3537

3638
resource "google_service_account_key" "gke_hub_key" {
37-
service_account_id = google_service_account.gke_hub_sa.name
39+
count = var.use_existing_sa ? 0 : 1
40+
service_account_id = google_service_account.gke_hub_sa[0].name
3841
}
3942

4043
module "gke_hub_registration" {

modules/hub/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ variable "gke_hub_membership_name" {
6969
type = string
7070
default = "gke-hub-membership"
7171
}
72+
73+
variable "use_existing_sa" {
74+
description = "Uses an existing service account to register membership. Requires sa_private_key"
75+
type = bool
76+
default = false
77+
}
78+
79+
variable "sa_private_key" {
80+
description = "Private key for service account base64 encoded. Required only if `use_existing_sa` is set to `true`."
81+
type = string
82+
default = null
83+
}

0 commit comments

Comments
 (0)