-
Notifications
You must be signed in to change notification settings - Fork 11
/
mgmtcluster.tf
414 lines (362 loc) · 14.5 KB
/
mgmtcluster.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# - ssh keypair -
resource "openstack_compute_keypair_v2" "keypair" {
name = "${var.prefix}-keypair"
}
# - application credential -
resource "openstack_identity_application_credential_v3" "appcred" {
name = "${var.prefix}-appcred"
description = "Credential for the ${var.prefix} management"
unrestricted = true
}
data "openstack_networking_network_v2" "extnet" {
external = true
name = var.external != "" ? var.external : null
network_id = var.external_id != "" ? var.external_id : null
}
# - management cluster -
resource "openstack_networking_floatingip_v2" "mgmtcluster_floatingip" {
pool = data.openstack_networking_network_v2.extnet.name
depends_on = [openstack_networking_router_interface_v2.router_interface]
description = "Floating IP for the ${var.prefix} management cluster node"
tags = [
"${var.prefix}-mgmtcluster", "k8s-cluster-api-mgmtcluster"
]
}
resource "openstack_networking_port_v2" "mgmtcluster_port" {
network_id = openstack_networking_network_v2.network_mgmt.id
name = "${var.prefix}-port"
security_group_ids = [
openstack_compute_secgroup_v2.security_group_mgmt.id,
]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.subnet_mgmt.id
}
}
resource "openstack_networking_floatingip_associate_v2" "mgmtcluster_floatingip_association" {
floating_ip = openstack_networking_floatingip_v2.mgmtcluster_floatingip.address
port_id = openstack_networking_port_v2.mgmtcluster_port.id
}
locals {
clouds = yamldecode(file("mycloud.${var.cloud_provider}.yaml"))
}
data "openstack_images_image_ids_v2" "images" {
name = var.image
sort = "updated_at:desc"
}
resource "openstack_blockstorage_volume_v3" "mgmtcluster_volume" {
size = 30
name = "${var.prefix}-mgmtcluster"
image_id = data.openstack_images_image_ids_v2.images.ids[0]
description = "Volume for the ${var.prefix} management cluster node"
}
resource "openstack_compute_instance_v2" "mgmtcluster_server" {
name = "${var.prefix}-mgmtcluster"
flavor_name = var.kind_flavor
availability_zone = var.availability_zone
key_pair = openstack_compute_keypair_v2.keypair.name
# image_name = var.image
network { port = openstack_networking_port_v2.mgmtcluster_port.id }
block_device {
uuid = openstack_blockstorage_volume_v3.mgmtcluster_volume.id
source_type = "volume"
boot_index = 0
destination_type = "volume"
delete_on_termination = true
}
lifecycle {
# Prevents OpenTofu from trying to destroy the instance when it was created before update with labeling its volume
ignore_changes = [block_device]
}
user_data = <<-EOF
#cloud-config
final_message: "The system is finally up, after $UPTIME seconds"
%{if var.http_proxy != ""}
apt:
http_proxy: "${var.http_proxy}"
https_proxy: "${var.http_proxy}"
%{endif}
package_update: true
package_upgrade: true
write_files:
- path: /tmp/daemon.json
content: |
{
%{if var.http_proxy != ""}
"proxies": {
"http-proxy": "${var.http_proxy}",
"https-proxy": "${var.http_proxy}",
"no-proxy": ".svc,.svc.cluster,.svc.cluster.local,127.0.0.0/8,169.254.169.254/32,fd00:ec2::254/128,${var.node_cidr},${var.pod_cidr},${var.service_cidr},${var.no_proxy}"
},
%{endif}
"mtu": ${var.kind_mtu}
}
owner: root:root
permissions: '0644'
- path: /etc/needrestart/conf.d/needrestart.conf
content: |
$nrconf{kernelhints} = -1;
$nrconf{restart} = 'a';
owner: root:root
permissions: '0644'
- path: /etc/profile.d/proxy.sh
content: |
PROXY="${var.http_proxy}"
if [[ ! -z "$PROXY" ]]; then
export HTTP_PROXY=$PROXY
export HTTPS_PROXY=$PROXY
export http_proxy=$PROXY
export https_proxy=$PROXY
export NO_PROXY=".svc,.svc.cluster,.svc.cluster.local,127.0.0.0/8,169.254.169.254/32,fd00:ec2::254/128,${var.node_cidr},${var.pod_cidr},${var.service_cidr},${var.no_proxy}"
export no_proxy=".svc,.svc.cluster,.svc.cluster.local,127.0.0.0/8,169.254.169.254/32,fd00:ec2::254/128,${var.node_cidr},${var.pod_cidr},${var.service_cidr},${var.no_proxy}"
fi
owner: root:root
permissions: '0644'
%{if var.http_proxy != ""}
- path: /tmp/docker-proxy-config.json
content: |
{
"proxies": {
"default": {
"httpProxy": "${var.http_proxy}",
"httpsProxy": "${var.http_proxy}",
"noProxy": ".svc,.svc.cluster,.svc.cluster.local,127.0.0.0/8,169.254.169.254/32,fd00:ec2::254/128,${var.node_cidr},${var.pod_cidr},${var.service_cidr},${var.no_proxy}"
}
}
}
owner: root:root
permissions: '0644'
%{endif}
runcmd:
# Note: Needrestart is part of the `apt-get upgrade` process from Ubuntu 22.04. By default, it is set to an
# "interactive" mode which causes the interruption of scripts. The interactive mode is applied when the new kernel
# version is available after the upgrade process and when upgraded services need to restart. A custom configuration
# file overrides mentioned settings and ensures that kernel hints are printed only to stderr and services are
# restarted automatically if needed.
- echo nf_conntrack > /etc/modules-load.d/90-nf_conntrack.conf
- modprobe nf_conntrack
- echo net.netfilter.nf_conntrack_max=131072 > /etc/sysctl.d/90-conntrack_max.conf
- sysctl -w -p /etc/sysctl.d/90-conntrack_max.conf
- mkdir /etc/docker
- HOME=/home/${var.ssh_username} /tmp/get_mtu.sh
- mv /tmp/daemon.json /etc/docker/daemon.json
- groupadd docker
- usermod -aG docker ${var.ssh_username}
- apt-get -y install --no-install-recommends --no-install-suggests docker.io yamllint qemu-utils git
# had to use this workaround due to ordering issues: https://bugs.launchpad.net/cloud-init/+bug/1486113
- test -f /tmp/docker-proxy-config.json && mkdir -p /home/${var.ssh_username}/.docker && cp /tmp/docker-proxy-config.json /home/${var.ssh_username}/.docker/config.json && chown -R ${var.ssh_username}:${var.ssh_username} /home/${var.ssh_username}/.docker
%{if var.http_proxy != ""}
- type snap && snap set system proxy.http="${var.http_proxy}"
- type snap && snap set system proxy.https="${var.http_proxy}"
%{endif}
EOF
}
resource "terraform_data" "cacert" {
depends_on = [
openstack_compute_instance_v2.mgmtcluster_server
]
count = contains(keys(local.clouds), "cacert") ? 1 : 0
triggers_replace = [
openstack_networking_floatingip_v2.mgmtcluster_floatingip.address,
file(local.clouds["cacert"])
]
connection {
host = openstack_networking_floatingip_v2.mgmtcluster_floatingip.address
private_key = openstack_compute_keypair_v2.keypair.private_key
user = var.ssh_username
}
provisioner "remote-exec" {
inline = [
"mkdir -p /home/${var.ssh_username}/cluster-defaults"
]
}
provisioner "file" {
source = local.clouds["cacert"]
destination = "/home/${var.ssh_username}/cluster-defaults/${var.cloud_provider}-cacert"
}
}
resource "terraform_data" "mgmtcluster_containerd_registry_host_files" {
depends_on = [
openstack_compute_instance_v2.mgmtcluster_server
]
for_each = toset(var.containerd_registry_files["hosts"])
triggers_replace = [
openstack_networking_floatingip_v2.mgmtcluster_floatingip.address,
file(each.key)
]
connection {
host = openstack_networking_floatingip_v2.mgmtcluster_floatingip.address
private_key = openstack_compute_keypair_v2.keypair.private_key
user = var.ssh_username
}
provisioner "remote-exec" {
inline = [
"mkdir -p /home/${var.ssh_username}/cluster-defaults/containerd/hosts"
]
}
provisioner "file" {
source = each.key
destination = "/home/${var.ssh_username}/cluster-defaults/containerd/hosts/${basename(each.key)}"
}
}
resource "terraform_data" "mgmtcluster_containerd_registry_cert_files" {
depends_on = [
openstack_compute_instance_v2.mgmtcluster_server
]
for_each = toset(var.containerd_registry_files["certs"])
triggers_replace = [
openstack_networking_floatingip_v2.mgmtcluster_floatingip.address,
file(each.key)
]
connection {
host = openstack_networking_floatingip_v2.mgmtcluster_floatingip.address
private_key = openstack_compute_keypair_v2.keypair.private_key
user = var.ssh_username
}
provisioner "remote-exec" {
inline = [
"mkdir -p /home/${var.ssh_username}/cluster-defaults/containerd/certs"
]
}
provisioner "file" {
source = each.key
destination = "/home/${var.ssh_username}/cluster-defaults/containerd/certs/${basename(each.key)}"
}
}
resource "terraform_data" "mgmtcluster_bootstrap_files" {
depends_on = [
openstack_compute_instance_v2.mgmtcluster_server,
terraform_data.mgmtcluster_containerd_registry_host_files,
terraform_data.mgmtcluster_containerd_registry_cert_files
]
triggers_replace = [
openstack_networking_floatingip_v2.mgmtcluster_floatingip.address
]
connection {
host = openstack_networking_floatingip_v2.mgmtcluster_floatingip.address
private_key = openstack_compute_keypair_v2.keypair.private_key
user = var.ssh_username
}
provisioner "remote-exec" {
inline = [
"mkdir -p /home/${var.ssh_username}/.config/openstack"
]
}
provisioner "file" {
content = openstack_compute_keypair_v2.keypair.private_key
destination = "/home/${var.ssh_username}/.ssh/id_rsa"
}
provisioner "file" {
source = "files/bin/get_k8s_git.sh"
destination = "/tmp/get_k8s_git.sh"
}
provisioner "file" {
source = "files/bin/wait.sh"
destination = "/tmp/wait.sh"
}
provisioner "file" {
source = "files/bin/get_mtu.sh"
destination = "/tmp/get_mtu.sh"
}
provisioner "file" {
content = templatefile("files/template/capi-settings.tmpl", {
cilium_binaries = var.cilium_binaries,
capi_openstack_version = var.capi_openstack_version,
clusterapi_version = var.clusterapi_version,
prefix = var.prefix,
testcluster_name = var.testcluster_name
})
destination = "/home/${var.ssh_username}/.capi-settings"
}
provisioner "file" {
content = templatefile("files/template/clusterctl.yaml.tmpl", {
anti_affinity = var.anti_affinity,
soft_anti_affinity_controller = var.soft_anti_affinity_controller,
availability_zone = var.availability_zone,
capo_instance_create_timeout = var.capo_instance_create_timeout,
cloud_provider = var.cloud_provider,
controller_count = var.controller_count,
controller_flavor = var.controller_flavor,
controller_metadata = var.controller_metadata,
deploy_cert_manager = var.deploy_cert_manager,
deploy_cindercsi = var.deploy_cindercsi,
deploy_flux = var.deploy_flux,
deploy_metrics = var.deploy_metrics,
deploy_nginx_ingress = var.deploy_nginx_ingress,
deploy_gateway_api = var.deploy_gateway_api,
deploy_occm = var.deploy_occm,
dns_nameservers = var.dns_nameservers,
etcd_unsafe_fs = var.etcd_unsafe_fs,
external_id = data.openstack_networking_network_v2.extnet.id,
image_registration_extra_flags = var.image_registration_extra_flags,
kube_image_raw = var.kube_image_raw,
kubernetes_version = var.kubernetes_version,
node_cidr = var.node_cidr,
pod_cidr = var.pod_cidr,
service_cidr = var.service_cidr,
prefix = var.prefix,
restrict_kubeapi = var.restrict_kubeapi,
use_cilium = var.use_cilium,
calico_version = var.calico_version,
use_ovn_lb_provider = var.use_ovn_lb_provider,
worker_count = var.worker_count,
worker_flavor = var.worker_flavor,
worker_metadata = var.worker_metadata
})
destination = "/home/${var.ssh_username}/cluster-defaults/clusterctl.yaml"
}
provisioner "file" {
content = templatefile("files/template/clouds.yaml.tmpl", {
appcredid = openstack_identity_application_credential_v3.appcred.id,
appcredsecret = openstack_identity_application_credential_v3.appcred.secret,
cloud_provider = var.cloud_provider,
clouds = local.clouds,
cacert = contains(keys(local.clouds), "cacert") ? "/home/${var.ssh_username}/cluster-defaults/${var.cloud_provider}-cacert" : "null"
})
destination = "/home/${var.ssh_username}/.config/openstack/clouds.yaml"
}
provisioner "file" {
content = templatefile("files/template/cloud.conf.tmpl", {
clouds = local.clouds,
appcredid = openstack_identity_application_credential_v3.appcred.id,
appcredsecret = openstack_identity_application_credential_v3.appcred.secret
})
destination = "/home/${var.ssh_username}/cluster-defaults/cloud.conf"
}
provisioner "file" {
content = templatefile("files/template/harbor-settings.tmpl", {
deploy_harbor = var.deploy_harbor,
harbor_config = var.harbor_config
})
destination = "/home/${var.ssh_username}/cluster-defaults/harbor-settings"
}
provisioner "file" {
source = "files/template/cluster-template.yaml"
destination = "/home/${var.ssh_username}/cluster-defaults/cluster-template.yaml"
}
provisioner "file" {
source = "files/fix-keystoneauth-plugins-unversioned.diff"
destination = "/tmp/fix-keystoneauth-plugins-unversioned.diff"
}
provisioner "remote-exec" {
inline = [
"chmod 0600 /home/${var.ssh_username}/.ssh/id_rsa /home/${var.ssh_username}/cluster-defaults/clusterctl.yaml /home/${var.ssh_username}/cluster-defaults/cloud.conf /home/${var.ssh_username}/.config/openstack/clouds.yaml",
"chmod 0755 /tmp/*.sh"
]
}
provisioner "remote-exec" {
inline = [
"/tmp/wait.sh"
]
}
provisioner "file" {
source = "extension"
destination = "/home/${var.ssh_username}/"
}
# FIXME: We should get the branch (and warnings for unpushed changes from the Makefile)
provisioner "remote-exec" {
inline = [
"/tmp/get_k8s_git.sh ${var.git_repo} ${var.git_reference}",
"/home/${var.ssh_username}/bin/bootstrap.sh"
]
}
}