-
Notifications
You must be signed in to change notification settings - Fork 8
/
gateway.tf
155 lines (145 loc) · 4.26 KB
/
gateway.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
# Copyright 2024, identinet GmbH. All rights reserved.
# SPDX-License-Identifier: MIT
resource "hcloud_server" "gateway" {
lifecycle {
prevent_destroy = false
ignore_changes = [
name,
image,
location,
network,
ssh_keys,
user_data,
]
}
depends_on = [hcloud_network_subnet.subnet]
name = "${var.cluster_name}-gateway"
delete_protection = var.delete_protection
rebuild_protection = var.delete_protection
location = var.default_location
image = var.default_image
server_type = var.gateway_server_type
ssh_keys = [for k in hcloud_ssh_key.pub_keys : k.name]
labels = var.gateway_labels
# Documentation https://cloudinit.readthedocs.io/en/latest/reference/modules.html#package-update-upgrade-install
user_data = format("%s\n%s\n%s", "#cloud-config", yamlencode({
package_update = true
package_upgrade = true
package_reboot_if_required = true
packages = concat(["netcat-openbsd"], local.base_packages) # netcat is required for acting as an ssh jump jost
# Find runcmd: find /var/lib/cloud/instances -name runcmd
runcmd = concat([
local.security_setup,
"ufw allow proto tcp from any to any port 6443",
<<-EOT
echo 'Unattended-Upgrade::Automatic-Reboot "true";' >> /etc/apt/apt.conf.d/50unattended-upgrades
# Enable packet forwarding
ufw route allow in on ens10 out on eth0
systemctl daemon-reload
EOT
,
local.haproxy_setup,
local.dist_upgrade,
], var.additional_runcmd)
write_files = [
{
path = "/usr/local/bin/haproxy-k8s.nu"
content = templatefile("${path.module}/templates/haproxy-k8s.nu", {
token = var.hcloud_token_read_only
host = "[::]"
port = "6443"
})
permissions = "0700"
},
{
path = "/etc/systemd/system/haproxy-k8s.service"
content = file("${path.module}/templates/haproxy-k8s.service")
},
{
path = "/etc/systemd/system/haproxy-k8s.timer"
content = file("${path.module}/templates/haproxy-k8s.timer")
},
{
path = "/etc/systemd/network/gateway-forwarding.network"
content = file("${path.module}/templates/gateway-forwarding.network")
},
{
# Enable postrouting with ufw
path = "/etc/ufw/before.rules"
content = templatefile("${path.module}/templates/gateway-ufw-before.rules", { subnet = var.subnet_cidr })
append = true
},
]
}),
yamlencode(var.additional_cloud_init)
)
firewall_ids = concat(
[hcloud_firewall.gateway_ssh.id],
var.gateway_firewall_icmp_open ? [hcloud_firewall.gateway_icmp.id] : [],
var.gateway_firewall_k8s_open ? [hcloud_firewall.gateway_k8s.id] : [],
var.gateway_firewall_ids)
public_net {
ipv4_enabled = true
ipv6_enabled = true
}
# Network needs to be present twice for some unknown reason :-/
network {
network_id = hcloud_network.private.id
ip = cidrhost(hcloud_network_subnet.subnet.ip_range, 1)
}
}
resource "hcloud_server_network" "gateway" {
server_id = hcloud_server.gateway.id
network_id = hcloud_network.private.id
ip = cidrhost(hcloud_network_subnet.subnet.ip_range, 1)
}
resource "hcloud_network_route" "default" {
network_id = hcloud_network.private.id
destination = "0.0.0.0/0"
gateway = hcloud_server_network.gateway.ip
}
resource "hcloud_firewall" "gateway_icmp" {
lifecycle {
prevent_destroy = false
}
name = "${var.cluster_name}-gateway-icmp"
rule {
direction = "in"
protocol = "icmp"
port = ""
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}
resource "hcloud_firewall" "gateway_ssh" {
lifecycle {
prevent_destroy = false
}
name = "${var.cluster_name}-gateway-ssh"
rule {
direction = "in"
protocol = "tcp"
port = "22"
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}
resource "hcloud_firewall" "gateway_k8s" {
lifecycle {
prevent_destroy = false
}
name = "${var.cluster_name}-gateway-k8s"
rule {
direction = "in"
protocol = "tcp"
port = "6443"
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}