Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Hub submodule - add option to use existing service account to register clusters. #678

Merged
merged 4 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/hub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ To deploy this config:
| 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 |
| location | The location (zone or region) this cluster has been created in. | string | n/a | yes |
| project\_id | The project in which the resource belongs. | string | n/a | yes |
| sa\_private\_key | Private key for service account. Required only if `use_existing_sa` is set to `true`. | string | `"null"` | no |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call out the fact that this should be base64 encoded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That is a good idea.

| skip\_gcloud\_download | Whether to skip downloading gcloud (assumes gcloud and kubectl already available outside the module) | bool | `"true"` | no |
| use\_existing\_sa | Uses an existing service account to register membership. Requires sa_private_key | bool | `"false"` | no |
| use\_tf\_google\_credentials\_env\_var | Optional GOOGLE_CREDENTIALS environment variable to be activated. | bool | `"false"` | no |

## Outputs
Expand Down
9 changes: 6 additions & 3 deletions modules/hub/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@
*/

locals {
gke_hub_sa_key = google_service_account_key.gke_hub_key.private_key
gke_hub_sa_key = var.use_existing_sa ? var.sa_private_key : google_service_account_key.gke_hub_key[0].private_key
}

data "google_client_config" "default" {
}

resource "google_service_account" "gke_hub_sa" {
count = var.use_existing_sa ? 0 : 1
account_id = var.gke_hub_sa_name
project = var.project_id
display_name = "Service Account for GKE Hub Registration"
}

resource "google_project_iam_member" "gke_hub_member" {
count = var.use_existing_sa ? 0 : 1
project = var.project_id
role = "roles/gkehub.connect"
member = "serviceAccount:${google_service_account.gke_hub_sa.email}"
member = "serviceAccount:${google_service_account.gke_hub_sa[0].email}"
}

resource "google_service_account_key" "gke_hub_key" {
service_account_id = google_service_account.gke_hub_sa.name
count = var.use_existing_sa ? 0 : 1
service_account_id = google_service_account.gke_hub_sa[0].name
}

module "gke_hub_registration" {
Expand Down
12 changes: 12 additions & 0 deletions modules/hub/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ variable "gke_hub_membership_name" {
type = string
default = "gke-hub-membership"
}

variable "use_existing_sa" {
description = "Uses an existing service account to register membership. Requires sa_private_key"
type = bool
default = false
}

variable "sa_private_key" {
description = "Private key for service account. Required only if `use_existing_sa` is set to `true`."
type = string
default = null
}