-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.tf
138 lines (120 loc) · 4.55 KB
/
server.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
data "hcloud_image" "arm" {
count = var.disable_arm ? 0 : 1
with_selector = "os=talos"
with_architecture = "arm"
most_recent = true
}
data "hcloud_image" "x86" {
count = var.disable_x86 ? 0 : 1
with_selector = "os=talos"
with_architecture = "x86"
most_recent = true
}
locals {
cluster_prefix = var.cluster_prefix ? "${var.cluster_name}-" : ""
control_plane_image_id = substr(var.control_plane_server_type, 0, 3) == "cax" ? data.hcloud_image.arm[0].id : data.hcloud_image.x86[0].id
worker_image_id = substr(var.worker_server_type, 0, 3) == "cax" ? data.hcloud_image.arm[0].id : data.hcloud_image.x86[0].id
control_planes = [for i in range(var.control_plane_count) : {
index = i
name = "${local.cluster_prefix}control-plane-${i + 1}"
ipv4_public = local.control_plane_public_ipv4_list[i],
ipv6_public = var.enable_ipv6 ? local.control_plane_public_ipv6_list[i] : null
ipv6_public_subnet = var.enable_ipv6 ? local.control_plane_public_ipv6_subnet_list[i] : null
ipv4_private = local.control_plane_private_ipv4_list[i]
}]
workers = [for i in range(var.worker_count) : {
index = i
name = "${local.cluster_prefix}worker-${i + 1}"
ipv4_public = local.worker_public_ipv4_list[i],
ipv6_public = var.enable_ipv6 ? local.worker_public_ipv6_list[i] : null
ipv6_public_subnet = var.enable_ipv6 ? local.worker_public_ipv6_subnet_list[i] : null
ipv4_private = local.worker_private_ipv4_list[i]
}]
}
resource "tls_private_key" "ssh_key" {
count = var.ssh_public_key == null ? 1 : 0
algorithm = "ED25519"
}
resource "hcloud_ssh_key" "this" {
name = "${local.cluster_prefix}default"
public_key = coalesce(var.ssh_public_key, can(tls_private_key.ssh_key[0].public_key_openssh) ? tls_private_key.ssh_key[0].public_key_openssh : null)
labels = {
"cluster" = var.cluster_name
}
}
resource "hcloud_server" "control_planes" {
for_each = { for control_plane in local.control_planes : control_plane.name => control_plane }
datacenter = data.hcloud_datacenter.this.name
name = each.value.name
image = local.control_plane_image_id
server_type = var.control_plane_server_type
user_data = data.talos_machine_configuration.control_plane[each.value.name].machine_configuration
ssh_keys = [hcloud_ssh_key.this.id]
placement_group_id = hcloud_placement_group.control_plane.id
labels = {
"cluster" = var.cluster_name,
"role" = "control-plane"
}
firewall_ids = [
hcloud_firewall.this.id
]
public_net {
ipv4_enabled = true
ipv4 = hcloud_primary_ip.control_plane_ipv4[each.value.index].id
ipv6_enabled = var.enable_ipv6
ipv6 = var.enable_ipv6 ? hcloud_primary_ip.control_plane_ipv6[each.value.index].id : null
}
network {
network_id = hcloud_network_subnet.nodes.network_id
ip = each.value.ipv4_private
alias_ips = [] # fix for https://github.com/hetznercloud/terraform-provider-hcloud/issues/650
}
depends_on = [
hcloud_network_subnet.nodes,
data.talos_machine_configuration.control_plane
]
lifecycle {
ignore_changes = [
user_data,
image
]
}
}
resource "hcloud_server" "workers" {
for_each = { for worker in local.workers : worker.name => worker }
datacenter = data.hcloud_datacenter.this.name
name = each.value.name
image = local.worker_image_id
server_type = var.worker_server_type
user_data = data.talos_machine_configuration.worker[each.value.name].machine_configuration
ssh_keys = [hcloud_ssh_key.this.id]
placement_group_id = hcloud_placement_group.worker.id
labels = {
"cluster" = var.cluster_name,
"role" = "worker"
}
firewall_ids = [
hcloud_firewall.this.id
]
public_net {
ipv4_enabled = true
ipv4 = hcloud_primary_ip.worker_ipv4[each.value.index].id
ipv6_enabled = var.enable_ipv6
ipv6 = var.enable_ipv6 ? hcloud_primary_ip.worker_ipv6[each.value.index].id : null
}
network {
network_id = hcloud_network_subnet.nodes.network_id
ip = each.value.ipv4_private
alias_ips = [] # fix for https://github.com/hetznercloud/terraform-provider-hcloud/issues/650
}
depends_on = [
hcloud_network_subnet.nodes,
data.talos_machine_configuration.worker
]
lifecycle {
ignore_changes = [
user_data,
image
]
}
}