Skip to content

Commit e888b26

Browse files
committed
Manage multinic instance groups in all zones in the region
Without this patch the `52_regional_multinic` module makes the blind assumption zones a, b, and c exist in the region. This is a problem for us-east1 and europe-west1 which do not have zone a. This patch fixes the problem by changing 50_compute's zone input parameter to a list of zone name strings, `zones`. Similarly, `52_regional_multinic` is updated to automatically determine all available zones in the region and create multinic groups in each zone. Resolves: #20
1 parent fba5400 commit e888b26

File tree

6 files changed

+57
-124
lines changed

6 files changed

+57
-124
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
v2.0.0 - 2020-09-29
2+
===
3+
4+
* Fix [issue/20][issue20] `modules/52_regional_multinic` now deploys instance
5+
groups to all available zones in the specified region. Fixes error
6+
deploying to us-east1 and europe-west1 where there is no `a` zone.
7+
* Note, resources will be destroyed and re-created. The inputs to
8+
`52_regional_multinic` have *not* changed relative to v1.4.0. The `zone`
9+
input to `50_compute` is replaced by `zones`.
10+
111
v1.4.0 - 2020-09-28
212
===
313

@@ -65,3 +75,4 @@ v0.4.3
6575

6676
[issue10]: https://github.com/openinfrastructure/terraform-google-multinic/issues/10
6777
[guest76]: https://github.com/GoogleCloudPlatform/guest-agent/issues/76
78+
[issue20]: https://github.com/openinfrastructure/terraform-google-multinic/issues/20

examples/compute/main.tf

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ locals {
4747
}
4848

4949
# Manage the regional MIG formation
50-
module "multinic-a" {
50+
module "multinic" {
5151
source = "../../modules/50_compute"
5252

5353
num_instances = var.num_instances
5454
preemptible = var.preemptible
5555
autoscale = var.num_instances == 0 ? false : true
5656

5757
project_id = local.project_id
58-
name_prefix = "multinic-a"
58+
name_prefix = "multinic"
5959
region = local.region
60-
zone = "${local.region}-a"
60+
zones = [ "${local.region}-b", "${local.region}-c" ]
6161

6262
nic0_network = local.nic0_network
6363
nic0_project = local.project_id
@@ -73,34 +73,6 @@ module "multinic-a" {
7373
service_account_email = "multinic@${local.project_id}.iam.gserviceaccount.com"
7474
}
7575

76-
module "multinic-b" {
77-
source = "../../modules/50_compute"
78-
79-
num_instances = var.num_instances_b
80-
preemptible = var.preemptible
81-
autoscale = var.num_instances_b == 0 ? false : true
82-
83-
84-
project_id = local.project_id
85-
name_prefix = "multinic-b"
86-
region = local.region
87-
zone = "${local.region}-b"
88-
89-
nic0_network = local.nic0_network
90-
nic0_project = local.project_id
91-
nic0_subnet = local.nic0_subnet
92-
nic0_cidrs = [local.nic0_netblock]
93-
94-
nic1_network = local.nic1_network
95-
nic1_project = local.project_id
96-
nic1_subnet = local.nic1_subnet
97-
nic1_cidrs = [local.nic1_netblock]
98-
99-
# Note this is the auto-healing check, not the traffic check
100-
hc_self_link = google_compute_health_check.multinic-health.self_link
101-
service_account_email = "multinic@${local.project_id}.iam.gserviceaccount.com"
102-
}
103-
10476
# The "health" health check is used for auto-healing with the MIG. The
10577
# timeouts are longer to reduce the risk of removing an otherwise healthy
10678
# instance.
@@ -148,12 +120,11 @@ resource "google_compute_region_backend_service" "multinic-main" {
148120
region = local.region
149121
load_balancing_scheme = "INTERNAL"
150122

151-
backend {
152-
group = module.multinic-a.instance_group
153-
}
154-
155-
backend {
156-
group = module.multinic-b.instance_group
123+
dynamic "backend" {
124+
for_each = module.multinic.instance_groups
125+
content {
126+
group = backend.value
127+
}
157128
}
158129

159130
# Note this is the traffic health check, not the auto-healing check
@@ -169,12 +140,11 @@ resource "google_compute_region_backend_service" "multinic-transit" {
169140
region = local.region
170141
load_balancing_scheme = "INTERNAL"
171142

172-
backend {
173-
group = module.multinic-a.instance_group
174-
}
175-
176-
backend {
177-
group = module.multinic-b.instance_group
143+
dynamic "backend" {
144+
for_each = module.multinic.instance_groups
145+
content {
146+
group = backend.value
147+
}
178148
}
179149

180150
# Note this is the traffic health check, not the auto-healing check

modules/50_compute/main.tf

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ resource google_compute_instance_template "multinic" {
8282
}
8383

8484
resource "google_compute_instance_group_manager" "multinic" {
85+
for_each = toset(var.zones)
8586
project = var.project_id
86-
name = "${var.name_prefix}-${var.zone}"
87+
name = "${var.name_prefix}-${each.value}"
8788

8889
base_instance_name = var.name_prefix
8990

90-
zone = var.zone
91+
zone = each.value
9192

9293
update_policy {
9394
type = "PROACTIVE"
@@ -121,11 +122,11 @@ resource "google_compute_instance_group_manager" "multinic" {
121122
}
122123

123124
resource "google_compute_autoscaler" "multinic" {
124-
count = var.autoscale ? 1 : 0
125-
project = var.project_id
126-
name = "${var.name_prefix}-${var.zone}"
127-
zone = var.zone
128-
target = google_compute_instance_group_manager.multinic.id
125+
for_each = toset(var.autoscale ? var.zones : [])
126+
project = var.project_id
127+
name = "${var.name_prefix}-${each.value}"
128+
zone = each.value
129+
target = google_compute_instance_group_manager.multinic[each.value].id
129130

130131
autoscaling_policy {
131132
max_replicas = var.max_replicas

modules/50_compute/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
output "instance_group" {
15-
description = "The instance group intended for use with a google_compute_region_backend_service resource"
16-
value = google_compute_instance_group_manager.multinic.instance_group
14+
output "instance_groups" {
15+
description = "The instance groups intended for use with a google_compute_region_backend_service resource"
16+
value = { for k,v in google_compute_instance_group_manager.multinic : k => v.instance_group }
1717
}

modules/50_compute/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ variable "region" {
2727
type = string
2828
}
2929

30-
variable "zone" {
31-
description = "The zone containing the managed resources"
32-
type = string
30+
variable "zones" {
31+
description = "The zones containing the managed resources, for example ['us-west1-a', 'us-west1-b', 'us-west1-c']"
32+
type = list(string)
3333
}
3434

3535
variable "service_account_email" {

modules/52_regional_multinic/main.tf

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Manage the regional MIG formation
16-
module "multinic-a" {
17-
source = "../50_compute"
18-
19-
num_instances = var.num_instances
20-
preemptible = var.preemptible
21-
autoscale = var.num_instances == 0 ? false : true
22-
23-
project_id = var.project_id
24-
name_prefix = "multinic-${var.region}-a"
25-
region = var.region
26-
zone = "${var.region}-a"
27-
28-
nic0_project = var.project_id
29-
nic0_network = var.nic0_network
30-
nic0_subnet = var.nic0_subnet
31-
32-
nic1_project = var.project_id
33-
nic1_network = var.nic1_network
34-
nic1_subnet = var.nic1_subnet
35-
36-
hc_self_link = google_compute_health_check.multinic-health.self_link
37-
service_account_email = var.service_account_email
15+
data "google_compute_zones" "available" {
16+
project = var.project_id
17+
region = var.region
3818
}
3919

40-
module "multinic-b" {
41-
source = "../50_compute"
42-
43-
num_instances = var.num_instances
44-
preemptible = var.preemptible
45-
autoscale = var.num_instances == 0 ? false : true
46-
47-
project_id = var.project_id
48-
name_prefix = "multinic-${var.region}-b"
49-
region = var.region
50-
zone = "${var.region}-b"
51-
52-
nic0_project = var.project_id
53-
nic0_network = var.nic0_network
54-
nic0_subnet = var.nic0_subnet
55-
56-
nic1_project = var.project_id
57-
nic1_network = var.nic1_network
58-
nic1_subnet = var.nic1_subnet
59-
60-
hc_self_link = google_compute_health_check.multinic-health.self_link
61-
service_account_email = var.service_account_email
20+
locals {
21+
zones = data.google_compute_zones.available.names
6222
}
6323

64-
module "multinic-c" {
24+
# Manage the regional MIG formation
25+
module "multinic" {
6526
source = "../50_compute"
6627

6728
num_instances = var.num_instances
6829
preemptible = var.preemptible
6930
autoscale = var.num_instances == 0 ? false : true
7031

7132
project_id = var.project_id
72-
name_prefix = "multinic-${var.region}-c"
33+
name_prefix = "multinic-${var.region}"
7334
region = var.region
74-
zone = "${var.region}-c"
35+
zones = local.zones
7536

7637
nic0_project = var.project_id
7738
nic0_network = var.nic0_network
@@ -132,16 +93,11 @@ resource "google_compute_region_backend_service" "multinic-main" {
13293
region = var.region
13394
load_balancing_scheme = "INTERNAL"
13495

135-
backend {
136-
group = module.multinic-a.instance_group
137-
}
138-
139-
backend {
140-
group = module.multinic-b.instance_group
141-
}
142-
143-
backend {
144-
group = module.multinic-c.instance_group
96+
dynamic "backend" {
97+
for_each = module.multinic.instance_groups
98+
content {
99+
group = backend.value
100+
}
145101
}
146102

147103
# Note this is the traffic health check, not the auto-healing check
@@ -157,16 +113,11 @@ resource "google_compute_region_backend_service" "multinic-transit" {
157113
region = var.region
158114
load_balancing_scheme = "INTERNAL"
159115

160-
backend {
161-
group = module.multinic-a.instance_group
162-
}
163-
164-
backend {
165-
group = module.multinic-b.instance_group
166-
}
167-
168-
backend {
169-
group = module.multinic-c.instance_group
116+
dynamic "backend" {
117+
for_each = module.multinic.instance_groups
118+
content {
119+
group = backend.value
120+
}
170121
}
171122

172123
# Note this is the traffic health check, not the auto-healing check

0 commit comments

Comments
 (0)