Skip to content

Commit 1578cca

Browse files
authored
Setup NAT for API cluster (#1272)
1 parent c23dd0e commit 1578cca

File tree

8 files changed

+77
-1
lines changed

8 files changed

+77
-1
lines changed

iac/provider-gcp/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ tf_vars := TF_VAR_environment=$(TERRAFORM_ENVIRONMENT) \
2020
$(call tfvar, CLIENT_CLUSTER_CACHE_DISK_SIZE_GB) \
2121
$(call tfvar, API_MACHINE_TYPE) \
2222
$(call tfvar, API_CLUSTER_SIZE) \
23+
$(call tfvar, API_USE_NAT) \
24+
$(call tfvar, API_NAT_IPS) \
2325
$(call tfvar, BUILD_MACHINE_TYPE) \
2426
$(call tfvar, BUILD_CLUSTER_SIZE) \
2527
$(call tfvar, BUILD_CLUSTER_ROOT_DISK_SIZE_GB) \

iac/provider-gcp/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ module "cluster" {
105105
loki_node_pool = var.loki_node_pool
106106
orchestrator_node_pool = var.orchestrator_node_pool
107107

108+
api_use_nat = var.api_use_nat
109+
api_nat_ips = var.api_nat_ips
110+
108111
logs_health_proxy_port = var.logs_health_proxy_port
109112
logs_proxy_port = var.logs_proxy_port
110113

iac/provider-gcp/nomad-cluster/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ module "network" {
9191
cloudflare_api_token_secret_name = var.cloudflare_api_token_secret_name
9292

9393
gcp_project_id = var.gcp_project_id
94+
gcp_region = var.gcp_region
95+
96+
api_use_nat = var.api_use_nat
97+
api_nat_ips = var.api_nat_ips
9498

9599
api_port = var.api_port
96100
docker_reverse_proxy_port = var.docker_reverse_proxy_port

iac/provider-gcp/nomad-cluster/network/main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,3 +792,37 @@ resource "google_compute_security_policy" "disable-bots-log-collector" {
792792
}
793793
}
794794
}
795+
796+
# Cloud Router for NAT
797+
resource "google_compute_router" "nat_router" {
798+
count = var.api_use_nat ? 1 : 0
799+
name = "${var.prefix}nat-router"
800+
network = var.network_name
801+
region = var.gcp_region
802+
}
803+
804+
# Static IP addresses for NAT (only created if explicit IPs not provided)
805+
resource "google_compute_address" "nat_ips" {
806+
count = var.api_use_nat && length(var.api_nat_ips) == 0 ? 2 : 0
807+
name = "${var.prefix}nat-ip-${count.index + 1}"
808+
region = var.gcp_region
809+
}
810+
811+
# Cloud NAT for API nodes
812+
resource "google_compute_router_nat" "api_nat" {
813+
count = var.api_use_nat ? 1 : 0
814+
name = "${var.prefix}api-nat"
815+
router = google_compute_router.nat_router[0].name
816+
nat_ip_allocate_option = "MANUAL_ONLY"
817+
nat_ips = length(var.api_nat_ips) > 0 ? var.api_nat_ips : google_compute_address.nat_ips[*].self_link
818+
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
819+
820+
log_config {
821+
enable = true
822+
filter = "ERRORS_ONLY"
823+
}
824+
825+
lifecycle {
826+
create_before_destroy = true
827+
}
828+
}

iac/provider-gcp/nomad-cluster/network/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ variable "gcp_project_id" {
2727
type = string
2828
}
2929

30+
variable "gcp_region" {
31+
type = string
32+
}
33+
34+
variable "api_use_nat" {
35+
type = bool
36+
}
37+
38+
variable "api_nat_ips" {
39+
type = list(string)
40+
}
3041

3142
variable "cloudflare_api_token_secret_name" {
3243
type = string

iac/provider-gcp/nomad-cluster/nodepool-api.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ resource "google_compute_instance_template" "api" {
146146
network = var.network_name
147147

148148
dynamic "access_config" {
149-
for_each = ["public_ip"]
149+
for_each = var.api_use_nat ? [] : ["public_ip"]
150150
content {}
151151
}
152152
}

iac/provider-gcp/nomad-cluster/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,13 @@ variable "orchestrator_base_hugepages_percentage" {
325325
description = "The percentage of memory to use for preallocated hugepages."
326326
type = number
327327
}
328+
329+
variable "api_use_nat" {
330+
description = "Whether API nodes should use NAT with dedicated external IPs."
331+
type = bool
332+
}
333+
334+
variable "api_nat_ips" {
335+
type = list(string)
336+
description = "List of names for static IP addresses to use for NAT. If empty and api_use_nat is true, IPs will be created automatically."
337+
}

iac/provider-gcp/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ variable "api_node_pool" {
4747
default = "api"
4848
}
4949

50+
variable "api_use_nat" {
51+
type = bool
52+
description = "Whether API nodes should use NAT with dedicated external IPs."
53+
default = false
54+
}
55+
56+
variable "api_nat_ips" {
57+
type = list(string)
58+
description = "List of names for static IP addresses to use for NAT. If empty and api_use_nat is true, IPs will be created automatically."
59+
default = []
60+
}
61+
5062
variable "api_resources_cpu_count" {
5163
type = number
5264
default = 2

0 commit comments

Comments
 (0)