Skip to content

Commit

Permalink
net-glb: Added support for regional external HTTP(s) load balancing.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosmo committed Aug 25, 2022
1 parent 79c3327 commit 90e3995
Show file tree
Hide file tree
Showing 13 changed files with 1,220 additions and 171 deletions.
64 changes: 52 additions & 12 deletions modules/net-glb/README.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion modules/net-glb/backend-services.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ resource "google_compute_backend_bucket" "bucket" {
}

resource "google_compute_backend_service" "group" {
for_each = local.backend_services_group
for_each = var.region == null ? local.backend_services_group : {}
name = "${var.name}-${each.key}"
project = var.project_id
description = "Terraform managed."
Expand Down Expand Up @@ -208,3 +208,4 @@ resource "google_compute_backend_service" "group" {
}
}
}

13 changes: 7 additions & 6 deletions modules/net-glb/global-forwarding-rule.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,34 @@
# tfdoc:file:description Global address and forwarding rule.

locals {
ip_address = (
var.reserve_ip_address
ip_address = var.region == null ? (
var.region == null && var.reserve_ip_address
? google_compute_global_address.static_ip.0.id
: null
)
) : null

port_range = coalesce(
var.global_forwarding_rule_config.port_range,
var.https ? "443" : "80"
)

target = (
target = var.region == null ? (
var.https
? google_compute_target_https_proxy.https.0.id
: google_compute_target_http_proxy.http.0.id
)
) : null
}

resource "google_compute_global_address" "static_ip" {
count = var.reserve_ip_address ? 1 : 0
count = var.region == null && var.reserve_ip_address ? 1 : 0
provider = google-beta
name = var.name
project = var.project_id
description = "Terraform managed."
}

resource "google_compute_global_forwarding_rule" "forwarding_rule" {
count = var.region == null ? 1 : 0
provider = google-beta
name = var.name
project = var.project_id
Expand Down
105 changes: 104 additions & 1 deletion modules/net-glb/health-checks.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ locals {
}

resource "google_compute_health_check" "health_check" {
for_each = local.health_checks_config
for_each = var.region == null ? local.health_checks_config : {}
provider = google-beta
name = "${var.name}-${each.key}"
project = var.project_id
Expand Down Expand Up @@ -148,3 +148,106 @@ resource "google_compute_health_check" "health_check" {
}
}
}

resource "google_compute_region_health_check" "health_check" {
for_each = var.region != null ? local.health_checks_config : {}
provider = google-beta
name = "${var.name}-${each.key}"
project = var.project_id
region = var.region
description = "Terraform managed."
check_interval_sec = try(each.value.options.check_interval_sec, null)
healthy_threshold = try(each.value.options.healthy_threshold, null)
timeout_sec = try(each.value.options.timeout_sec, null)
unhealthy_threshold = try(each.value.options.unhealthy_threshold, null)

dynamic "http_health_check" {
for_each = (
try(each.value.type, null) == "http" || try(each.value.type, null) == null
? { 1 = 1 }
: {}
)
content {
host = try(each.value.check.host, null)
port = try(each.value.check.port, null)
port_name = try(each.value.check.port_name, null)
port_specification = try(each.value.check.port_specification, null)
proxy_header = try(each.value.check.proxy_header, null)
request_path = try(each.value.check.request_path, null)
response = try(each.value.check.response, null)
}
}

dynamic "https_health_check" {
for_each = (
try(each.value.type, null) == "https" || try(each.value.type, null) == null
? { 1 = 1 }
: {}
)
content {
host = try(each.value.check.host, null)
port = try(each.value.check.port, null)
port_name = try(each.value.check.port_name, null)
port_specification = try(each.value.check.port_specification, null)
proxy_header = try(each.value.check.proxy_header, null)
request_path = try(each.value.check.request_path, null)
response = try(each.value.check.response, null)
}
}

dynamic "tcp_health_check" {
for_each = (
try(each.value.type, null) == "tcp" || try(each.value.type, null) == null
? { 1 = 1 }
: {}
)
content {
port = try(each.value.check.port, null)
port_name = try(each.value.check.port_name, null)
port_specification = try(each.value.check.port_specification, null)
proxy_header = try(each.value.check.proxy_header, null)
request = try(each.value.check.request, null)
response = try(each.value.check.response, null)
}
}

dynamic "ssl_health_check" {
for_each = (
try(each.value.type, null) == "ssl" || try(each.value.type, null) == null
? { 1 = 1 }
: {}
)
content {
port = try(each.value.check.port, null)
port_name = try(each.value.check.port_name, null)
port_specification = try(each.value.check.port_specification, null)
proxy_header = try(each.value.check.proxy_header, null)
request = try(each.value.check.request, null)
response = try(each.value.check.response, null)
}
}

dynamic "http2_health_check" {
for_each = (
try(each.value.type, null) == "http2" || try(each.value.type, null) == null
? { 1 = 1 }
: {}
)
content {
host = try(each.value.check.host, null)
port = try(each.value.check.port, null)
port_name = try(each.value.check.port_name, null)
port_specification = try(each.value.check.port_specification, null)
proxy_header = try(each.value.check.proxy_header, null)
request_path = try(each.value.check.request_path, null)
response = try(each.value.check.response, null)
}
}

dynamic "log_config" {
for_each = try(each.value.logging, false) ? { 0 = 0 } : {}
content {
enable = true
}
}
}
11 changes: 8 additions & 3 deletions modules/net-glb/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ output "ssl_certificates" {

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

output "ip_address_self_link" {
description = "The URI of the reserved global IP address."
value = google_compute_global_forwarding_rule.forwarding_rule.ip_address
value = var.region == null ? google_compute_global_forwarding_rule.forwarding_rule.0.ip_address : google_compute_forwarding_rule.forwarding_rule.0.ip_address
}

output "target_proxy" {
Expand All @@ -61,5 +61,10 @@ output "target_proxy" {

output "global_forwarding_rule" {
description = "The global forwarding rule."
value = google_compute_global_forwarding_rule.forwarding_rule
value = var.region == null ? google_compute_global_forwarding_rule.forwarding_rule.0 : null
}

output "forwarding_rule" {
description = "The regional forwarding rule."
value = var.region == null ? google_compute_global_forwarding_rule.forwarding_rule.0 : google_compute_forwarding_rule.forwarding_rule.0
}
62 changes: 62 additions & 0 deletions modules/net-glb/regional-forwarding-rule.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

# tfdoc:file:description Global address and forwarding rule.

locals {
regional_ip_address = var.region != null ? (
var.reserve_ip_address
? google_compute_address.static_ip.0.id
: null
) : null

regional_port_range = coalesce(
var.forwarding_rule_config.port_range,
var.https ? "443" : "80"
)

regional_target = var.region != null ? (
var.https
? google_compute_region_target_https_proxy.https.0.id
: google_compute_region_target_http_proxy.http.0.id
) : null
}

resource "google_compute_address" "static_ip" {
count = var.region != null && var.reserve_ip_address ? 1 : 0
provider = google-beta
name = var.name
project = var.project_id
region = var.region
description = "Terraform managed."
address_type = "EXTERNAL"
}

resource "google_compute_forwarding_rule" "forwarding_rule" {
count = var.region != null ? 1 : 0
provider = google-beta
name = var.name
project = var.project_id
region = var.region
description = "Terraform managed."
ip_protocol = var.forwarding_rule_config.ip_protocol
load_balancing_scheme = var.forwarding_rule_config.load_balancing_scheme
port_range = local.regional_port_range
target = local.regional_target
ip_address = local.regional_ip_address
network_tier = var.forwarding_rule_config.network_tier
network = var.forwarding_rule_config.network
}
Loading

0 comments on commit 90e3995

Please sign in to comment.