-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcluster.tf
More file actions
270 lines (248 loc) · 9.07 KB
/
Copy pathcluster.tf
File metadata and controls
270 lines (248 loc) · 9.07 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Talos Cluster Configuration on Proxmox
# Talos Machine Secrets
resource "talos_machine_secrets" "machine_secrets" {}
# Talos Client Configuration
data "talos_client_configuration" "talosconfig" {
cluster_name = var.cluster_name
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
endpoints = [var.cluster_vip, var.talos_cp_01_ip_addr, var.talos_cp_02_ip_addr, var.talos_cp_03_ip_addr]
}
# Control Plane Machine Configurations
# All control planes use the VIP as the cluster endpoint for HA
data "talos_machine_configuration" "machineconfig_cp" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${var.cluster_vip}:6443"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.machine_secrets.machine_secrets
kubernetes_version = var.kubernetes_version
config_patches = [
yamlencode({
machine = {
network = {
interfaces = [{
interface = "eth0"
vip = {
ip = var.cluster_vip
}
}]
}
}
})
]
}
data "talos_machine_configuration" "machineconfig_cp_02" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${var.cluster_vip}:6443"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.machine_secrets.machine_secrets
kubernetes_version = var.kubernetes_version
config_patches = [
yamlencode({
machine = {
network = {
interfaces = [{
interface = "eth0"
vip = {
ip = var.cluster_vip
}
}]
}
}
})
]
}
data "talos_machine_configuration" "machineconfig_cp_03" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${var.cluster_vip}:6443"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.machine_secrets.machine_secrets
kubernetes_version = var.kubernetes_version
config_patches = [
yamlencode({
machine = {
network = {
interfaces = [{
interface = "eth0"
vip = {
ip = var.cluster_vip
}
}]
}
}
})
]
}
# Apply Control Plane Configurations
resource "talos_machine_configuration_apply" "cp_config_apply" {
depends_on = [proxmox_virtual_environment_vm.talos_cp_01]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
machine_configuration_input = data.talos_machine_configuration.machineconfig_cp.machine_configuration
node = var.talos_cp_01_ip_addr
}
resource "talos_machine_configuration_apply" "cp_config_apply_02" {
depends_on = [proxmox_virtual_environment_vm.talos_cp_02]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
machine_configuration_input = data.talos_machine_configuration.machineconfig_cp_02.machine_configuration
node = var.talos_cp_02_ip_addr
}
resource "talos_machine_configuration_apply" "cp_config_apply_03" {
depends_on = [proxmox_virtual_environment_vm.talos_cp_03]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
machine_configuration_input = data.talos_machine_configuration.machineconfig_cp_03.machine_configuration
node = var.talos_cp_03_ip_addr
}
# Worker Machine Configurations
# All workers use the VIP as the cluster endpoint for HA
# Includes Longhorn requirements: kubelet extra mounts for MountPropagation
data "talos_machine_configuration" "machineconfig_worker" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${var.cluster_vip}:6443"
machine_type = "worker"
machine_secrets = talos_machine_secrets.machine_secrets.machine_secrets
kubernetes_version = var.kubernetes_version
config_patches = [
yamlencode({
machine = {
kubelet = {
extraMounts = [
{
destination = "/var/lib/longhorn"
type = "bind"
source = "/var/lib/longhorn"
options = ["bind", "rshared", "rw"]
}
]
}
}
})
]
}
data "talos_machine_configuration" "machineconfig_worker_02" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${var.cluster_vip}:6443"
machine_type = "worker"
machine_secrets = talos_machine_secrets.machine_secrets.machine_secrets
kubernetes_version = var.kubernetes_version
config_patches = [
yamlencode({
machine = {
kubelet = {
extraMounts = [
{
destination = "/var/lib/longhorn"
type = "bind"
source = "/var/lib/longhorn"
options = ["bind", "rshared", "rw"]
}
]
}
}
})
]
}
data "talos_machine_configuration" "machineconfig_worker_03" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${var.cluster_vip}:6443"
machine_type = "worker"
machine_secrets = talos_machine_secrets.machine_secrets.machine_secrets
kubernetes_version = var.kubernetes_version
config_patches = [
yamlencode({
machine = {
kubelet = {
extraMounts = [
{
destination = "/var/lib/longhorn"
type = "bind"
source = "/var/lib/longhorn"
options = ["bind", "rshared", "rw"]
}
]
}
}
})
]
}
# Apply Worker Configurations
resource "talos_machine_configuration_apply" "worker_config_apply" {
depends_on = [proxmox_virtual_environment_vm.talos_worker_01]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
machine_configuration_input = data.talos_machine_configuration.machineconfig_worker.machine_configuration
node = var.talos_worker_01_ip_addr
}
resource "talos_machine_configuration_apply" "worker_config_apply_02" {
depends_on = [proxmox_virtual_environment_vm.talos_worker_02]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
machine_configuration_input = data.talos_machine_configuration.machineconfig_worker_02.machine_configuration
node = var.talos_worker_02_ip_addr
}
resource "talos_machine_configuration_apply" "worker_config_apply_03" {
depends_on = [proxmox_virtual_environment_vm.talos_worker_03]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
machine_configuration_input = data.talos_machine_configuration.machineconfig_worker_03.machine_configuration
node = var.talos_worker_03_ip_addr
}
# Bootstrap Control Plane (commented out - cluster already bootstrapped)
resource "talos_machine_bootstrap" "bootstrap" {
depends_on = [talos_machine_configuration_apply.cp_config_apply_02]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
node = var.talos_cp_01_ip_addr
}
# Cluster Health Check
data "talos_cluster_health" "health" {
depends_on = [
talos_machine_configuration_apply.cp_config_apply_02,
talos_machine_configuration_apply.cp_config_apply_03,
talos_machine_configuration_apply.worker_config_apply,
talos_machine_configuration_apply.worker_config_apply_02,
talos_machine_configuration_apply.worker_config_apply_03
]
client_configuration = data.talos_client_configuration.talosconfig.client_configuration
control_plane_nodes = [
var.talos_cp_01_ip_addr,
var.talos_cp_02_ip_addr,
var.talos_cp_03_ip_addr
]
worker_nodes = [
var.talos_worker_01_ip_addr,
var.talos_worker_02_ip_addr,
var.talos_worker_03_ip_addr
]
endpoints = data.talos_client_configuration.talosconfig.endpoints
}
# Retrieve Kubeconfig
resource "talos_cluster_kubeconfig" "kubeconfig" {
depends_on = [data.talos_cluster_health.health]
client_configuration = talos_machine_secrets.machine_secrets.client_configuration
node = var.talos_cp_01_ip_addr
}
# Outputs
output "talosconfig" {
value = data.talos_client_configuration.talosconfig.talos_config
sensitive = true
}
output "kubeconfig" {
value = talos_cluster_kubeconfig.kubeconfig.kubeconfig_raw
sensitive = true
}
# Custom Script for Configuration
resource "null_resource" "run_custom_script" {
provisioner "local-exec" {
command = <<EOT
mkdir -p ~/.kube ~/.talos
terraform output -raw kubeconfig > ~/.kube/config
terraform output -raw talosconfig > ~/.talos/config
chmod 600 ~/.kube/config ~/.talos/config
EOT
}
triggers = {
kubeconfig = talos_cluster_kubeconfig.kubeconfig.kubeconfig_raw
talosconfig = data.talos_client_configuration.talosconfig.talos_config
timestamp = timestamp() # Ensure the resource always detects changes
}
depends_on = [
talos_cluster_kubeconfig.kubeconfig,
data.talos_client_configuration.talosconfig,
data.talos_cluster_health.health
]
}