Skip to content

Commit 89de347

Browse files
committed
Expand LB creation options
Provides a variable which swaps around the load balancer scheme used between internal and external, determining if a private or public IP is used. Provides additional variable which overrides architecture definitions and will disable load balancer creation.
1 parent 1ae8672 commit 89de347

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

main.tf

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ locals {
8989
subnetwork = coalesce(module.networking.subnetwork_link, try(data.google_compute_subnetwork.existing[0].self_link, null))
9090
create_network = var.subnetwork == null ? true : false
9191
fetch_existing = var.subnetwork == null ? 0 : 1
92-
has_lb = data.hiera5_bool.has_compilers.value ? true : false
92+
has_lb = var.disable_lb ? false : data.hiera5_bool.has_compilers.value ? true : false
9393
labels = merge(var.labels, { "stack" = var.stack_name })
9494
}
9595

@@ -112,14 +112,15 @@ module "networking" {
112112

113113
# Contain all the loadbalancer configuration in a module for readability
114114
module "loadbalancer" {
115-
source = "./modules/loadbalancer"
116-
id = local.id
117-
ports = ["8140", "8142"]
118-
network = local.network
119-
subnetwork = local.subnetwork
120-
region = var.region
121-
instances = module.instances.compilers
122-
has_lb = local.has_lb
115+
source = "./modules/loadbalancer"
116+
id = local.id
117+
ports = ["8140", "8142"]
118+
network = local.network
119+
subnetwork = local.subnetwork
120+
region = var.region
121+
instances = module.instances.compilers
122+
has_lb = local.has_lb
123+
lb_ip_mode = var.lb_ip_mode
123124
}
124125

125126
# Contain all the instances configuration in a module for readability

modules/loadbalancer/main.tf

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
locals {
33
instance_zones = toset(nonsensitive(var.instances[*].zone))
44
lb_count = var.has_lb ? 1 : 0
5+
lb_scheme = var.lb_ip_mode == "public" ? "EXTERNAL" : "INTERNAL"
6+
lb_network = var.lb_ip_mode == "public" ? null : var.network
7+
lb_subnetwork = var.lb_ip_mode == "public" ? null : var.subnetwork
58
}
69

710
# Create an instance group per zone to attach that zone's compilers to
@@ -14,7 +17,7 @@ resource "google_compute_instance_group" "backend" {
1417

1518
# Define a health check that'll indicate the health of a compiler node, very
1619
# irritating that this is a 1:1 mapping of front end IP to port
17-
resource "google_compute_health_check" "pe_compiler" {
20+
resource "google_compute_region_health_check" "pe_compiler" {
1821
count = local.lb_count
1922
name = "pe-compiler-${var.id}"
2023

@@ -23,10 +26,11 @@ resource "google_compute_health_check" "pe_compiler" {
2326

2427
# The backend service that bundles together all the zonal instance groups
2528
resource "google_compute_region_backend_service" "pe_compiler_lb" {
26-
count = local.lb_count
27-
name = "pe-compiler-lb-${var.id}"
28-
health_checks = [google_compute_health_check.pe_compiler[0].self_link]
29-
region = var.region
29+
count = local.lb_count
30+
name = "pe-compiler-lb-${var.id}"
31+
load_balancing_scheme = local.lb_scheme
32+
health_checks = [google_compute_region_health_check.pe_compiler[0].self_link]
33+
region = var.region
3034

3135
dynamic "backend" {
3236
for_each = local.instance_zones
@@ -41,9 +45,9 @@ resource "google_compute_forwarding_rule" "pe_compiler_lb" {
4145
count = local.lb_count
4246
name = "pe-compiler-lb-${var.id}"
4347
service_label = "puppet"
44-
load_balancing_scheme = "INTERNAL"
48+
load_balancing_scheme = local.lb_scheme
4549
ports = var.ports
46-
network = var.network
47-
subnetwork = var.subnetwork
4850
backend_service = google_compute_region_backend_service.pe_compiler_lb[0].self_link
51+
network = local.lb_network
52+
subnetwork = local.lb_subnetwork
4953
}

modules/loadbalancer/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ variable network {
3030
variable subnetwork {
3131
description = "Regional subnetwork assigned to VPC network provisioned by the networking submodule"
3232
type = string
33+
}
34+
variable "lb_ip_mode" {
35+
description = "Designate if a public or private IP address is assigned to load balancer"
36+
type = string
3337
}

variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,18 @@ variable "subnetwork_project" {
8686
type = string
8787
default = null
8888
}
89+
variable "lb_ip_mode" {
90+
description = "Designate if a public or private IP address is assigned to load balancer"
91+
type = string
92+
default = "public"
93+
94+
validation {
95+
condition = contains(["public", "private"], var.lb_ip_mode)
96+
error_message = "The provisioned load balancer can only have a public or private IP address assigned."
97+
}
98+
}
99+
variable "disable_lb" {
100+
description = "Disable load balancer creation for all architectures if you desire manually provisioning your own"
101+
type = bool
102+
default = false
103+
}

0 commit comments

Comments
 (0)