Skip to content

Commit 5bc6905

Browse files
committed
net-glb: Added support for regional external HTTP(s) load balancing.
1 parent 79c3327 commit 5bc6905

14 files changed

+1380
-167
lines changed

modules/net-glb/README.md

+52-12
Large diffs are not rendered by default.

modules/net-glb/backend-services.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ resource "google_compute_backend_bucket" "bucket" {
6060
}
6161

6262
resource "google_compute_backend_service" "group" {
63-
for_each = local.backend_services_group
63+
for_each = var.region == null ? local.backend_services_group : {}
6464
name = "${var.name}-${each.key}"
6565
project = var.project_id
6666
description = "Terraform managed."
@@ -208,3 +208,4 @@ resource "google_compute_backend_service" "group" {
208208
}
209209
}
210210
}
211+

modules/net-glb/global-forwarding-rule.tf

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,34 @@
1717
# tfdoc:file:description Global address and forwarding rule.
1818

1919
locals {
20-
ip_address = (
21-
var.reserve_ip_address
20+
ip_address = var.region == null ? (
21+
var.region == null && var.reserve_ip_address
2222
? google_compute_global_address.static_ip.0.id
2323
: null
24-
)
24+
) : null
2525

2626
port_range = coalesce(
2727
var.global_forwarding_rule_config.port_range,
2828
var.https ? "443" : "80"
2929
)
3030

31-
target = (
31+
target = var.region == null ? (
3232
var.https
3333
? google_compute_target_https_proxy.https.0.id
3434
: google_compute_target_http_proxy.http.0.id
35-
)
35+
) : null
3636
}
3737

3838
resource "google_compute_global_address" "static_ip" {
39-
count = var.reserve_ip_address ? 1 : 0
39+
count = var.region == null && var.reserve_ip_address ? 1 : 0
4040
provider = google-beta
4141
name = var.name
4242
project = var.project_id
4343
description = "Terraform managed."
4444
}
4545

4646
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
47+
count = var.region == null ? 1 : 0
4748
provider = google-beta
4849
name = var.name
4950
project = var.project_id

modules/net-glb/health-checks.tf

+104-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ locals {
4848
}
4949

5050
resource "google_compute_health_check" "health_check" {
51-
for_each = local.health_checks_config
51+
for_each = var.region == null ? local.health_checks_config : {}
5252
provider = google-beta
5353
name = "${var.name}-${each.key}"
5454
project = var.project_id
@@ -148,3 +148,106 @@ resource "google_compute_health_check" "health_check" {
148148
}
149149
}
150150
}
151+
152+
resource "google_compute_region_health_check" "health_check" {
153+
for_each = var.region != null ? local.health_checks_config : {}
154+
provider = google-beta
155+
name = "${var.name}-${each.key}"
156+
project = var.project_id
157+
region = var.region
158+
description = "Terraform managed."
159+
check_interval_sec = try(each.value.options.check_interval_sec, null)
160+
healthy_threshold = try(each.value.options.healthy_threshold, null)
161+
timeout_sec = try(each.value.options.timeout_sec, null)
162+
unhealthy_threshold = try(each.value.options.unhealthy_threshold, null)
163+
164+
dynamic "http_health_check" {
165+
for_each = (
166+
try(each.value.type, null) == "http" || try(each.value.type, null) == null
167+
? { 1 = 1 }
168+
: {}
169+
)
170+
content {
171+
host = try(each.value.check.host, null)
172+
port = try(each.value.check.port, null)
173+
port_name = try(each.value.check.port_name, null)
174+
port_specification = try(each.value.check.port_specification, null)
175+
proxy_header = try(each.value.check.proxy_header, null)
176+
request_path = try(each.value.check.request_path, null)
177+
response = try(each.value.check.response, null)
178+
}
179+
}
180+
181+
dynamic "https_health_check" {
182+
for_each = (
183+
try(each.value.type, null) == "https" || try(each.value.type, null) == null
184+
? { 1 = 1 }
185+
: {}
186+
)
187+
content {
188+
host = try(each.value.check.host, null)
189+
port = try(each.value.check.port, null)
190+
port_name = try(each.value.check.port_name, null)
191+
port_specification = try(each.value.check.port_specification, null)
192+
proxy_header = try(each.value.check.proxy_header, null)
193+
request_path = try(each.value.check.request_path, null)
194+
response = try(each.value.check.response, null)
195+
}
196+
}
197+
198+
dynamic "tcp_health_check" {
199+
for_each = (
200+
try(each.value.type, null) == "tcp" || try(each.value.type, null) == null
201+
? { 1 = 1 }
202+
: {}
203+
)
204+
content {
205+
port = try(each.value.check.port, null)
206+
port_name = try(each.value.check.port_name, null)
207+
port_specification = try(each.value.check.port_specification, null)
208+
proxy_header = try(each.value.check.proxy_header, null)
209+
request = try(each.value.check.request, null)
210+
response = try(each.value.check.response, null)
211+
}
212+
}
213+
214+
dynamic "ssl_health_check" {
215+
for_each = (
216+
try(each.value.type, null) == "ssl" || try(each.value.type, null) == null
217+
? { 1 = 1 }
218+
: {}
219+
)
220+
content {
221+
port = try(each.value.check.port, null)
222+
port_name = try(each.value.check.port_name, null)
223+
port_specification = try(each.value.check.port_specification, null)
224+
proxy_header = try(each.value.check.proxy_header, null)
225+
request = try(each.value.check.request, null)
226+
response = try(each.value.check.response, null)
227+
}
228+
}
229+
230+
dynamic "http2_health_check" {
231+
for_each = (
232+
try(each.value.type, null) == "http2" || try(each.value.type, null) == null
233+
? { 1 = 1 }
234+
: {}
235+
)
236+
content {
237+
host = try(each.value.check.host, null)
238+
port = try(each.value.check.port, null)
239+
port_name = try(each.value.check.port_name, null)
240+
port_specification = try(each.value.check.port_specification, null)
241+
proxy_header = try(each.value.check.proxy_header, null)
242+
request_path = try(each.value.check.request_path, null)
243+
response = try(each.value.check.response, null)
244+
}
245+
}
246+
247+
dynamic "log_config" {
248+
for_each = try(each.value.logging, false) ? { 0 = 0 } : {}
249+
content {
250+
enable = true
251+
}
252+
}
253+
}

modules/net-glb/outputs.tf

+8-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ output "ssl_certificates" {
4343

4444
output "ip_address" {
4545
description = "The reserved global IP address."
46-
value = try(google_compute_global_address.static_ip[0].address, null)
46+
value = var.region == null ? try(google_compute_global_address.static_ip.0.address, null) : try(google_compute_address.static_ip.0.address, null)
4747
}
4848

4949
output "ip_address_self_link" {
5050
description = "The URI of the reserved global IP address."
51-
value = google_compute_global_forwarding_rule.forwarding_rule.ip_address
51+
value = var.region == null ? google_compute_global_forwarding_rule.forwarding_rule.0.ip_address : google_compute_forwarding_rule.forwarding_rule.0.ip_address
5252
}
5353

5454
output "target_proxy" {
@@ -61,5 +61,10 @@ output "target_proxy" {
6161

6262
output "global_forwarding_rule" {
6363
description = "The global forwarding rule."
64-
value = google_compute_global_forwarding_rule.forwarding_rule
64+
value = var.region == null ? google_compute_global_forwarding_rule.forwarding_rule.0 : null
65+
}
66+
67+
output "forwarding_rule" {
68+
description = "The regional forwarding rule."
69+
value = var.region == null ? google_compute_global_forwarding_rule.forwarding_rule.0 : google_compute_forwarding_rule.forwarding_rule.0
6570
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
# tfdoc:file:description Bucket and group backend services for regional load balancers.
18+
19+
resource "google_compute_region_backend_service" "group" {
20+
for_each = var.region != null ? local.backend_services_group : {}
21+
name = "${var.name}-${each.key}"
22+
project = var.project_id
23+
region = var.region
24+
description = "Terraform managed."
25+
affinity_cookie_ttl_sec = try(each.value.group_config.options.affinity_cookie_ttl_sec, null)
26+
enable_cdn = try(each.value.enable_cdn, null)
27+
connection_draining_timeout_sec = try(each.value.group_config.options.connection_draining_timeout_sec, null)
28+
load_balancing_scheme = try(each.value.group_config.options.load_balancing_scheme, null)
29+
locality_lb_policy = try(each.value.group_config.options.locality_lb_policy, null)
30+
port_name = try(each.value.group_config.options.port_name, null)
31+
protocol = try(each.value.group_config.options.protocol, null)
32+
session_affinity = try(each.value.group_config.options.session_affinity, null)
33+
timeout_sec = try(each.value.group_config.options.timeout_sec, null)
34+
35+
# If no health checks are defined, use the default one.
36+
# Otherwise, look in the health_checks_config map.
37+
# Otherwise, use the health_check id as is (already existing).
38+
health_checks = (
39+
try(length(each.value.group_config.health_checks), 0) == 0
40+
? try(
41+
[google_compute_region_health_check.health_check["default"].id],
42+
null
43+
)
44+
: [
45+
for hc in each.value.group_config.health_checks :
46+
try(google_compute_region_health_check.health_check[hc].id, hc)
47+
]
48+
)
49+
50+
dynamic "backend" {
51+
for_each = try(each.value.group_config.backends, [])
52+
content {
53+
balancing_mode = try(backend.value.options.balancing_mode, null)
54+
capacity_scaler = try(backend.value.options.capacity_scaler, null)
55+
group = try(backend.value.group, null)
56+
max_connections = try(backend.value.options.max_connections, null)
57+
max_connections_per_instance = try(backend.value.options.max_connections_per_instance, null)
58+
max_connections_per_endpoint = try(backend.value.options.max_connections_per_endpoint, null)
59+
max_rate = try(backend.value.options.max_rate, null)
60+
max_rate_per_instance = try(backend.value.options.max_rate_per_instance, null)
61+
max_rate_per_endpoint = try(backend.value.options.max_rate_per_endpoint, null)
62+
max_utilization = try(backend.value.options.max_utilization, null)
63+
}
64+
}
65+
66+
dynamic "circuit_breakers" {
67+
for_each = (
68+
try(each.value.group_config.options.circuit_breakers, null) == null
69+
? []
70+
: [each.value.group_config.options.circuit_breakers]
71+
)
72+
iterator = cb
73+
content {
74+
max_requests_per_connection = try(cb.value.max_requests_per_connection, null)
75+
max_connections = try(cb.value.max_connections, null)
76+
max_pending_requests = try(cb.value.max_pending_requests, null)
77+
max_requests = try(cb.value.max_requests, null)
78+
max_retries = try(cb.value.max_retries, null)
79+
}
80+
}
81+
82+
dynamic "consistent_hash" {
83+
for_each = (
84+
try(each.value.group_config.options.consistent_hash, null) == null
85+
? []
86+
: [each.value.group_config.options.consistent_hash]
87+
)
88+
content {
89+
http_header_name = try(consistent_hash.value.http_header_name, null)
90+
minimum_ring_size = try(consistent_hash.value.minimum_ring_size, null)
91+
92+
dynamic "http_cookie" {
93+
for_each = try(consistent_hash.value.http_cookie, null) == null ? [] : [consistent_hash.value.http_cookie]
94+
content {
95+
name = try(http_cookie.value.name, null)
96+
path = try(http_cookie.value.path, null)
97+
98+
dynamic "ttl" {
99+
for_each = try(consistent_hash.value.ttl, null) == null ? [] : [consistent_hash.value.ttl]
100+
content {
101+
seconds = try(ttl.value.seconds, null) # Must be from 0 to 315,576,000,000 inclusive
102+
nanos = try(ttl.value.nanos, null) # Must be from 0 to 999,999,999 inclusive
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}
109+
110+
dynamic "cdn_policy" {
111+
for_each = (
112+
try(each.value.cdn_policy, null) == null
113+
? []
114+
: [each.value.cdn_policy]
115+
)
116+
iterator = cdn_policy
117+
content {
118+
signed_url_cache_max_age_sec = try(cdn_policy.value.signed_url_cache_max_age_sec, null)
119+
default_ttl = try(cdn_policy.value.default_ttl, null)
120+
max_ttl = try(cdn_policy.value.max_ttl, null)
121+
client_ttl = try(cdn_policy.value.client_ttl, null)
122+
negative_caching = try(cdn_policy.value.negative_caching, null)
123+
cache_mode = try(cdn_policy.value.cache_mode, null)
124+
serve_while_stale = try(cdn_policy.value.serve_while_stale, null)
125+
126+
dynamic "negative_caching_policy" {
127+
for_each = (
128+
try(cdn_policy.value.negative_caching_policy, null) == null
129+
? []
130+
: [cdn_policy.value.negative_caching_policy]
131+
)
132+
iterator = ncp
133+
content {
134+
code = try(ncp.value.code, null)
135+
}
136+
}
137+
}
138+
}
139+
140+
dynamic "iap" {
141+
for_each = (
142+
try(each.value.group_config.options.iap, null) == null
143+
? []
144+
: [each.value.group_config.options.iap]
145+
)
146+
content {
147+
oauth2_client_id = try(iap.value.oauth2_client_id, null)
148+
oauth2_client_secret = try(iap.value.oauth2_client_secret, null) # sensitive
149+
oauth2_client_secret_sha256 = try(iap.value.oauth2_client_secret_sha256, null) # sensitive
150+
}
151+
}
152+
153+
dynamic "log_config" {
154+
for_each = (
155+
try(each.value.group_config.log_config, null) == null
156+
? []
157+
: [each.value.group_config.log_config]
158+
)
159+
content {
160+
enable = try(log_config.value.enable, null)
161+
sample_rate = try(log_config.value.sample_rate, null)
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)