-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrol-plane.tf
210 lines (178 loc) · 5.55 KB
/
control-plane.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
resource "libvirt_volume" "control_plane" {
name = "control-plane-volume${count.index + 1}"
base_volume_id = libvirt_volume.cloud_image.id
size = var.control_plane_disk_size
count = var.control_planes
}
data "template_file" "control_plane_repositories" {
template = file("cloud-init/repository.tpl")
count = length(var.repositories)
vars = {
repository_url = element(values(var.repositories), count.index)
repository_name = element(keys(var.repositories), count.index)
}
}
data "template_file" "control_plane_commands" {
template = file("cloud-init/commands.tpl")
count = length(var.packages)
vars = {
packages = join(", ", var.packages)
}
}
data "template_file" "control_plane_cloud_init" {
template = file("cloud-init/common.tpl")
count = var.control_planes
vars = {
hostname = "cp${count.index + 1}"
locale = var.locale
timezone = var.timezone
authorized_keys = join("\n", formatlist(" - %s", var.authorized_keys))
packages = join("\n", formatlist(" - %s", var.packages))
repositories = join("\n", data.template_file.control_plane_repositories.*.rendered)
commands = join("\n", data.template_file.control_plane_commands.*.rendered)
}
}
resource "libvirt_cloudinit_disk" "control_plane" {
count = var.control_planes
name = "control-plane-cloudinit-disk${count.index + 1}"
pool = var.pool
user_data = data.template_file.control_plane_cloud_init[count.index].rendered
}
resource "libvirt_domain" "control_plane" {
count = var.control_planes
name = "cp${count.index + 1}"
memory = var.control_plane_memory
vcpu = var.control_plane_vcpu
cloudinit = element(libvirt_cloudinit_disk.control_plane.*.id, count.index)
cpu {
mode = "host-passthrough"
}
disk {
volume_id = element(libvirt_volume.control_plane.*.id, count.index)
}
network_interface {
network_name = var.network_name
hostname = "cp${count.index + 1}"
wait_for_lease = true
}
graphics {
type = "vnc"
listen_type = "address"
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
}
resource "null_resource" "control_plane_wait_cloudinit" {
depends_on = [libvirt_domain.control_plane]
count = var.control_planes
connection {
host = element(
libvirt_domain.control_plane.*.network_interface.0.addresses.0,
count.index
)
user = var.username
type = "ssh"
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "remote-exec" {
inline = [
"cloud-init status --wait > /dev/null",
]
}
}
resource "null_resource" "control_plane_provision" {
depends_on = [null_resource.control_plane_wait_cloudinit]
count = var.control_planes
connection {
host = element(
libvirt_domain.control_plane.*.network_interface.0.addresses.0,
count.index
)
user = var.username
type = "ssh"
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "remote-exec" {
script = "provision-os-node.sh"
}
}
data "template_file" "control_plane_containerd_conf" {
template = file("configs/config.toml.tpl")
vars = {
registry_mirror = var.registry_mirror
}
}
resource "null_resource" "control_plane_provision_k8s_containerd" {
depends_on = [null_resource.control_plane_provision]
count = var.kubernetes_enable ? var.control_planes : 0
connection {
host = element(
libvirt_domain.control_plane.*.network_interface.0.addresses.0,
count.index
)
user = var.username
type = "ssh"
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "file" {
content = data.template_file.control_plane_containerd_conf.rendered
destination = "/tmp/config.toml"
}
provisioner "remote-exec" {
inline = [
"sudo mkdir /etc/containerd",
"sudo mv /tmp/config.toml /etc/containerd",
"sudo chown root:root /etc/containerd/config.toml",
]
}
provisioner "file" {
source = "provision-k8s-node.sh"
destination = "/tmp/provision-k8s-node.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/provision-k8s-node.sh",
"/tmp/provision-k8s-node.sh ${var.containerd_version} ${var.kubernetes_version} ${var.kubernetes_release} ${libvirt_domain.control_plane.0.network_interface.0.addresses.0}",
]
}
provisioner "file" {
source = "provision-k8s-cp.sh"
destination = "/tmp/provision-k8s-cp.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/provision-k8s-cp.sh",
"/tmp/provision-k8s-cp.sh ${var.helm_version} ${var.kubernetes_version}",
]
}
}
resource "null_resource" "control_plane_reboot" {
depends_on = [null_resource.control_plane_provision_k8s_containerd]
count = var.kubernetes_enable ? var.control_planes : 0
provisioner "local-exec" {
environment = {
user = var.username
host = element(
libvirt_domain.control_plane.*.network_interface.0.addresses.0,
count.index
)
}
command = <<EOT
export sshopts="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -oConnectionAttempts=60"
if ssh $sshopts $user@$host '[ -f /var/run/reboot-required ]'; then
ssh $sshopts $user@$host sudo reboot || :
export delay=5
# wait for node reboot completed
while ssh $sshopts $user@$host '[ -f /var/run/reboot-required ]'; do
sleep $delay
delay=$((delay+1))
[ $delay -gt 60 ] && exit 1
ssh $sshopts $user@$host '[ -f /var/run/reboot-required ]'
done
fi
EOT
}
}