-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgateway.tf
More file actions
178 lines (166 loc) · 5 KB
/
gateway.tf
File metadata and controls
178 lines (166 loc) · 5 KB
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
# Copyright 2024, identinet GmbH. All rights reserved.
# SPDX-License-Identifier: MIT
module "gateway_network_interface" {
source = "./network_interface"
image = var.default_image
any = {}
}
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
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 ${module.gateway_network_interface.network_interface} 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/00-gateway-forwarding.network"
content = templatefile("${path.module}/templates/gateway-forwarding.network", { network_interface = module.gateway_network_interface.network_interface })
},
{
path = "/home/kubeapi/.ssh/authorized_keys"
content = templatefile("${path.module}/templates/ssh_authorized_keys", { ssh_keys = var.ssh_keys_kubeapi })
permissions = "0444"
},
{
# 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(merge(var.additional_cloud_init,
{
users = concat(var.additional_cloud_init.users,
[
{
# kubeapi user that allows users to locally forward and access the kubernetes API port and nothing else
name = "kubeapi"
shell = "/usr/sbin/nologin"
}
])
},
))
)
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"
]
}
}