Skip to content

Commit c6eeed2

Browse files
Create load balancers
1 parent 389d0c7 commit c6eeed2

File tree

8 files changed

+160
-5
lines changed

8 files changed

+160
-5
lines changed

examples/simple/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ output "connection_strings" {
127127
sensitive = true
128128
}
129129

130+
output "load_balancer_details" {
131+
description = "Details of the Materialize instance load balancers."
132+
value = module.materialize.load_balancer_details
133+
}
134+
130135
variable "operator_version" {
131136
description = "Version of the Materialize operator to install"
132137
type = string
@@ -151,6 +156,8 @@ variable "materialize_instances" {
151156
namespace = optional(string)
152157
database_name = string
153158
create_database = optional(bool, true)
159+
create_load_balancer = optional(bool, true)
160+
internal_load_balancer = optional(bool, true)
154161
environmentd_version = optional(string)
155162
cpu_request = optional(string, "1")
156163
memory_request = optional(string, "1Gi")

main.tf

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ module "operator" {
119119
}
120120
}
121121

122+
module "load_balancers" {
123+
source = "./modules/load_balancers"
124+
125+
for_each = { for idx, instance in local.instances : instance.name => instance if lookup(instance, "create_load_balancer", false) }
126+
127+
instance_name = each.value.name
128+
namespace = module.operator[0].materialize_instances[each.value.name].namespace
129+
resource_id = module.operator[0].materialize_instance_resource_ids[each.value.name]
130+
internal = each.value.internal_load_balancer
131+
132+
depends_on = [
133+
module.operator,
134+
module.gke,
135+
]
136+
}
137+
122138
locals {
123139
default_helm_values = {
124140
observability = {
@@ -176,11 +192,13 @@ locals {
176192
locals {
177193
instances = [
178194
for instance in var.materialize_instances : {
179-
name = instance.name
180-
namespace = instance.namespace
181-
database_name = instance.database_name
182-
create_database = instance.create_database
183-
environmentd_version = instance.environmentd_version
195+
name = instance.name
196+
namespace = instance.namespace
197+
database_name = instance.database_name
198+
create_database = instance.create_database
199+
create_load_balancer = instance.create_load_balancer
200+
internal_load_balancer = instance.internal_load_balancer
201+
environmentd_version = instance.environmentd_version
184202

185203
metadata_backend_url = format(
186204
"postgres://%s:%s@%s:5432/%s?sslmode=disable",

modules/load_balancers/main.tf

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
resource "kubernetes_service" "console_load_balancer" {
2+
metadata {
3+
name = "mz${var.resource_id}-console-lb"
4+
namespace = var.namespace
5+
annotations = {
6+
"networking.gke.io/load-balancer-type" = var.internal ? "Internal" : "External"
7+
}
8+
}
9+
10+
spec {
11+
type = "LoadBalancer"
12+
external_traffic_policy = "Local"
13+
selector = {
14+
"materialize.cloud/name" = "mz${var.resource_id}-console"
15+
}
16+
port {
17+
name = "http"
18+
port = 8080
19+
target_port = 8080
20+
protocol = "TCP"
21+
}
22+
}
23+
24+
lifecycle {
25+
ignore_changes = [
26+
metadata[0].annotations["cloud.google.com/neg"]
27+
]
28+
}
29+
wait_for_load_balancer = true
30+
}
31+
32+
resource "kubernetes_service" "balancerd_load_balancer" {
33+
metadata {
34+
name = "mz${var.resource_id}-balancerd-lb"
35+
namespace = var.namespace
36+
annotations = {
37+
"networking.gke.io/load-balancer-type" = var.internal ? "Internal" : "External"
38+
}
39+
}
40+
41+
spec {
42+
type = "LoadBalancer"
43+
external_traffic_policy = "Local"
44+
selector = {
45+
"materialize.cloud/name" = "mz${var.resource_id}-balancerd"
46+
}
47+
port {
48+
name = "sql"
49+
port = 6875
50+
target_port = 6875
51+
protocol = "TCP"
52+
}
53+
port {
54+
name = "https"
55+
port = 6876
56+
target_port = 6876
57+
protocol = "TCP"
58+
}
59+
}
60+
61+
lifecycle {
62+
ignore_changes = [
63+
metadata[0].annotations["cloud.google.com/neg"]
64+
]
65+
}
66+
wait_for_load_balancer = true
67+
}

modules/load_balancers/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "instance_name" {
2+
description = "The name of the Materialize instance."
3+
value = var.instance_name
4+
}
5+
6+
output "console_load_balancer_ip" {
7+
description = "IP address of load balancer pointing at the web console."
8+
value = kubernetes_service.console_load_balancer.status[0].load_balancer[0].ingress[0].ip
9+
}
10+
11+
output "balancerd_load_balancer_ip" {
12+
description = "IP address of load balancer pointing at balancerd."
13+
value = kubernetes_service.balancerd_load_balancer.status[0].load_balancer[0].ingress[0].ip
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "instance_name" {
2+
description = "The name of the Materialize instance."
3+
type = string
4+
}
5+
6+
variable "namespace" {
7+
description = "The kubernetes namespace to create the LoadBalancer Service in."
8+
type = string
9+
}
10+
11+
variable "resource_id" {
12+
description = "The resource_id in the Materialize status."
13+
type = string
14+
}
15+
16+
variable "internal" {
17+
description = "Whether the load balancer is internal to the VPC."
18+
type = bool
19+
}

modules/load_balancers/versions.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
google = {
6+
source = "hashicorp/google"
7+
version = ">= 6.0"
8+
}
9+
kubernetes = {
10+
source = "hashicorp/kubernetes"
11+
version = "~> 2.0"
12+
}
13+
helm = {
14+
source = "hashicorp/helm"
15+
version = "~> 2.0"
16+
}
17+
}
18+
}

outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ output "operator" {
8585
instances = module.operator[0].materialize_instances
8686
} : null
8787
}
88+
89+
output "load_balancer_details" {
90+
description = "Details of the Materialize instance load balancers."
91+
value = {
92+
for load_balancer in module.load_balancers : load_balancer.instance_name => {
93+
console_load_balancer_ip = load_balancer.console_load_balancer_ip
94+
balancerd_load_balancer_ip = load_balancer.balancerd_load_balancer_ip
95+
}
96+
}
97+
}

variables.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ variable "materialize_instances" {
108108
namespace = optional(string)
109109
database_name = string
110110
create_database = optional(bool, true)
111+
create_load_balancer = optional(bool, true)
112+
internal_load_balancer = optional(bool, true)
111113
environmentd_version = optional(string)
112114
cpu_request = optional(string, "1")
113115
memory_request = optional(string, "1Gi")

0 commit comments

Comments
 (0)