Skip to content

Commit 7d3b111

Browse files
sitoleCode42Cate
authored andcommitted
Implement ingress for easier services exposing (#1314)
1 parent 9fe1670 commit 7d3b111

File tree

14 files changed

+276
-3
lines changed

14 files changed

+276
-3
lines changed

iac/provider-gcp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ tf_vars := TF_VAR_environment=$(TERRAFORM_ENVIRONMENT) \
4242
$(call tfvar, ALLOW_SANDBOX_INTERNET) \
4343
$(call tfvar, API_RESOURCES_CPU_COUNT) \
4444
$(call tfvar, API_RESOURCES_MEMORY_MB) \
45+
$(call tfvar, INGRESS_COUNT) \
4546
$(call tfvar, CLIENT_PROXY_COUNT) \
4647
$(call tfvar, CLIENT_PROXY_RESOURCES_CPU_COUNT) \
4748
$(call tfvar, CLIENT_PROXY_RESOURCES_MEMORY_MB) \

iac/provider-gcp/init/main.tf

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,27 @@ resource "google_secret_manager_secret_version" "analytics_collector_api_token"
208208
depends_on = [time_sleep.secrets_api_wait_60_seconds]
209209
}
210210

211+
resource "google_secret_manager_secret" "routing_domains" {
212+
secret_id = "${var.prefix}routing-domains"
213+
214+
replication {
215+
auto {}
216+
}
217+
218+
depends_on = [time_sleep.secrets_api_wait_60_seconds]
219+
}
220+
221+
resource "google_secret_manager_secret_version" "routing_domains" {
222+
secret = google_secret_manager_secret.routing_domains.name
223+
secret_data = jsonencode([])
224+
225+
lifecycle {
226+
ignore_changes = [secret_data]
227+
}
228+
229+
depends_on = [time_sleep.secrets_api_wait_60_seconds]
230+
}
231+
211232
resource "google_artifact_registry_repository" "orchestration_repository" {
212233
format = "DOCKER"
213234
repository_id = "e2b-orchestration"
@@ -222,7 +243,6 @@ resource "time_sleep" "artifact_registry_api_wait_90_seconds" {
222243
create_duration = "90s"
223244
}
224245

225-
226246
resource "google_artifact_registry_repository_iam_member" "orchestration_repository_member" {
227247
repository = google_artifact_registry_repository.orchestration_repository.name
228248
role = "roles/artifactregistry.reader"

iac/provider-gcp/init/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ output "analytics_collector_api_token_secret_name" {
3030
value = google_secret_manager_secret.analytics_collector_api_token.name
3131
}
3232

33+
output "routing_domains_secret_name" {
34+
value = google_secret_manager_secret.routing_domains.name
35+
}
36+
3337
output "orchestration_repository_name" {
3438
value = google_artifact_registry_repository.orchestration_repository.name
3539
}

iac/provider-gcp/main.tf

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ provider "google" {
5454
zone = var.gcp_zone
5555
}
5656

57+
data "google_secret_manager_secret_version" "routing_domains" {
58+
secret = module.init.routing_domains_secret_name
59+
}
60+
61+
locals {
62+
// Taking additional domains from local env is there just for backward compatibility
63+
additional_domains_from_secret = nonsensitive(jsondecode(data.google_secret_manager_secret_version.routing_domains.secret_data))
64+
additional_domains_from_env = (var.additional_domains != "" ?
65+
[for item in split(",", var.additional_domains) : trimspace(item)] : [])
66+
67+
additional_domains = distinct(concat(local.additional_domains_from_env, local.additional_domains_from_secret))
68+
}
69+
5770
module "init" {
5871
source = "./init"
5972

@@ -108,16 +121,16 @@ module "cluster" {
108121
api_use_nat = var.api_use_nat
109122
api_nat_ips = var.api_nat_ips
110123

124+
ingress_port = var.ingress_port
111125
edge_api_port = var.edge_api_port
112126
edge_proxy_port = var.edge_proxy_port
113127
api_port = var.api_port
114128
docker_reverse_proxy_port = var.docker_reverse_proxy_port
115129
nomad_port = var.nomad_port
116130
google_service_account_email = module.init.service_account_email
117131
domain_name = var.domain_name
118-
additional_domains = (var.additional_domains != "" ?
119-
[for item in split(",", var.additional_domains) : trimspace(item)] : [])
120132

133+
additional_domains = local.additional_domains
121134
additional_api_services = (var.additional_api_services_json != "" ?
122135
jsondecode(var.additional_api_services_json) :
123136
[])
@@ -172,6 +185,10 @@ module "nomad" {
172185
clickhouse_job_constraint_prefix = var.clickhouse_job_constraint_prefix
173186
clickhouse_node_pool = var.clickhouse_node_pool
174187

188+
# Ingress
189+
ingress_port = var.ingress_port
190+
ingress_count = var.ingress_count
191+
175192
# API
176193
api_resources_cpu_count = var.api_resources_cpu_count
177194
api_resources_memory_mb = var.api_resources_memory_mb

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ module "network" {
9696
api_use_nat = var.api_use_nat
9797
api_nat_ips = var.api_nat_ips
9898

99+
ingress_port = var.ingress_port
99100
api_port = var.api_port
100101
docker_reverse_proxy_port = var.docker_reverse_proxy_port
101102
network_name = var.network_name
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
resource "google_compute_health_check" "ingress" {
2+
name = "${var.prefix}ingress"
3+
4+
timeout_sec = 3
5+
check_interval_sec = 5
6+
healthy_threshold = 2
7+
unhealthy_threshold = 2
8+
9+
http_health_check {
10+
port = var.ingress_port.port
11+
request_path = var.ingress_port.health_path
12+
}
13+
}
14+
15+
resource "google_compute_backend_service" "ingress" {
16+
name = "${var.prefix}ingress"
17+
18+
protocol = "HTTP"
19+
port_name = var.ingress_port.name
20+
21+
session_affinity = null
22+
health_checks = [google_compute_health_check.ingress.id]
23+
24+
timeout_sec = 65
25+
26+
load_balancing_scheme = "EXTERNAL_MANAGED"
27+
locality_lb_policy = "ROUND_ROBIN"
28+
29+
backend {
30+
group = var.api_instance_group
31+
}
32+
}
33+
34+
resource "google_compute_url_map" "ingress" {
35+
name = "${var.prefix}ingress"
36+
default_service = google_compute_backend_service.ingress.self_link
37+
}
38+
39+
resource "google_compute_global_forwarding_rule" "ingress" {
40+
name = "${var.prefix}ingress-forward-http"
41+
ip_protocol = "TCP"
42+
port_range = "443"
43+
load_balancing_scheme = "EXTERNAL_MANAGED"
44+
ip_address = google_compute_global_address.ingress_ipv4.address
45+
target = google_compute_target_https_proxy.ingress.self_link
46+
}
47+
48+
resource "google_compute_global_address" "ingress_ipv4" {
49+
name = "${var.prefix}ingress-ipv4"
50+
ip_version = "IPV4"
51+
}
52+
53+
resource "google_compute_target_https_proxy" "ingress" {
54+
name = "${var.prefix}ingress-https"
55+
url_map = google_compute_url_map.ingress.self_link
56+
57+
certificate_map = "//certificatemanager.googleapis.com/${google_certificate_manager_certificate_map.certificate_map.id}"
58+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ resource "google_compute_firewall" "default-hc" {
455455
}
456456
}
457457

458+
allow {
459+
protocol = "tcp"
460+
ports = [var.ingress_port.port]
461+
}
462+
458463
dynamic "allow" {
459464
for_each = toset(var.additional_ports)
460465

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ variable "api_port" {
5151
})
5252
}
5353

54+
variable "ingress_port" {
55+
type = object({
56+
name = string
57+
port = number
58+
health_path = string
59+
})
60+
}
61+
5462
variable "docker_reverse_proxy_port" {
5563
type = object({
5664
name = string

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ resource "google_compute_instance_group_manager" "api_pool" {
7171
port = var.docker_reverse_proxy_port.port
7272
}
7373

74+
named_port {
75+
name = var.ingress_port.name
76+
port = var.ingress_port.port
77+
}
78+
7479
dynamic "named_port" {
7580
for_each = local.api_additional_ports
7681
content {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ variable "api_port" {
100100
})
101101
}
102102

103+
variable "ingress_port" {
104+
type = object({
105+
name = string
106+
port = number
107+
health_path = string
108+
})
109+
}
110+
103111
variable "docker_reverse_proxy_port" {
104112
type = object({
105113
name = string

0 commit comments

Comments
 (0)