-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
348 lines (326 loc) · 11 KB
/
main.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
locals {
cloud_init_volume_name = var.cloud_init_volume_name == "" ? "${var.name}-cloud-init.iso" : var.cloud_init_volume_name
network_interfaces = concat(
[for libvirt_network in var.libvirt_networks: {
network_name = libvirt_network.network_name != "" ? libvirt_network.network_name : null
network_id = libvirt_network.network_id != "" ? libvirt_network.network_id : null
macvtap = null
addresses = null
mac = libvirt_network.mac
hostname = null
}],
[for macvtap_interface in var.macvtap_interfaces: {
network_name = null
network_id = null
macvtap = macvtap_interface.interface
addresses = null
mac = macvtap_interface.mac
hostname = null
}]
)
volumes = var.data_volume_id != "" ? [var.volume_id, var.data_volume_id] : [var.volume_id]
fluentbit_updater_etcd = var.fluentbit.enabled && var.fluentbit_dynamic_config.enabled && var.fluentbit_dynamic_config.source == "etcd"
fluentbit_updater_git = var.fluentbit.enabled && var.fluentbit_dynamic_config.enabled && var.fluentbit_dynamic_config.source == "git"
}
module "network_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//network?ref=v0.26.0"
network_interfaces = concat(
[for idx, libvirt_network in var.libvirt_networks: {
ip = libvirt_network.ip
gateway = libvirt_network.gateway
prefix_length = libvirt_network.prefix_length
interface = "libvirt${idx}"
mac = libvirt_network.mac
dns_servers = libvirt_network.dns_servers
}],
[for idx, macvtap_interface in var.macvtap_interfaces: {
ip = macvtap_interface.ip
gateway = macvtap_interface.gateway
prefix_length = macvtap_interface.prefix_length
interface = "macvtap${idx}"
mac = macvtap_interface.mac
dns_servers = macvtap_interface.dns_servers
}]
)
}
module "prometheus_config_updater_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//configurations-auto-updater?ref=v0.26.0"
install_dependencies = var.install_dependencies
filesystem = {
path = "/etc/prometheus/configs/"
files_permission = "0770"
directories_permission = "0770"
}
etcd = {
key_prefix = var.etcd.key_prefix
endpoints = var.etcd.endpoints
connection_timeout = "10s"
request_timeout = "10s"
retry_interval = "500ms"
retries = 10
auth = {
ca_certificate = var.etcd.ca_certificate
client_certificate = var.etcd.client.certificate
client_key = var.etcd.client.key
username = var.etcd.client.username
password = var.etcd.client.password
}
}
notification_command = {
command = ["/usr/local/bin/reload-prometheus-configs"]
retries = 30
}
naming = {
binary = "prometheus-config-updater"
service = "prometheus-config-updater"
}
user = "prometheus"
vault_agent = {
etcd_auth = {
enabled = var.etcd.vault_agent_secret_path != ""
secret_path = var.etcd.vault_agent_secret_path
}
}
}
module "prometheus_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//prometheus?ref=v0.26.0"
install_dependencies = var.install_dependencies
prometheus = var.prometheus
}
module "prometheus_node_exporter_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//prometheus-node-exporter?ref=v0.26.0"
install_dependencies = var.install_dependencies
}
module "chrony_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//chrony?ref=v0.26.0"
install_dependencies = var.install_dependencies
chrony = {
servers = var.chrony.servers
pools = var.chrony.pools
makestep = var.chrony.makestep
}
}
module "fluentbit_updater_etcd_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//configurations-auto-updater?ref=v0.26.0"
install_dependencies = var.install_dependencies
filesystem = {
path = "/etc/fluent-bit-customization/dynamic-config"
files_permission = "700"
directories_permission = "700"
}
etcd = {
key_prefix = var.fluentbit_dynamic_config.etcd.key_prefix
endpoints = var.fluentbit_dynamic_config.etcd.endpoints
connection_timeout = "60s"
request_timeout = "60s"
retry_interval = "4s"
retries = 15
auth = {
ca_certificate = var.fluentbit_dynamic_config.etcd.ca_certificate
client_certificate = var.fluentbit_dynamic_config.etcd.client.certificate
client_key = var.fluentbit_dynamic_config.etcd.client.key
username = var.fluentbit_dynamic_config.etcd.client.username
password = var.fluentbit_dynamic_config.etcd.client.password
}
}
notification_command = {
command = ["/usr/local/bin/reload-fluent-bit-configs"]
retries = 30
}
naming = {
binary = "fluent-bit-config-updater"
service = "fluent-bit-config-updater"
}
user = "fluentbit"
vault_agent = {
etcd_auth = {
enabled = var.fluentbit_dynamic_config.etcd.vault_agent_secret_path != ""
secret_path = var.fluentbit_dynamic_config.etcd.vault_agent_secret_path
}
}
}
module "fluentbit_updater_git_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//gitsync?ref=v0.26.0"
install_dependencies = var.install_dependencies
filesystem = {
path = "/etc/fluent-bit-customization/dynamic-config"
files_permission = "700"
directories_permission = "700"
}
git = var.fluentbit_dynamic_config.git
notification_command = {
command = ["/usr/local/bin/reload-fluent-bit-configs"]
retries = 30
}
naming = {
binary = "fluent-bit-config-updater"
service = "fluent-bit-config-updater"
}
user = "fluentbit"
}
module "fluentbit_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//fluent-bit?ref=v0.26.0"
install_dependencies = var.install_dependencies
fluentbit = {
metrics = var.fluentbit.metrics
systemd_services = [
{
tag = var.fluentbit.prometheus_tag
service = "prometheus.service"
},
{
tag = var.fluentbit.prometheus_updater_tag
service = "prometheus-config-updater.service"
},
{
tag = var.fluentbit.node_exporter_tag
service = "node-exporter.service"
}
]
forward = var.fluentbit.forward
}
dynamic_config = {
enabled = var.fluentbit_dynamic_config.enabled
entrypoint_path = "/etc/fluent-bit-customization/dynamic-config/index.conf"
}
}
module "vault_agent_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//vault-agent?ref=v0.26.0"
install_dependencies = var.install_dependencies
vault_agent = {
auth_method = var.vault_agent.auth_method
vault_address = var.vault_agent.vault_address
vault_ca_cert = var.vault_agent.vault_ca_cert
extra_config = ""
}
}
module "data_volume_configs" {
source = "git::https://github.com/Ferlab-Ste-Justine/terraform-cloudinit-templates.git//data-volumes?ref=v0.26.0"
volumes = [{
label = "prometheus_data"
device = "vdb"
filesystem = "ext4"
mount_path = "/var/lib/prometheus"
mount_options = "defaults"
}]
}
locals {
cloudinit_templates = concat([
{
filename = "base.cfg"
content_type = "text/cloud-config"
content = templatefile(
"${path.module}/files/user_data.yaml.tpl",
{
hostname = var.name
ssh_admin_public_key = var.ssh_admin_public_key
ssh_admin_user = var.ssh_admin_user
admin_user_password = var.admin_user_password
prometheus_secrets = var.prometheus_secrets
}
)
},
{
filename = "prometheus_config_updater.cfg"
content_type = "text/cloud-config"
content = module.prometheus_config_updater_configs.configuration
},
{
filename = "prometheus.cfg"
content_type = "text/cloud-config"
content = module.prometheus_configs.configuration
},
{
filename = "node_exporter.cfg"
content_type = "text/cloud-config"
content = module.prometheus_node_exporter_configs.configuration
}
],
var.chrony.enabled ? [{
filename = "chrony.cfg"
content_type = "text/cloud-config"
content = module.chrony_configs.configuration
}] : [],
local.fluentbit_updater_etcd ? [{
filename = "fluent_bit_updater.cfg"
content_type = "text/cloud-config"
content = module.fluentbit_updater_etcd_configs.configuration
}] : [],
local.fluentbit_updater_git ? [{
filename = "fluent_bit_updater.cfg"
content_type = "text/cloud-config"
content = module.fluentbit_updater_git_configs.configuration
}] : [],
var.fluentbit.enabled ? [{
filename = "fluent_bit.cfg"
content_type = "text/cloud-config"
content = module.fluentbit_configs.configuration
}] : [],
var.vault_agent.enabled ? [{
filename = "vault_agent.cfg"
content_type = "text/cloud-config"
content = module.vault_agent_configs.configuration
}] : [],
var.data_volume_id != "" ? [{
filename = "data_volume.cfg"
content_type = "text/cloud-config"
content = module.data_volume_configs.configuration
}]: []
)
}
data "template_cloudinit_config" "user_data" {
gzip = false
base64_encode = false
dynamic "part" {
for_each = local.cloudinit_templates
content {
filename = part.value["filename"]
content_type = part.value["content_type"]
content = part.value["content"]
}
}
}
resource "libvirt_cloudinit_disk" "prometheus" {
name = local.cloud_init_volume_name
user_data = data.template_cloudinit_config.user_data.rendered
network_config = module.network_configs.configuration
pool = var.cloud_init_volume_pool
}
resource "libvirt_domain" "prometheus" {
name = var.name
cpu {
mode = "host-passthrough"
}
vcpu = var.vcpus
memory = var.memory
dynamic "disk" {
for_each = local.volumes
content {
volume_id = disk.value
}
}
dynamic "network_interface" {
for_each = local.network_interfaces
content {
network_name = network_interface.value["network_name"]
network_id = network_interface.value["network_id"]
macvtap = network_interface.value["macvtap"]
addresses = network_interface.value["addresses"]
mac = network_interface.value["mac"]
hostname = network_interface.value["hostname"]
}
}
autostart = true
cloudinit = libvirt_cloudinit_disk.prometheus.id
//https://github.com/dmacvicar/terraform-provider-libvirt/blob/main/examples/v0.13/ubuntu/ubuntu-example.tf#L61
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
}