Skip to content

Commit 63724c4

Browse files
committed
allow creating tofu-controlled compute volumes
1 parent 4def5ba commit 63724c4

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

environments/skeleton/{{cookiecutter.environment}}/terraform/compute.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module "compute" {
1414
vnic_type = lookup(each.value, "vnic_type", var.vnic_type)
1515
vnic_profile = lookup(each.value, "vnic_profile", var.vnic_profile)
1616
key_pair = var.key_pair
17+
volumes = lookup(each.value, "volumes", {})
18+
1719
environment_root = var.environment_root
1820
k3s_token = var.k3s_token
1921
control_address = [for n in openstack_compute_instance_v2.control["control"].network: n.fixed_ip_v4 if n.access_network][0]

environments/skeleton/{{cookiecutter.environment}}/terraform/compute/nodes.tf

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
locals {
2+
all_compute_volumes = {for v in setproduct(var.nodes, keys(var.volumes)): "${v[0]}-${v[1]}" => {"node" = v[0], "volume" = v[1]}}
3+
# e.g. with
4+
# var.nodes = ["compute-0", "compute-1"]
5+
# var.volumes = {
6+
# "vol-a" = {size = 10},
7+
# "vol-b" = {size = 20}
8+
# }
9+
# this is a mapping with
10+
# keys "compute-0-vol-a", "compute-0-vol-b" ...
11+
# values which are a mapping e.g. {"node"="compute-0", "volume"="vol-a"}
12+
}
13+
14+
resource "openstack_blockstorage_volume_v3" "compute" {
15+
16+
for_each = local.all_compute_volumes
17+
18+
name = "${var.cluster_name}-${each.key}"
19+
description = "Compute node ${each.value.node} volume ${each.value.volume}"
20+
size = var.volumes[each.value.volume].size
21+
}
22+
123
resource "openstack_networking_port_v2" "compute" {
224

325
for_each = toset(var.nodes)
@@ -27,15 +49,24 @@ resource "openstack_compute_instance_v2" "compute" {
2749
flavor_name = var.flavor
2850
key_pair = var.key_pair
2951

52+
# root device:
53+
block_device {
54+
uuid = var.image_id
55+
source_type = "image"
56+
destination_type = var.volume_backed_instances ? "volume" : "local"
57+
volume_size = var.volume_backed_instances ? var.root_volume_size : null
58+
boot_index = 0
59+
delete_on_termination = true
60+
}
61+
62+
# additional volumes:
3063
dynamic "block_device" {
31-
for_each = var.volume_backed_instances ? [1]: []
64+
for_each = var.volumes
3265
content {
33-
uuid = var.image_id
34-
source_type = "image"
3566
destination_type = "volume"
36-
volume_size = var.root_volume_size
37-
boot_index = 0
38-
delete_on_termination = true
67+
source_type = "volume"
68+
boot_index = -1
69+
uuid = openstack_blockstorage_volume_v3.compute["${each.key}-${block_device.key}"].id
3970
}
4071
}
4172

environments/skeleton/{{cookiecutter.environment}}/terraform/compute/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ variable "root_volume_size" {
6464
default = 40
6565
}
6666

67+
variable "volumes" {
68+
description = <<-EOF
69+
Mapping defining volumes to create and attach.
70+
Keys are unique volume name.
71+
Values are a mapping with:
72+
size: Size of volume in GB
73+
EOF
74+
type = any
75+
default = {}
76+
}
77+
6778
variable "security_group_ids" {
6879
type = list
6980
}

0 commit comments

Comments
 (0)