Skip to content

Commit 7507019

Browse files
authored
Support attaching pre-existing volumes to node groups (#998)
* support attaching pre-existing volumes to node groups and output IDs to inventory * fix tf lint errors
1 parent aae120c commit 7507019

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

environments/site/tofu/inventory.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ${cluster_name}_${group_name}:
2929
networks: ${jsonencode({for n in node.network: n.name => {"fixed_ip_v4": n.fixed_ip_v4, "fixed_ip_v6": n.fixed_ip_v6}})}
3030
node_fqdn: ${login_groups[group_name]["fqdns"][nodename]}
3131
node_fip: ${login_groups[group_name]["nodegroup_fips"][nodename]}
32+
volumes: ${jsonencode(try(login_groups[group_name]["volumes"][nodename], {}))}
3233
%{ endfor ~}
3334

3435
${group_name}:
@@ -53,6 +54,7 @@ ${cluster_name}_${group_name}:
5354
instance_id: ${ node.id }
5455
networks: ${jsonencode({for n in node.network: n.name => {"fixed_ip_v4": n.fixed_ip_v4, "fixed_ip_v6": n.fixed_ip_v6}})}
5556
node_fqdn: ${compute_groups[group_name]["fqdns"][nodename]}
57+
volumes: ${jsonencode(try(compute_groups[group_name]["volumes"][nodename], {}))}
5658
%{ endfor ~}
5759
vars:
5860
# NB: this is the target image, not necessarily what is provisioned

environments/site/tofu/node_group/nodes.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ locals {
99
# this is a mapping with
1010
# keys "compute-0-vol-a", "compute-0-vol-b" ...
1111
# values which are a mapping e.g. {"node"="compute-0", "volume"="vol-a"}
12+
all_compute_volume_ids = { for k, v in local.all_compute_volumes : k => try(openstack_blockstorage_volume_v3.compute[k].id, data.openstack_blockstorage_volume_v3.compute[k].id) }
13+
1214

1315
# Workaround for lifecycle meta-argument only taking static values
1416
compute_instances = var.ignore_image_changes ? openstack_compute_instance_v2.compute_fixed_image : openstack_compute_instance_v2.compute
@@ -34,9 +36,14 @@ locals {
3436
baremetal_az = var.availability_zone != null ? var.availability_zone : "nova"
3537
}
3638

39+
data "openstack_blockstorage_volume_v3" "compute" {
40+
for_each = { for k, v in local.all_compute_volumes : k => v if var.extra_volumes[v.volume].size == null }
41+
name = "${var.cluster_name}-${each.key}"
42+
}
43+
3744
resource "openstack_blockstorage_volume_v3" "compute" {
3845

39-
for_each = local.all_compute_volumes
46+
for_each = { for k, v in local.all_compute_volumes : k => v if var.extra_volumes[v.volume].size != null }
4047

4148
name = "${var.cluster_name}-${each.key}"
4249
description = "Compute node ${each.value.node} volume ${each.value.volume}"
@@ -49,7 +56,7 @@ resource "openstack_compute_volume_attach_v2" "compute" {
4956
for_each = local.all_compute_volumes
5057

5158
instance_id = local.compute_instances[each.value.node].id
52-
volume_id = openstack_blockstorage_volume_v3.compute[each.key].id
59+
volume_id = local.all_compute_volume_ids[each.key]
5360
}
5461

5562
resource "openstack_networking_port_v2" "compute" {
@@ -238,3 +245,7 @@ output "fqdns" {
238245
output "nodegroup_fips" {
239246
value = local.nodegroup_fips
240247
}
248+
249+
output "volumes" {
250+
value = { for nn in var.nodes : nn => { for vn, vv in var.extra_volumes : vn => local.all_compute_volume_ids["${nn}-${vn}"] } }
251+
}

environments/site/tofu/node_group/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ variable "extra_volumes" {
6565
EOF
6666
type = map(
6767
object({
68-
size = number
68+
size = optional(number)
6969
volume_type = optional(string)
7070
})
7171
)

environments/site/tofu/variables.tf

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ variable "login" {
6565
vnic_types: Overrides variable vnic_types
6666
volume_backed_instances: Overrides variable volume_backed_instances
6767
root_volume_size: Overrides variable root_volume_size
68-
extra_volumes: Mapping defining additional volumes to create and attach
69-
Keys are unique volume name.
70-
Values are a mapping with:
71-
size: Size of volume in GB
72-
volume_type: Optional. Type of volume, or cloud default
73-
**NB**: The order in /dev is not guaranteed to match the mapping
68+
extra_volumes: Mapping defining additional volumes. Keys are a unique volume
69+
name. Values are a mapping with:
70+
- size: Optional. Size of volume to create in GB.
71+
- volume_type: Optional. Type of volume, or cloud default.
72+
If size is not given then a volume `cluster_name-node-volume_key`
73+
must already exist, where `node` is an entry in this group's
74+
`nodes` parameter. Such volumes will not be managed by the
75+
appliance.
76+
**NB**: The order in /dev is not guaranteed to match the mapping.
7477
fip_addresses: List of addresses of floating IPs to associate with
7578
nodes, in the same order as nodes parameter. The
7679
floating IPs must already be allocated to the project.
@@ -136,12 +139,15 @@ variable "compute" {
136139
ignore_image_changes: Ignore changes to the image_id parameter (see docs/experimental/compute-init.md)
137140
volume_backed_instances: Overrides variable volume_backed_instances
138141
root_volume_size: Overrides variable root_volume_size
139-
extra_volumes: Mapping defining additional volumes to create and attach
140-
Keys are unique volume name.
141-
Values are a mapping with:
142-
size: Size of volume in GB
143-
volume_type: Optional. Type of volume, or cloud default
144-
**NB**: The order in /dev is not guaranteed to match the mapping
142+
extra_volumes: Mapping defining additional volumes. Keys are a unique volume
143+
name. Values are a mapping with:
144+
- size: Optional. Size of volume to create in GB.
145+
- volume_type: Optional. Type of volume, or cloud default.
146+
If size is not given then a volume `cluster_name-node-volume_key`
147+
must already exist, where `node` is an entry in this group's
148+
`nodes` parameter. Such volumes will not be managed by the
149+
appliance.
150+
**NB**: The order in /dev is not guaranteed to match the mapping.
145151
ip_addresses: Mapping of list of fixed IP addresses for nodes, keyed
146152
by network name, in same order as nodes parameter.
147153
For any networks not specified here the cloud will

0 commit comments

Comments
 (0)