Skip to content

Commit 5562cd6

Browse files
authored
feat: Add support for multiple registry projects (#815)
1 parent 259dbfb commit 5562cd6

File tree

44 files changed

+270
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+270
-136
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ Then perform the following commands on the root folder:
177177
| project\_id | The project ID to host the cluster in (required) | `string` | n/a | yes |
178178
| region | The region to host the cluster in (optional if zonal cluster / required if regional) | `string` | `null` | no |
179179
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | `bool` | `true` | no |
180-
| registry\_project\_id | Project holding the Google Container Registry. If empty, we use the cluster project. If grant\_registry\_access is true, storage.objectViewer role is assigned on this project. | `string` | `""` | no |
180+
| registry\_project\_id | Deprecated. Replaced by `registry_project_ids`. Still works for the purposes of backwards compatibility, but will be removed in a future version. | `string` | `""` | no |
181+
| registry\_project\_ids | Projects holding Google Container Registries. If empty, we use the cluster project. If a service account is created and the `grant_registry_access` variable is set to `true`, the `storage.objectViewer` role is assigned on these projects. | `list(string)` | `[]` | no |
181182
| release\_channel | The release channel of this cluster. Accepted values are `UNSPECIFIED`, `RAPID`, `REGULAR` and `STABLE`. Defaults to `UNSPECIFIED`. | `string` | `null` | no |
182183
| remove\_default\_node\_pool | Remove default node pool while setting up the cluster | `bool` | `false` | no |
183184
| resource\_usage\_export\_dataset\_id | The ID of a BigQuery Dataset for using BigQuery as the destination of resource usage export. | `string` | `""` | no |
@@ -282,7 +283,7 @@ following project roles:
282283
- roles/iam.serviceAccountUser
283284
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
284285

285-
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
286+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_ids` projects:
286287
- roles/resourcemanager.projectIamAdmin
287288

288289
### Enable APIs

autogen/main/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ following project roles:
241241
- roles/iam.serviceAccountUser
242242
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
243243

244-
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
244+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_ids` projects:
245245
- roles/resourcemanager.projectIamAdmin
246246

247247
### Enable APIs

autogen/main/sa.tf.tmpl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ locals {
2525
)
2626
// if user set var.service_account it will be used even if var.create_service_account==true, so service account will be created but not used
2727
service_account = (var.service_account == "" || var.service_account == "create") && var.create_service_account ? local.service_account_list[0] : var.service_account
28+
29+
registry_projects_list = compact(
30+
length(var.registry_project_ids) == 0 && var.registry_project_id == ""
31+
? [var.project_id]
32+
: concat([var.registry_project_id], var.registry_project_ids)
33+
)
2834
}
2935

3036
resource "random_string" "cluster_service_account_suffix" {
@@ -70,15 +76,15 @@ resource "google_project_iam_member" "cluster_service_account-resourceMetadata-w
7076
}
7177

7278
resource "google_project_iam_member" "cluster_service_account-gcr" {
73-
count = var.create_service_account && var.grant_registry_access ? 1 : 0
74-
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
75-
role = "roles/storage.objectViewer"
76-
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
79+
for_each = var.create_service_account && var.grant_registry_access ? toset(local.registry_projects_list) : []
80+
project = each.key
81+
role = "roles/storage.objectViewer"
82+
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
7783
}
7884

7985
resource "google_project_iam_member" "cluster_service_account-artifact-registry" {
80-
count = var.create_service_account && var.grant_registry_access ? 1 : 0
81-
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
82-
role = "roles/artifactregistry.reader"
83-
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
86+
for_each = var.create_service_account && var.grant_registry_access ? toset(local.registry_projects_list) : []
87+
project = each.key
88+
role = "roles/artifactregistry.reader"
89+
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
8490
}

autogen/main/variables.tf.tmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,16 @@ variable "grant_registry_access" {
360360

361361
variable "registry_project_id" {
362362
type = string
363-
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
363+
description = "Deprecated. Replaced by `registry_project_ids`. Still works for the purposes of backwards compatibility, but will be removed in a future version."
364364
default = ""
365365
}
366366

367+
variable "registry_project_ids" {
368+
type = list(string)
369+
description = "Projects holding Google Container Registries. If empty, we use the cluster project. If a service account is created and the `grant_registry_access` variable is set to `true`, the `storage.objectViewer` role is assigned on these projects."
370+
default = []
371+
}
372+
367373
variable "service_account" {
368374
type = string
369375
description = "The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created."

autogen/safer-cluster/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ developers, which mostly just want to deploy and debug applications.
5353
own projects, so that they can be administered independently (e.g., dev cluster;
5454
production clusters; staging clusters should go in different projects.)
5555

56-
- *A shared GCR project (`registry_project_id`):* all clusters can share the same GCR project.
56+
- *Shared GCR projects (`registry_project_ids`):* all clusters can share the same
57+
GCR projects.
5758

5859
- Easier to share images between environments. The same image could be
5960
progressively rolled-out in dev, staging, and then production.
@@ -93,7 +94,7 @@ The Safer Cluster setup relies on several service accounts:
9394

9495
```
9596
create_service_account = true
96-
registry_project_id = <the project id for your GCR project>
97+
registry_project_ids = [<the project id for your GCR project>]
9798
grant_registry_access = true
9899
```
99100

autogen/safer-cluster/main.tf.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module "gke" {
101101
create_service_account = var.compute_engine_service_account == "" ? true : false
102102
service_account = var.compute_engine_service_account
103103
registry_project_id = var.registry_project_id
104+
registry_project_ids = var.registry_project_ids
104105
grant_registry_access = var.grant_registry_access
105106

106107
// Basic Auth disabled

autogen/safer-cluster/variables.tf.tmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,16 @@ variable "grant_registry_access" {
210210

211211
variable "registry_project_id" {
212212
type = string
213-
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
213+
description = "Deprecated. Replaced by `registry_project_ids`. Still works for the purposes of backwards compatibility, but will be removed in a future version."
214214
default = ""
215215
}
216216

217+
variable "registry_project_ids" {
218+
type = list(string)
219+
description = "Projects holding Google Container Registries. If empty, we use the cluster project. If a service account is created and the `grant_registry_access` variable is set to `true`, the `storage.objectViewer` role is assigned on these projects."
220+
default = []
221+
}
222+
217223
variable "cluster_resource_labels" {
218224
type = map(string)
219225
description = "The GCE resource labels (a map of key/value pairs) to be applied to the cluster"

examples/workload_metadata_config/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module "gke" {
5151
ip_range_services = var.ip_range_services
5252
create_service_account = true
5353
grant_registry_access = true
54-
registry_project_id = var.registry_project_id
54+
registry_project_ids = var.registry_project_ids
5555
enable_private_endpoint = true
5656
enable_private_nodes = true
5757
master_ipv4_cidr_block = "172.16.0.0/28"

examples/workload_metadata_config/outputs.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ output "client_token" {
2525
}
2626

2727
output "ca_certificate" {
28-
value = module.gke.ca_certificate
28+
sensitive = true
29+
value = module.gke.ca_certificate
2930
}
3031

3132
output "service_account" {

examples/workload_metadata_config/variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ variable "ip_range_services" {
4848
description = "The secondary ip range to use for services"
4949
}
5050

51-
variable "registry_project_id" {
52-
description = "Project name for the GCR registry"
51+
variable "registry_project_ids" {
52+
description = "Project names for GCR registries"
53+
type = list(string)
5354
}

0 commit comments

Comments
 (0)