-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.tf
312 lines (266 loc) · 8.35 KB
/
cluster.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
#######################
# Cluster declaration #
#######################
provider "libvirt" {
uri = var.libvirt_uri
}
# This is the CaaSP kvm image that has been created by IBS
resource "libvirt_volume" "img" {
name = "${var.name_prefix}${basename(var.img_source_url)}"
source = var.img_source_url
pool = var.pool
}
##############################
# Configure the DNS Provider #
##############################
provider "dns" {
update {
server = "10.10.10.11"
key_name = "isv-lab.net."
key_algorithm = "hmac-md5"
key_secret = "kDcnxEodcQHr/2sYVloQ9ZCvjB6+76FShItaDdwnHNh/16lGq+pCo2exiIDJKpILAkGG9KZGubdgECvD6zYqzw=="
}
}
# Create a DNS A record set
#resource "dns_a_record_set" "www" {
# ...
#}
##############
# Networking #
##############
resource "libvirt_network" "network" {
name = "${var.name_prefix}net"
mode = var.net_mode
domain = "${var.name_prefix}${var.domain_name}"
addresses = [var.network]
dns {
enabled = true
}
}
################
# Registration #
################
data "template_file" "register_scc" {
template = file("cloud-init/register-scc.tpl")
count = var.caasp_registry_code == "" ? 0 : 1
vars = {
sle_registry_code = var.sle_registry_code
caasp_registry_code = var.caasp_registry_code
}
}
######################
# Load Balancer node #
######################
resource "libvirt_volume" "lb" {
name = "${var.name_prefix}lb.qcow2"
pool = var.pool
size = var.disk_size
base_volume_id = libvirt_volume.img.id
}
data "template_file" "haproxy_backends_master" {
count = var.master_count
template = file("cloud-init/haproxy-backends.tpl")
vars = {
fqdn = "${var.name_prefix}master-${count.index}.${var.name_prefix}${var.domain_name}"
ip = cidrhost(var.network, 512 + count.index)
}
}
data "template_file" "lb_cloud_init_user_data" {
template = file("cloud-init/lb.cfg.tpl")
vars = {
fqdn = "${var.name_prefix}lb-0.${var.name_prefix}${var.domain_name}"
backends = join(
" ",
data.template_file.haproxy_backends_master.*.rendered,
)
authorized_keys = join("\n", formatlist(" - %s", var.authorized_keys))
# repositories = join("\n", data.template_file.zypper_repositories.*.rendered)
repositories = ""
register_scc = join("\n", data.template_file.register_scc.*.rendered)
username = var.username
password = var.password
}
}
resource "libvirt_cloudinit_disk" "lb" {
name = "${var.name_prefix}lb_cloud_init.iso"
pool = var.pool
user_data = data.template_file.lb_cloud_init_user_data.rendered
network_config = file("cloud-init/network.cfg")
}
resource "libvirt_domain" "lb" {
name = "${var.name_prefix}lb"
memory = var.lb_memory
vcpu = var.lb_vcpu
metadata = "lb.${var.domain_name},lb,0"
cloudinit = libvirt_cloudinit_disk.lb.id
# cpu = "host-passthrough"
disk {
volume_id = libvirt_volume.lb.id
}
network_interface {
network_id = libvirt_network.network.id
hostname = "${var.name_prefix}lb"
addresses = [cidrhost(var.network, 256)]
wait_for_lease = true
}
graphics {
type = "vnc"
listen_type = "address"
}
connection {
type = "ssh"
user = "root"
password = "linux"
}
}
output "ip_lb" {
value = libvirt_domain.lb.network_interface[0].addresses[0]
}
#####################
### Cluster masters #
#####################
#data "template_file" "zypper_repositories" {
# template = file("cloud-init/repository.tpl")
# count = length(var.repositories)
# vars {
# repository_url = "${element(values(var.repositories[count.index]), 0)}"
# repository_name = "${element(keys(var.repositories[count.index]), 0)}"
# }
#}
resource "libvirt_volume" "master" {
name = "${var.name_prefix}master_${count.index}.qcow2"
pool = var.pool
size = var.disk_size
base_volume_id = libvirt_volume.img.id
count = var.master_count
}
data "template_file" "master_cloud_init_user_data" {
# needed when 0 master nodes are defined
count = var.master_count
template = file("cloud-init/master.cfg.tpl")
vars = {
fqdn = "${var.name_prefix}master-${count.index}.${var.name_prefix}${var.domain_name}"
authorized_keys = join("\n", formatlist(" - %s", var.authorized_keys))
#repositories = join("\n", data.template_file.zypper_repositories.*.rendered)
repositories = ""
register_scc = join("\n", data.template_file.register_scc.*.rendered)
packages = join("\n", formatlist(" - %s", var.packages))
username = var.username
password = var.password
}
depends_on = [libvirt_domain.lb]
}
resource "libvirt_cloudinit_disk" "master" {
# needed when 0 master nodes are defined
count = var.master_count
name = "${var.name_prefix}master_cloud_init_${count.index}.iso"
pool = var.pool
user_data = element(
data.template_file.master_cloud_init_user_data.*.rendered,
count.index,
)
network_config = file("cloud-init/network.cfg")
}
resource "libvirt_domain" "master" {
count = var.master_count
name = "${var.name_prefix}master-${count.index}"
memory = var.master_memory
vcpu = var.master_vcpu
cloudinit = element(libvirt_cloudinit_disk.master.*.id, count.index)
metadata = "master-${count.index}.${var.domain_name},master,${count.index},${var.name_prefix}"
depends_on = [libvirt_domain.lb]
# cpu {
# mode = "host-passthrough"
# }
disk {
volume_id = element(libvirt_volume.master.*.id, count.index)
}
network_interface {
network_id = libvirt_network.network.id
hostname = "${var.name_prefix}master-${count.index}"
addresses = [cidrhost(var.network, 512 + count.index)]
wait_for_lease = true
}
graphics {
type = "vnc"
listen_type = "address"
}
connection {
type = "ssh"
user = "root"
password = "linux"
}
}
output "masters" {
value = [libvirt_domain.master.*.network_interface.0.addresses.0]
}
####################
## Cluster workers #
####################
resource "libvirt_volume" "worker" {
name = "${var.name_prefix}worker_${count.index}.qcow2"
pool = var.pool
size = var.disk_size
base_volume_id = libvirt_volume.img.id
count = var.worker_count
}
data "template_file" "worker_cloud_init_user_data" {
# needed when 0 worker nodes are defined
count = var.worker_count
template = file("cloud-init/worker.cfg.tpl")
vars = {
fqdn = "${var.name_prefix}worker-${count.index}.${var.name_prefix}${var.domain_name}"
authorized_keys = join("\n", formatlist(" - %s", var.authorized_keys))
# repositories = join("\n", data.template_file.zypper_repositories.*.rendered)
repositories = ""
register_scc = join("\n", data.template_file.register_scc.*.rendered)
packages = join("\n", formatlist(" - %s", var.packages))
username = var.username
password = var.password
}
depends_on = [libvirt_domain.lb]
}
resource "libvirt_cloudinit_disk" "worker" {
# needed when 0 worker nodes are defined
count = var.worker_count
name = "${var.name_prefix}worker_cloud_init_${count.index}.iso"
pool = var.pool
user_data = element(
data.template_file.worker_cloud_init_user_data.*.rendered,
count.index,
)
network_config = file("cloud-init/network.cfg")
}
resource "libvirt_domain" "worker" {
count = var.worker_count
name = "${var.name_prefix}worker-${count.index}"
memory = var.worker_memory
vcpu = var.worker_vcpu
cloudinit = element(libvirt_cloudinit_disk.worker.*.id, count.index)
metadata = "worker-${count.index}.${var.domain_name},worker,${count.index},${var.name_prefix}"
depends_on = [libvirt_domain.lb]
# cpu {
# mode = "host-passthrough"
# }
disk {
volume_id = element(libvirt_volume.worker.*.id, count.index)
}
network_interface {
network_id = libvirt_network.network.id
hostname = "${var.name_prefix}worker-${count.index}"
addresses = [cidrhost(var.network, 768 + count.index)]
wait_for_lease = true
}
graphics {
type = "vnc"
listen_type = "address"
}
connection {
type = "ssh"
user = "root"
password = "linux"
}
}
output "workers" {
value = [libvirt_domain.worker.*.network_interface.0.addresses.0]
}