-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
427 lines (380 loc) · 14.3 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
locals {
# PSC client side
services = toset(flatten(tolist([for name in keys(var.ports) : [for i in range(var.brokers) : "${name}-${i}"]])))
kafka_endpoints = [
for ep in google_compute_forwarding_rule.endpoint : ep.ip_address if length(regexall("kafka", ep.name)) > 0
]
byoc = split(".", var.domain)[0]
# PSC server side
incremented_ports = merge(
[
for i in range(var.brokers) : {
for name, port in var.ports :
"${name}-${i}" => port + i * var.port-increment
}
]...)
non_incremented_ports = merge(
[
for i in range(var.brokers) : {
for name, port in var.ports :
"${name}-${i}" => port
}
]...)
psc-subnets = {for i, name in keys(local.incremented_ports) : name => "${var.psc-subnet-cidr-prefix}${i * 8}/29"}
}
# VPC for the client side endpoints and VMs
resource "google_compute_network" "client-network" {
name = "${var.prefix}-client-network"
auto_create_subnetworks = false
}
# Subnet for the client side endpoints
resource "google_compute_subnetwork" "client-subnet" {
name = "${var.prefix}-client-subnet"
ip_cidr_range = var.client_cidr
region = var.region
network = google_compute_network.client-network.id
}
# Static IP address for the PSC endpoints
resource "google_compute_address" "endpoint" {
name = "${var.prefix}-endpoint-address-${each.key}"
for_each = local.services
region = var.region
subnetwork = google_compute_subnetwork.client-subnet.id
address_type = "INTERNAL"
}
# Client VM for testing
resource "google_compute_instance" "client" {
name = "${var.prefix}-client-vm"
machine_type = "n1-standard-1"
zone = "${var.region}-${var.availability_zones[0]}"
metadata = {
ssh-keys = <<KEYS
${var.ssh_user}:${file(abspath(var.public_key_path))}
KEYS
}
boot_disk {
initialize_params {
image = var.image
}
}
network_interface {
network = google_compute_network.client-network.id
subnetwork = google_compute_subnetwork.client-subnet.id
access_config {}
}
metadata_startup_script = file("${path.module}/client.tftpl")
}
# Private DNS zone to override the broker location to be the PSC endpoints
resource "google_dns_managed_zone" "local-zone" {
name = "${local.byoc}-local-zone"
dns_name = "${var.domain}."
visibility = "private"
private_visibility_config {
networks {
network_url = google_compute_network.client-network.id
}
}
}
# DNS entry for the seed broker (round robin over the three endpoints)
resource "google_dns_record_set" "seed" {
name = "${split(":", var.seed-broker)[0]}."
managed_zone = google_dns_managed_zone.local-zone.name
type = "A"
ttl = 300
rrdatas = local.kafka_endpoints
}
# DNS entries for the endpoints (kafka-0, registry-1, proxy-2, etc)
resource "google_dns_record_set" "endpoint" {
name = "${each.key}.${var.domain}."
for_each = local.services
managed_zone = google_dns_managed_zone.local-zone.name
type = "A"
ttl = 300
rrdatas = [google_compute_forwarding_rule.endpoint[each.key].ip_address]
}
# Access Connector to allow the cloudfunction to access private VPCs
resource "google_vpc_access_connector" "default" {
name = "${var.prefix}-vpc-access-connector"
network = google_compute_network.client-network.id
ip_cidr_range = "192.168.50.0/28"
region = var.region
}
# Zip file to hold the cloudfunction Source Code
data "archive_file" "locator" {
type = "zip"
output_path = "source.zip"
source_dir = "locator"
}
# Storage bucket to hold the cloudfunction zip file
resource "google_storage_bucket" "bucket" {
name = "${var.prefix}-bucket-${local.byoc}"
location = var.location
}
# Directive to upload the cloudfunction zip file to the bucket
resource "google_storage_bucket_object" "archive" {
name = "source.zip"
bucket = google_storage_bucket.bucket.name
source = "./source.zip"
}
# Cloudfunction for DNS updates from the HAProxy scripts
resource "google_cloudfunctions_function" "function" {
name = "update-dns-${local.byoc}"
runtime = "go120"
vpc_connector = google_vpc_access_connector.default.id
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.archive.name
trigger_http = true
entry_point = "update" # The name of the .go file containing the function
environment_variables = {
PROJECT = var.project_name
ZONE = "${local.byoc}-local-zone"
}
lifecycle {
replace_triggered_by = [google_storage_bucket_object.archive]
}
}
# IAM entry to allow any user to invoke the cloudfunction (credentials passed as POST arguments)
resource "google_cloudfunctions_function_iam_member" "invoker" {
project = google_cloudfunctions_function.function.project
region = google_cloudfunctions_function.function.region
cloud_function = google_cloudfunctions_function.function.name
role = "roles/cloudfunctions.invoker"
member = "allUsers"
}
####################################################################
# VPC to host the HAProxy infrastructure needed for PSC
resource "google_compute_network" "proxy-network" {
name = "${var.prefix}-proxy-network"
auto_create_subnetworks = false
}
# Proxy subnet indirectly required by forwarding rules
resource "google_compute_subnetwork" "proxy-network-subnet" {
name = "${var.prefix}-proxy-subnet"
ip_cidr_range = var.proxy-cidr
region = var.region
network = google_compute_network.proxy-network.id
purpose = "REGIONAL_MANAGED_PROXY"
role = "ACTIVE"
}
# Private subnet to host HAProxy VMs and related infra
resource "google_compute_subnetwork" "haproxy-network-subnet" {
name = "${var.prefix}-vm-subnet"
ip_cidr_range = var.vm-cidr
region = var.region
network = google_compute_network.proxy-network.id
}
# Debug: Firewall rule to allow SSH access
#resource "google_compute_firewall" "ssh-to-proxy-network" {
# name = "${var.prefix}-ssh-firewall-rule"
# network = google_compute_network.proxy-network.name
# allow {
# protocol = "all"
# }
# source_ranges = ["your.ip.here"]
# direction = "INGRESS"
# priority = 1
# log_config {
# metadata = "INCLUDE_ALL_METADATA"
# }
#}
# Firewall rule to allow
resource "google_compute_firewall" "internal" {
name = "${var.prefix}-allow-internal"
network = google_compute_network.proxy-network.name
allow {
protocol = "all"
}
source_ranges = [var.vm-cidr, var.proxy-cidr]
direction = "INGRESS"
priority = 1
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
# Peering to allow our new VM VPC to access the Redpanda VPC created by BYOC
resource "google_compute_network_peering" "peering1" {
name = "${var.prefix}-peering-inbound"
network = google_compute_network.proxy-network.self_link
peer_network = var.rp-network
}
# Peering to allow our new VM VPC to access the Redpanda VPC created by BYOC
resource "google_compute_network_peering" "peering2" {
name = "${var.prefix}-peering-outbound"
network = var.rp-network
peer_network = google_compute_network.proxy-network.self_link
}
# Router (needed for HAProxy VMs to access the internet to download packages)
resource "google_compute_router" "router" {
name = "${var.prefix}-router"
project = var.project_name
network = google_compute_network.proxy-network.id
region = var.region
}
# NAT (needed for HAProxy VMs to access the internet to download packages)
resource "google_compute_router_nat" "nat" {
name = "${var.prefix}-router-nat"
router = google_compute_router.router.name
region = var.region
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
subnetwork {
name = google_compute_subnetwork.haproxy-network-subnet.self_link
source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
}
log_config {
enable = true
filter = "ERRORS_ONLY"
}
}
# HAProxy VMs
resource "google_compute_instance" "haproxy" {
name = "${var.prefix}-vm-${count.index}"
machine_type = "n1-standard-1"
count = var.haproxy-count
zone = "${var.region}-${var.availability_zones[count.index % length(var.availability_zones)]}"
metadata = {
ssh-keys = <<KEYS
${var.ssh_user}:${file(abspath(var.public_key_path))}
KEYS
}
boot_disk {
initialize_params {
image = var.image
}
}
network_interface {
subnetwork = google_compute_subnetwork.haproxy-network-subnet.name
#access_config {} # Commented out to prevent public NAT ip being created
}
metadata_startup_script = templatefile("${path.module}/haproxy.tftpl", {
seed-broker = var.seed-broker,
seed-user = var.seed-user,
seed-password = var.seed-password,
prefix = "kafka",
credentials = google_service_account_key.mykey.private_key
update-url = google_cloudfunctions_function.function.https_trigger_url
ports = join(" ", [for name, port in var.ports : port]),
port-increment = var.port-increment
})
}
# HAProxy VM Instance Group (needed for internal load balancer to target the VMs)
resource "google_compute_instance_group" "default" {
name = "${var.prefix}-instance-group-${var.region}-${var.availability_zones[count.index]}"
count = length(var.availability_zones)
zone = "${var.region}-${var.availability_zones[count.index]}"
instances = tolist([
for i in google_compute_instance.haproxy.* : i.self_link
if i.zone == "${var.region}-${var.availability_zones[count.index]}"
])
network = google_compute_network.proxy-network.id
dynamic "named_port" {
for_each = local.incremented_ports
content {
name = named_port.key
port = named_port.value
}
}
}
# Health check definition for our HAProxy targets
resource "google_compute_region_health_check" "kafka" {
name = "${var.prefix}-backend-health-check-kafka"
tcp_health_check {
port = "30092"
}
region = var.region
}
# Firewall rules to allow health checks against our internal load balancer
resource "google_compute_firewall" "health-check" {
name = "${var.prefix}-health-check-firewall-rule"
network = google_compute_network.proxy-network.name
allow {
protocol = "all"
}
source_ranges = ["130.211.0.0/22", "35.191.0.0/16", "35.235.240.0/20"] # These are the Google Health Check sources
direction = "INGRESS"
priority = 1
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
# Internal proxy load balancer (backend) for the PSC service attachment to point to
resource "google_compute_region_backend_service" "backend" {
name = "${var.prefix}-backend-service-${each.key}"
for_each = local.incremented_ports
protocol = "TCP"
load_balancing_scheme = "INTERNAL_MANAGED"
dynamic "backend" {
for_each = google_compute_instance_group.default
content {
group = backend.value.self_link
balancing_mode = "CONNECTION"
max_connections = 100
capacity_scaler = 1
}
}
port_name = each.key
health_checks = [google_compute_region_health_check.kafka.id]
}
# Internal proxy load balancer (TCP proxy) for the PSC service attachment to point to
resource "google_compute_region_target_tcp_proxy" "default" {
name = "${var.prefix}-tcp-proxy-${each.key}"
for_each = local.incremented_ports
backend_service = google_compute_region_backend_service.backend[each.key].id
# proxy_header = "PROXY_V1"
}
# Internal proxy load balancer (forwarding rule) for the PSC service attachment to point to
resource "google_compute_forwarding_rule" "default" {
name = "${var.prefix}-internal-forwarding-rule-${each.key}"
for_each = local.non_incremented_ports
load_balancing_scheme = "INTERNAL_MANAGED"
network = google_compute_network.proxy-network.id
subnetwork = google_compute_subnetwork.haproxy-network-subnet.name
target = google_compute_region_target_tcp_proxy.default[each.key].id
ip_protocol = "TCP"
port_range = each.value
}
# Subnets for PSC (one per service attachment as they can't be shared)
resource "google_compute_subnetwork" "service-attachment-network-subnet" {
name = "${var.prefix}-service-attachment-subnet-${each.key}"
for_each = local.psc-subnets
ip_cidr_range = each.value
region = var.region
network = google_compute_network.proxy-network.id
purpose = "PRIVATE_SERVICE_CONNECT"
}
# PSC service attachments
resource "google_compute_service_attachment" "default" {
name = "${var.prefix}-service-attachment-${each.key}"
for_each = local.incremented_ports
connection_preference = "ACCEPT_AUTOMATIC"
enable_proxy_protocol = false
nat_subnets = [google_compute_subnetwork.service-attachment-network-subnet[each.key].id]
region = var.region
target_service = google_compute_forwarding_rule.default[each.key].id
}
# Service account for DNS updates
resource "google_service_account" "service_account" {
account_id = "dns-update-account"
display_name = "DNS Update Account"
}
# Credentials for DNS update service account
resource "google_service_account_key" "mykey" {
service_account_id = google_service_account.service_account.id
}
resource "google_project_iam_member" "dns_admin_binding" {
project = var.project_name
role = "roles/dns.admin"
member = "serviceAccount:${google_service_account.service_account.email}"
}
####################################################################
# Client side PSC endpoints
resource "google_compute_forwarding_rule" "endpoint" {
name = "psc-endpoint-${each.key}"
for_each = local.services
region = var.region
target = google_compute_service_attachment.default[each.key].id
load_balancing_scheme = "" # need to override EXTERNAL default when target is a service attachment
network = google_compute_network.client-network.id
ip_address = google_compute_address.endpoint[each.key].id
}