-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxy.tf
126 lines (116 loc) · 3.45 KB
/
haproxy.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
resource "helm_release" "haproxy" {
name = "haproxy"
repository = "https://charts.bitnami.com/bitnami"
chart = "haproxy"
version = var.haproxy_chart_version
namespace = var.relay_name
values = local.values
depends_on = [kubernetes_namespace_v1.relay, kubernetes_config_map_v1.haproxy]
}
resource "kubernetes_config_map_v1" "haproxy" {
metadata {
name = "haproxy"
namespace = kubernetes_namespace_v1.relay.id
annotations = {
"meta.helm.sh/release-name" = "haproxy"
"meta.helm.sh/release-namespace" = var.relay_name
}
labels = {
"app.kubernetes.io/component" = "haproxy"
"app.kubernetes.io/instance" = "haproxy"
"app.kubernetes.io/managed-by" = "terraform"
"app.kubernetes.io/name" = "haproxy"
}
}
data = {
"haproxy.cfg" = <<EOF
global
log stdout format rfc3164 local0
maxconn 1024
defaults
log global
timeout client 60s
timeout connect 60s
timeout server 60s
frontend fe_main
bind :8080
default_backend relays
backend relays
mode http
balance hdr(Charon-Cluster)
${local.servers_config}
EOF
}
}
locals {
servers_config = trim(join("", formatlist("%s \n", null_resource.backend_servers[*].triggers.server)), ",")
cert_manager_annotations = var.auto_create_tls ? [
"cert-manager.io/cluster-issuer: \"letsencrypt\"",
"cert-manager.io/issue-temporary-certificate: \"true\"",
"acme.cert-manager.io/http01-edit-in-place: \"true\""
] : []
cert_manager_annotations_yaml = join("\n", local.cert_manager_annotations)
tls_yaml = join("", [
for domain in concat(var.extra_domains) : <<EOT
- hosts:
- ${domain}
secretName: ${replace(domain, ".", "-")}-tls
EOT
])
extra_domains_yaml = length(var.extra_domains) > 0 ? join("", [
for host in var.extra_domains : <<EOT
- name: "${host}"
path: /
EOT
]) : ""
dns_annotations_yaml = var.auto_create_dns ? "" : "external-dns.alpha.kubernetes.io/hostname: \"\""
node_selector_yaml = (var.node_selector_enabled && var.node_selector != "") ? join("\n", [
"nodeSelector:",
" \"node_pool\": \"${var.node_selector}\"",
"tolerations:",
" - key: \"${var.node_selector}\"",
" operator: \"Equal\"",
" value: \"true\"",
" effect: \"NoSchedule\""
]) : ""
values = [
<<EOF
---
service:
ports:
- name: http
protocol: TCP
port: 80
targetPort: http
type: ClusterIP
externalTrafficPolicy: Local
replicaCount: ${var.haproxy_replicas_count}
containerPorts:
- name: http
containerPort: 8080
- name: bootnode-http
containerPort: 3640
existingConfigmap: haproxy
ingress:
enabled: true
ingressClassName: nginx
hostname: ${var.relay_name}.${var.primary_base_domain}
annotations:
${indent(4, local.cert_manager_annotations_yaml)}
${indent(4, local.dns_annotations_yaml)}
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
${length(var.extra_domains) > 0 ? " extraHosts:" : ""}
${local.extra_domains_yaml}
tls: true
${length(var.extra_domains) > 0 ? " extraTls:" : ""}
${local.tls_yaml}
${local.node_selector_yaml}
EOF
]
}
resource "null_resource" "backend_servers" {
count = var.cluster_size
triggers = {
server = lower(var.cloud_provider) == "gcp" ? " server ${var.relay_name}-${count.index} ${local.gcp_ips[count.index]}:3640 check inter 10s fall 12 rise 2" : " server ${var.relay_name}-${count.index} ${local.aws_lbs[count.index]}:3640 check inter 10s fall 12 rise 2"
}
}