-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
278 lines (259 loc) · 8.56 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
locals {
aws_lbs = [for svc in kubernetes_service_v1.relay-tcp : svc.status[0].load_balancer[0].ingress[0].hostname]
gcp_ips = var.external_ips != null ? var.external_ips : [for svc in kubernetes_service_v1.relay-tcp : svc.status[0].load_balancer[0].ingress[0].ip]
commonLabels = {
"app.kubernetes.io/app" = var.relay_name
}
}
resource "kubernetes_namespace_v1" "relay" {
metadata {
name = var.relay_name
}
}
resource "kubernetes_config_map_v1" "relay" {
count = var.cluster_size
metadata {
name = "${var.relay_name}-${count.index}"
namespace = kubernetes_namespace_v1.relay.id
}
data = {
CHARON_HTTP_ADDRESS = "0.0.0.0:3640"
CHARON_P2P_TCP_ADDRESS = "0.0.0.0:3610"
CHARON_MONITORING_ADDRESS = "0.0.0.0:3620"
CHARON_LOG_FORMAT = "json"
CHARON_LOG_LEVEL = "debug"
CHARON_P2P_RELAY_LOGLEVEL = "debug"
CHARON_LOKI_ADDRESSES = var.loki_endpoint
CHARON_LOKI_SERVICE = var.relay_name
}
}
resource "kubernetes_pod_disruption_budget_v1" "relay" {
count = var.cluster_size
metadata {
name = "${var.relay_name}-${count.index}"
namespace = kubernetes_namespace_v1.relay.id
}
spec {
max_unavailable = var.max_unavailable
selector {
match_labels = {
app = "${var.relay_name}-${count.index}"
}
}
}
}
resource "kubernetes_stateful_set_v1" "relay" {
count = var.cluster_size
metadata {
name = "${var.relay_name}-${count.index}"
namespace = kubernetes_namespace_v1.relay.id
labels = {
app = "${var.relay_name}-${count.index}"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "${var.relay_name}-${count.index}"
}
}
template {
metadata {
labels = {
app = "${var.relay_name}-${count.index}"
}
}
spec {
container {
name = "${var.relay_name}-${count.index}"
image = "ghcr.io/obolnetwork/charon:${var.relay_version}"
command = lower(var.cloud_provider) == "gcp" ? ["sh", "-ace", "/usr/local/bin/charon relay --p2p-external-ip=${local.gcp_ips[count.index]}"] : ["sh", "-ace", "/usr/local/bin/charon relay --p2p-external-hostname=${local.aws_lbs[count.index]}"]
working_dir = "/charon"
env_from {
config_map_ref {
name = "${var.relay_name}-${count.index}"
}
}
resources {
limits = {
cpu = var.cpu_limits
memory = var.memory_limits
}
requests = {
cpu = var.cpu_requests
memory = var.memory_requests
}
}
volume_mount {
name = "data"
mount_path = "/charon"
}
liveness_probe {
http_get {
path = "/livez"
port = "3640"
}
initial_delay_seconds = 5
period_seconds = 1
}
readiness_probe {
http_get {
path = "/readyz"
port = "3640"
}
initial_delay_seconds = 3
period_seconds = 1
}
image_pull_policy = "Always"
}
affinity {
dynamic "pod_affinity" {
for_each = try(var.node_affinity_config, [])
content {
dynamic "preferred_during_scheduling_ignored_during_execution" {
for_each = pod_affinity.value.type == "preferred_during_scheduling_ignored_during_execution" ? [1] : []
content {
weight = pod_affinity.value.weight
pod_affinity_term {
topology_key = pod_affinity.value.topology_key
dynamic "label_selector" {
for_each = pod_affinity.value.label_selectors
content {
match_labels = try(label_selector.value.match_expressions, null) == null ? local.commonLabels : null
dynamic "match_expressions" {
for_each = label_selector.value.match_expressions == null ? [] : label_selector.value.match_expressions
content {
key = match_expressions.value.key
operator = match_expressions.value.operator
values = match_expressions.value.values
}
}
}
}
}
}
}
dynamic "required_during_scheduling_ignored_during_execution" {
for_each = pod_affinity.value.type == "required_during_scheduling_ignored_during_execution" ? [1] : []
content {
topology_key = pod_affinity.value.topology_key
dynamic "label_selector" {
for_each = pod_affinity.value.label_selectors
content {
match_labels = try(label_selector.value.match_expressions, null) == null ? local.commonLabels : null
dynamic "match_expressions" {
for_each = label_selector.value.match_expressions == null ? [] : label_selector.value.match_expressions
content {
key = match_expressions.value.key
operator = match_expressions.value.operator
values = match_expressions.value.values
}
}
}
}
}
}
}
}
}
security_context {
run_as_user = 0
}
node_selector = (var.node_selector_enabled && var.node_selector != "") ? {
node_pool = var.node_selector
} : null
dynamic "toleration" {
for_each = (var.node_selector_enabled && var.node_selector != "") ? [1] : []
content {
effect = "NoSchedule"
key = var.node_selector
operator = "Equal"
value = "true"
}
}
automount_service_account_token = false
enable_service_links = false
}
}
volume_claim_template {
metadata {
name = "data"
namespace = ""
}
spec {
access_modes = ["ReadWriteOnce"]
storage_class_name = var.storageclass
resources {
requests = {
storage = "1Gi"
}
}
}
}
service_name = "${var.relay_name}-${count.index}"
}
depends_on = [kubernetes_namespace_v1.relay, resource.helm_release.haproxy, kubernetes_config_map_v1.relay]
}
resource "kubernetes_service_v1" "relay-tcp" {
count = var.cluster_size
metadata {
name = "${var.relay_name}-${count.index}-tcp"
namespace = kubernetes_namespace_v1.relay.id
annotations = lower(var.cloud_provider) == "gcp" ? {
"cloud.google.com/neg" = "{\"ingress\":true}"
} : null
}
wait_for_load_balancer = var.wait_for_load_balancer
spec {
port {
name = "p2p-tcp"
protocol = "TCP"
port = 3610
target_port = "3610"
}
port {
name = "monitoring"
protocol = "TCP"
port = 3620
target_port = "3620"
}
port {
name = "bootnode-http"
protocol = "TCP"
port = 3640
target_port = "3640"
}
selector = {
app = "${var.relay_name}-${count.index}"
}
type = "LoadBalancer"
load_balancer_ip = (lower(var.cloud_provider) == "gcp" && var.external_ips != null) ? var.external_ips[count.index] : null
external_traffic_policy = "Local"
}
}
resource "kubernetes_service_v1" "relay-udp" {
count = var.udp_enabled ? var.cluster_size : 0
metadata {
name = "${var.relay_name}-${count.index}-udp"
namespace = kubernetes_namespace_v1.relay.id
annotations = lower(var.cloud_provider) == "gcp" ? {
"cloud.google.com/neg" = "{\"ingress\":true}"
} : null
}
wait_for_load_balancer = false
spec {
port {
name = "p2p-udp"
protocol = "UDP"
port = 3630
target_port = "3630"
}
selector = {
app = "${var.relay_name}-${count.index}"
}
type = "LoadBalancer"
load_balancer_ip = (lower(var.cloud_provider) == "gcp" && var.external_ips != null) ? var.external_ips[count.index] : null
external_traffic_policy = "Local"
}
}