-
Notifications
You must be signed in to change notification settings - Fork 1
/
main-infra.tf
101 lines (80 loc) · 2.26 KB
/
main-infra.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
# Setup the infrastructure
provider "hcloud" {
token = var.hcloud_token
}
locals {
labels = merge(var.hcloud_labels, {
env = var.name
})
}
# SSH Key
resource "tls_private_key" "ssh" {
algorithm = "ED25519"
}
resource "local_sensitive_file" "ssh_private" {
content = tls_private_key.ssh.private_key_openssh
filename = abspath("${path.root}/files/id_ed25519")
}
resource "local_sensitive_file" "ssh_public" {
content = tls_private_key.ssh.public_key_openssh
filename = abspath("${path.root}/files/id_ed25519.pub")
}
resource "hcloud_ssh_key" "default" {
name = var.name
public_key = tls_private_key.ssh.public_key_openssh
labels = local.labels
}
# Network
resource "hcloud_network" "cluster" {
name = var.name
ip_range = "10.0.0.0/8"
labels = local.labels
}
resource "hcloud_network_subnet" "cluster" {
network_id = hcloud_network.cluster.id
network_zone = "eu-central"
type = "cloud"
ip_range = "10.0.0.0/24"
}
# Control Plane Node
resource "hcloud_server" "control" {
name = "${var.name}-control"
server_type = var.hcloud_server_type
location = var.hcloud_location
image = var.hcloud_image
ssh_keys = [hcloud_ssh_key.default.id]
labels = local.labels
connection {
host = self.ipv4_address
private_key = tls_private_key.ssh.private_key_openssh
}
provisioner "remote-exec" {
inline = ["cloud-init status --wait || test $? -eq 2"]
}
}
resource "hcloud_server_network" "control" {
server_id = hcloud_server.control.id
subnet_id = hcloud_network_subnet.cluster.id
}
# Worker / Agent Nodes
resource "hcloud_server" "worker" {
count = var.worker_count
name = "${var.name}-worker-${count.index}"
server_type = var.hcloud_server_type
location = var.hcloud_location
image = var.hcloud_image
ssh_keys = [hcloud_ssh_key.default.id]
labels = local.labels
connection {
host = self.ipv4_address
private_key = tls_private_key.ssh.private_key_openssh
}
provisioner "remote-exec" {
inline = ["cloud-init status --wait || test $? -eq 2"]
}
}
resource "hcloud_server_network" "worker" {
count = var.worker_count
server_id = hcloud_server.worker[count.index].id
subnet_id = hcloud_network_subnet.cluster.id
}