-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
575 lines (529 loc) · 18.5 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# Define required providers
terraform {
required_version = ">= 0.14.0"
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.53.0"
}
}
}
# Configure the OpenStack Provider
provider "openstack" {
cloud = var.cloud
insecure = true
}
#### Network Configuration
# Get external network
data "openstack_networking_network_v2" "external-network" {
name = "PUBLICNET"
}
# Create a router
resource "openstack_networking_router_v2" "openstack-flex-router" {
name = "openstack-flex-router"
admin_state_up = true
external_network_id = data.openstack_networking_network_v2.external-network.id
}
## Create management network security group for bastion
# Create sec group
resource "openstack_networking_secgroup_v2" "secgroup-bastion" {
name = "openstack-flex-bastion"
}
## Create management network security group bastion rule for public SSH access
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-bastion-public-ssh" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-bastion.id
protocol = "tcp"
port_range_min = "22"
port_range_max = "22"
remote_ip_prefix = "0.0.0.0/0"
}
## Create management network security group bastion rule for public icmp echo requests
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-bastion-public-ping" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-bastion.id
protocol = "icmp"
remote_ip_prefix = "0.0.0.0/0"
}
## Create management network security group for flex nodes
# Create sec group
resource "openstack_networking_secgroup_v2" "secgroup-flex-nodes" {
name = "openstack-flex-nodes"
}
## Create management network security group node rule for local TCP access
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-flex-node-permit-tcp-local" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-flex-nodes.id
protocol = "tcp"
remote_ip_prefix = "172.31.0.0/22"
}
## Create management network security group node rule for local UDP access
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-flex-node-permit-udp-local" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-flex-nodes.id
protocol = "udp"
remote_ip_prefix = "172.31.0.0/22"
}
## Create management network security group node rule for public HTTP access
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-flex-node-public-http" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-flex-nodes.id
protocol = "tcp"
port_range_min = "80"
port_range_max = "80"
remote_ip_prefix = "0.0.0.0/0"
}
## Create management network security group node rule for public HTTPS access
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-flex-node-public-https" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-flex-nodes.id
protocol = "tcp"
port_range_min = "443"
port_range_max = "443"
remote_ip_prefix = "0.0.0.0/0"
}
## Create management network security group node rule for public icmp echo requests
# Create sec group rule
resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-flex-node-public-ping" {
direction = "ingress"
ethertype = "IPv4"
security_group_id = openstack_networking_secgroup_v2.secgroup-flex-nodes.id
protocol = "icmp"
remote_ip_prefix = "0.0.0.0/0"
}
## Management network
# Create management network
resource "openstack_networking_network_v2" "openstack-flex" {
name = "openstack-flex"
admin_state_up = "true"
external = false
port_security_enabled = true
}
# Create management subnet
resource "openstack_networking_subnet_v2" "openstack-flex-subnet" {
name = "openstack-flex-subnet"
network_id = openstack_networking_network_v2.openstack-flex.id
cidr = "172.31.0.0/22"
ip_version = 4
enable_dhcp = true
allocation_pool {
start = "172.31.0.10"
end = "172.31.2.254"
}
}
# Create management router interface
resource "openstack_networking_router_interface_v2" "openstack-flex-router-interface" {
router_id = openstack_networking_router_v2.openstack-flex-router.id
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
## Internal Network
# Create internal network
resource "openstack_networking_network_v2" "openstack-flex-internal" {
name = "openstack-flex-internal"
admin_state_up = "true"
external = false
port_security_enabled = false
}
# Create internal subnet
resource "openstack_networking_subnet_v2" "openstack-flex-subnet-internal" {
name = "openstack-flex-subnet-internal"
network_id = openstack_networking_network_v2.openstack-flex-internal.id
cidr = "192.168.0.0/22"
no_gateway = true
ip_version = 4
enable_dhcp = false
}
## Compute Network
# Create compute network
resource "openstack_networking_network_v2" "openstack-flex-compute" {
name = "openstack-flex-compute"
admin_state_up = "true"
external = false
port_security_enabled = false
}
# Create compute subnet
resource "openstack_networking_subnet_v2" "openstack-flex-subnet-compute" {
name = "openstack-flex-subnet-compute"
network_id = openstack_networking_network_v2.openstack-flex-compute.id
cidr = "192.168.100.0/22"
no_gateway = true
ip_version = 4
enable_dhcp = false
}
resource "openstack_compute_keypair_v2" "mykey" {
name = "mykey"
public_key = file(var.ssh_public_key_path)
}
# Create network ports for k8s nodes
resource "openstack_networking_port_v2" "kubernetes-ports" {
count = var.kubernetes_count
name = format("kubernetes%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create kubernetes nodes
resource "openstack_compute_instance_v2" "k8s-controller" {
count = var.kubernetes_count
name = format("kubernetes%02d.%s", count.index + 1, var.cluster_name)
image_name = var.kubernetes_image
flavor_name = var.kubernetes_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.kubernetes-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("kubernetes%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "kubernetes"
}
}
# Create network ports for controller nodes
resource "openstack_networking_port_v2" "controller-ports" {
count = var.controller_count
name = format("controller%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create controller nodes
resource "openstack_compute_instance_v2" "openstack-controller" {
count = var.controller_count
name = format("controller%02d.%s", count.index + 1, var.cluster_name)
image_name = var.controller_image
flavor_name = var.controller_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.controller-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("controller%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "controller"
}
}
# Create network ports for worker nodes
resource "openstack_networking_port_v2" "worker-ports" {
count = var.worker_count
name = format("worker%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create worker nodes
resource "openstack_compute_instance_v2" "openstack-worker" {
count = var.worker_count
name = format("worker%02d.%s", count.index + 1, var.cluster_name)
image_name = var.worker_image
flavor_name = var.worker_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.worker-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("worker%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "worker"
}
}
# Create network ports for compute nodes
resource "openstack_networking_port_v2" "compute-ports" {
count = var.compute_count
name = format("compute%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create compute nodes
resource "openstack_compute_instance_v2" "compute-node" {
count = var.compute_count
name = format("compute%02d.%s", count.index + 1, var.cluster_name)
image_name = var.compute_image
flavor_name = var.compute_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.compute-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("compute%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "compute"
}
}
# Create network ports for network nodes
resource "openstack_networking_port_v2" "network-ports" {
count = var.network_count
name = format("network%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create network nodes
resource "openstack_compute_instance_v2" "network-node" {
count = var.network_count
name = format("network%02d.%s", count.index + 1, var.cluster_name)
image_name = var.network_image
flavor_name = var.network_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.network-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("network%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "network"
}
}
# Create network ports for storage nodes
resource "openstack_networking_port_v2" "storage-ports" {
count = var.storage_count
name = format("storage%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create storage nodes
resource "openstack_compute_instance_v2" "storage-node" {
count = var.storage_count
name = format("storage%02d.%s", count.index + 1, var.cluster_name)
image_name = var.storage_image
flavor_name = var.storage_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.storage-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("storage%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "storage"
}
}
# Create network ports for ceph nodes
resource "openstack_networking_port_v2" "ceph-ports" {
count = var.storage_count
name = format("ceph%02d.%s", count.index + 1, var.cluster_name)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-flex-nodes.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create ceph nodes
resource "openstack_compute_instance_v2" "ceph-node" {
count = var.ceph_count
name = format("ceph%02d.%s", count.index + 1, var.cluster_name)
image_name = var.ceph_image
flavor_name = var.ceph_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.ceph-ports[count.index].id
}
network {
name = openstack_networking_network_v2.openstack-flex-internal.name
}
network {
name = openstack_networking_network_v2.openstack-flex-compute.name
}
metadata = {
hostname = format("ceph%02d", count.index + 1)
group = "openstack-flex"
cluster_name = var.cluster_name
role = "ceph"
}
}
# Create storage volumes and attach to storage nodes
module "storage-volumes" {
source = "./modules/storage-volumes"
for_each = {for item in openstack_compute_instance_v2.storage-node : item.name => item.id}
instance-name = each.key
instance-uuid = each.value
}
# Create storage volumes and attach to ceph nodes
module "ceph-volumes" {
source = "./modules/ceph-volumes"
for_each = {for item in openstack_compute_instance_v2.ceph-node : item.name => item.id}
instance-name = each.key
instance-uuid = each.value
}
# Create admin port for bastion node
resource "openstack_networking_port_v2" "bastion" {
name = "bastion"
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup-bastion.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
}
dynamic "allowed_address_pairs" {
for_each = toset(var.mlb_vips)
content {
ip_address = allowed_address_pairs.value
}
}
}
# Create bastion node
data "template_file" "cloudinit" {
template = file("./scripts/cloudinit/configure.yaml")
}
resource "openstack_compute_instance_v2" "bastion" {
name = format("openstack-flex-launcher.%s", var.cluster_name)
image_name = var.bastion_image
flavor_name = var.bastion_flavor
key_pair = openstack_compute_keypair_v2.mykey.name
network {
port = openstack_networking_port_v2.bastion.id
}
metadata = {
hostname = "openstack-flex-node-launcher"
role = "flex-launcher"
group = "openstack-flex"
cluster_name = var.cluster_name
# Ip addresses in a format for adding to k8s IPAddressPool
metallb_flips = jsonencode([for ip in var.mlb_vips : "${ip}/32"])
}
user_data = data.template_file.cloudinit.rendered
}
# Create network port for metallb_vips
resource "openstack_networking_port_v2" "mlbvips" {
count = length(var.mlb_vips)
name = format("mlbvip%02d", count.index + 1)
network_id = openstack_networking_network_v2.openstack-flex.id
admin_state_up = "true"
no_security_groups = "true"
fixed_ip {
subnet_id = openstack_networking_subnet_v2.openstack-flex-subnet.id
ip_address = var.mlb_vips[count.index]
}
}
# Create floating ip for bastion/jump server
resource "openstack_networking_floatingip_v2" "bastion" {
pool = "PUBLICNET"
port_id = openstack_networking_port_v2.bastion.id
}
# Create floating ip for metallb_vip
resource "openstack_networking_floatingip_v2" "mlbflips" {
count = length(var.mlb_vips)
pool = "PUBLICNET"
port_id = openstack_networking_port_v2.mlbvips[count.index].id
}
output "bastion_flip" {
value = openstack_networking_floatingip_v2.bastion.address
}
output "metallb_flips" {
value = zipmap(var.mlb_vips, openstack_networking_floatingip_v2.mlbflips[*].address)
}