Skip to content

Commit 6dc1eb1

Browse files
feat!: Added support for multi-project GKE Hub registration (#840)
* Added support for multi-project GKE Hub registration Added an optional variable HUB_PROJECT_ID which will allow you to specific a seperate GCP project for the GKE HUB than the project the cluster is deployed to. This included updating the 3 examples the leveraged the hub module. Issue: #837 * Adding service idenity resource for multi project deployments Ensuring that the Hub default Service Account exists when adding a cluster from outside the hub project Issue: #837 * Fixing bharathkkb comments Fixes #837 * Fix linting issue * Removed Google project data souce Issue: #837 * Adding upgrade documentation for this change. * Lint updates on readme. Co-authored-by: James Duncan <[email protected]> Co-authored-by: Bharath KKB <[email protected]>
1 parent d1fbef4 commit 6dc1eb1

File tree

8 files changed

+60
-23
lines changed

8 files changed

+60
-23
lines changed

docs/upgrading_to_v14.0.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ The module now uses the new ASM [installation script](https://cloud.google.com/s
4141
- Supports migrations from open source Istio 1.7 or 1.8 to ASM
4242

4343
Please see the script page for up to date details.
44+
45+
### GKE Hub Register & Unregister behaviour has changed
46+
47+
The [Hub submodule](https://github.com/terraform-google-modules/terraform-google-kubernetes-engine/tree/master/modules/hub) now supports registering a cluster to a Hub that is in a separate project. This is via the introduction of the `hub_project_id`.
48+
variable. If you specify this variable, the cluster will be registered to this project and the GKE cluster will be deployed in the project specified in the `project_id` variable.
49+
50+
To upgrade to the latest version, you will need to remove the state for the `run_destroy_command[0]` resource because, as of this release we register / unregister clusters using the `--gke-uri` option.
51+
52+
If you run into errors during upgrade, you can remove the state for the run_destroy_command resource by running:
53+
`terraform state rm module.hub.module.gke_hub_registration.null_resource.run_destroy_command[0]`

examples/simple_zonal_with_hub_kubeconfig/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ It creates a [kind](https://kind.sigs.k8s.io/) cluster, sets current kubecontext
99

1010
| Name | Description | Type | Default | Required |
1111
|------|-------------|------|---------|:--------:|
12-
| project\_id | The project ID (environ) to register the cluster in | `any` | n/a | yes |
12+
| project\_id | The project ID to host the cluster in | `any` | n/a | yes |
1313

1414
## Outputs
1515

examples/simple_zonal_with_hub_kubeconfig/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
*/
1616

1717
variable "project_id" {
18-
description = "The project ID (environ) to register the cluster in"
18+
description = "The project ID to host the cluster in"
1919
}

modules/hub/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ To deploy this config:
3939
| gcloud\_sdk\_version | The gcloud sdk version to use. Minimum required version is 293.0.0 | `string` | `"296.0.1"` | no |
4040
| gke\_hub\_membership\_name | Membership name that uniquely represents the cluster being registered on the Hub | `string` | `"gke-hub-membership"` | no |
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 |
42+
| hub\_project\_id | The project in which the GKE Hub belongs. | `string` | `""` | no |
4243
| labels | Comma separated labels in the format name=value to apply to cluster in the GCP Console. | `string` | `""` | no |
4344
| location | The location (zone or region) this cluster has been created in. | `string` | n/a | yes |
4445
| module\_depends\_on | List of modules or resources this module depends on. | `list` | `[]` | no |

modules/hub/main.tf

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
locals {
1818
gke_hub_sa_key = var.use_existing_sa ? var.sa_private_key : google_service_account_key.gke_hub_key[0].private_key
1919

20-
is_gke_flag = var.use_kubeconfig ? 0 : 1
20+
is_gke_flag = var.use_kubeconfig ? 0 : 1
21+
hub_project = var.hub_project_id == "" ? var.project_id : var.hub_project_id
22+
23+
cluster_uri = "https://container.googleapis.com/projects/${var.project_id}/locations/${var.location}/clusters/${var.cluster_name}"
2124
create_cmd_gke_entrypoint = "${path.module}/scripts/gke_hub_registration.sh"
22-
create_cmd_gke_body = "${local.is_gke_flag} ${var.gke_hub_membership_name} ${var.location} ${var.cluster_name} ${local.gke_hub_sa_key} ${var.project_id} ${var.labels}"
25+
create_cmd_gke_body = "${local.is_gke_flag} ${var.gke_hub_membership_name} ${local.gke_hub_sa_key} ${local.cluster_uri} ${local.hub_project} ${var.labels}"
2326
destroy_gke_entrypoint = "${path.module}/scripts/gke_hub_unregister.sh"
24-
destroy_gke_body = "${local.is_gke_flag} ${var.gke_hub_membership_name} ${var.location} ${var.cluster_name} ${var.project_id}"
27+
destroy_gke_body = "${local.is_gke_flag} ${var.gke_hub_membership_name} ${local.cluster_uri} ${local.hub_project}"
2528
}
2629

2730
data "google_client_config" "default" {
@@ -30,17 +33,38 @@ data "google_client_config" "default" {
3033
resource "google_service_account" "gke_hub_sa" {
3134
count = var.use_existing_sa ? 0 : 1
3235
account_id = var.gke_hub_sa_name
33-
project = var.project_id
36+
project = local.hub_project
3437
display_name = "Service Account for GKE Hub Registration"
3538
}
3639

3740
resource "google_project_iam_member" "gke_hub_member" {
3841
count = var.use_existing_sa ? 0 : 1
39-
project = var.project_id
42+
project = local.hub_project
4043
role = "roles/gkehub.connect"
4144
member = "serviceAccount:${google_service_account.gke_hub_sa[0].email}"
4245
}
4346

47+
resource "google_project_iam_member" "hub_service_agent_gke" {
48+
count = var.hub_project_id == "" ? 0 : 1
49+
project = var.project_id
50+
role = "roles/gkehub.serviceAgent"
51+
member = "serviceAccount:${google_project_service_identity.sa_gkehub[0].email}"
52+
}
53+
54+
resource "google_project_iam_member" "hub_service_agent_hub" {
55+
count = var.hub_project_id == "" ? 0 : 1
56+
project = local.hub_project
57+
role = "roles/gkehub.serviceAgent"
58+
member = "serviceAccount:${google_project_service_identity.sa_gkehub[0].email}"
59+
}
60+
61+
resource "google_project_service_identity" "sa_gkehub" {
62+
count = var.hub_project_id == "" ? 0 : 1
63+
provider = google-beta
64+
project = local.hub_project
65+
service = "gkehub.googleapis.com"
66+
}
67+
4468
resource "google_service_account_key" "gke_hub_key" {
4569
count = var.use_existing_sa ? 0 : 1
4670
service_account_id = google_service_account.gke_hub_sa[0].name

modules/hub/scripts/gke_hub_registration.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ fi
2222

2323
GKE_CLUSTER_FLAG=$1
2424
MEMBERSHIP_NAME=$2
25-
CLUSTER_LOCATION=$3
26-
CLUSTER_NAME=$4
27-
SERVICE_ACCOUNT_KEY=$5
28-
PROJECT_ID=$6
29-
LABELS=$7
25+
SERVICE_ACCOUNT_KEY=$3
26+
CLUSTER_URI=$4
27+
HUB_PROJECT_ID=$5
28+
LABELS=$6
3029

3130
#write temp key, cleanup at exit
3231
tmp_file=$(mktemp)
@@ -37,18 +36,18 @@ echo "${SERVICE_ACCOUNT_KEY}" | base64 ${B64_ARG} > "$tmp_file"
3736

3837
if [[ ${GKE_CLUSTER_FLAG} == 1 ]]; then
3938
echo "Registering GKE Cluster."
40-
gcloud container hub memberships register "${MEMBERSHIP_NAME}" --gke-cluster="${CLUSTER_LOCATION}"/"${CLUSTER_NAME}" --service-account-key-file="${tmp_file}" --project="${PROJECT_ID}" --quiet
39+
gcloud container hub memberships register "${MEMBERSHIP_NAME}" --gke-uri="${CLUSTER_URI}" --service-account-key-file="${tmp_file}" --project="${HUB_PROJECT_ID}" --quiet
4140
else
4241
echo "Registering a non-GKE Cluster. Using current-context to register Hub membership."
4342
#Get the kubeconfig
4443
CONTEXT=$(kubectl config current-context)
45-
gcloud container hub memberships register "${MEMBERSHIP_NAME}" --context="${CONTEXT}" --service-account-key-file="${tmp_file}" --project="${PROJECT_ID}" --quiet
44+
gcloud container hub memberships register "${MEMBERSHIP_NAME}" --context="${CONTEXT}" --service-account-key-file="${tmp_file}" --project="${HUB_PROJECT_ID}" --quiet
4645
fi
4746

4847

4948
# Add labels to the registered cluster
5049
if [ -z ${LABELS+x} ]; then
5150
echo "No hub labels to apply."
5251
else
53-
gcloud container hub memberships update "${MEMBERSHIP_NAME}" --update-labels "$LABELS" --project="${PROJECT_ID}"
52+
gcloud container hub memberships update "${MEMBERSHIP_NAME}" --update-labels "$LABELS" --project="${HUB_PROJECT_ID}"
5453
fi

modules/hub/scripts/gke_hub_unregister.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@
1515

1616
set -e
1717

18-
if [ "$#" -lt 5 ]; then
18+
if [ "$#" -lt 4 ]; then
1919
>&2 echo "Not all expected arguments set."
2020
exit 1
2121
fi
2222

2323
GKE_CLUSTER_FLAG=$1
2424
MEMBERSHIP_NAME=$2
25-
CLUSTER_LOCATION=$3
26-
CLUSTER_NAME=$4
27-
PROJECT_ID=$5
28-
29-
25+
CLUSTER_URI=$3
26+
HUB_PROJECT_ID=$4
3027

3128
if [[ ${GKE_CLUSTER_FLAG} == 1 ]]; then
3229
echo "Un-Registering GKE Cluster."
33-
gcloud container hub memberships unregister "${MEMBERSHIP_NAME}" --gke-cluster="${CLUSTER_LOCATION}"/"${CLUSTER_NAME}" --project "${PROJECT_ID}"
30+
gcloud container hub memberships unregister "${MEMBERSHIP_NAME}" --gke-uri="${CLUSTER_URI}" --project "${HUB_PROJECT_ID}"
3431
else
3532
echo "Un-Registering a non-GKE Cluster. Using current-context to unregister Hub membership."
3633
#Get Current context
3734
CONTEXT=$(kubectl config current-context)
38-
gcloud container hub memberships unregister "${MEMBERSHIP_NAME}" --context="${CONTEXT}" --project="${PROJECT_ID}"
35+
gcloud container hub memberships unregister "${MEMBERSHIP_NAME}" --context="${CONTEXT}" --project="${HUB_PROJECT_ID}"
3936
fi

modules/hub/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ variable "project_id" {
2929
type = string
3030
}
3131

32+
variable "hub_project_id" {
33+
description = "The project in which the GKE Hub belongs."
34+
type = string
35+
default = ""
36+
}
37+
3238
variable "location" {
3339
description = "The location (zone or region) this cluster has been created in."
3440
type = string

0 commit comments

Comments
 (0)