From e74f90b7c152a6fb89a19e1d6b7be0620a3cf6cb Mon Sep 17 00:00:00 2001 From: Maxime Boissonneault Date: Tue, 14 Jan 2025 14:48:40 -0500 Subject: [PATCH] implement (un)managed volumes for aws, gcp and azure --- aws/infrastructure.tf | 18 +++++++++++++++--- azure/infrastructure.tf | 13 +++++++++++-- gcp/infrastructure.tf | 14 +++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/aws/infrastructure.tf b/aws/infrastructure.tf index 60443dd3..1616f186 100644 --- a/aws/infrastructure.tf +++ b/aws/infrastructure.tf @@ -130,7 +130,9 @@ resource "aws_instance" "instances" { } resource "aws_ebs_volume" "volumes" { - for_each = module.design.volumes + for_each = { + for x, values in module.design.volumes : x => values if lookup(values, "managed", true) + } availability_zone = local.availability_zone size = each.value.size type = lookup(each.value, "type", null) @@ -140,6 +142,16 @@ resource "aws_ebs_volume" "volumes" { Name = "${var.cluster_name}-${each.key}" } } +data "aws_ebs_volume" "existing_volumes" { + for_each = { + for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true) + } + + filter { + name = "tag:Name" + values = ["${var.cluster_name}-${each.key}"] + } +} locals { device_names = [ @@ -151,7 +163,7 @@ locals { resource "aws_volume_attachment" "attachments" { for_each = module.design.volumes device_name = local.device_names[index(module.design.volume_per_instance[each.value.instance], replace(each.key, "${each.value.instance}-", ""))] - volume_id = aws_ebs_volume.volumes[each.key].id + volume_id = try(aws_ebs_volume.volumes[each.key].id, aws_ebs_volume.existing_volumes[each.key].id) instance_id = aws_instance.instances[each.value.instance].id skip_destroy = true } @@ -175,7 +187,7 @@ locals { pv_key => { for name, specs in pv_values: name => merge( - { glob = "/dev/disk/by-id/*${replace(aws_ebs_volume.volumes["${x}-${pv_key}-${name}"].id, "-", "")}" }, + { glob = try("/dev/disk/by-id/*${replace(aws_ebs_volume.volumes["${x}-${pv_key}-${name}"].id, "-", "")}", "/dev/disk/by-id/*${replace(aws_ebs_volume.existing_volumes["${x}-${pv_key}-${name}"].id, "-", "")}") }, specs, ) } if contains(values.tags, pv_key) diff --git a/azure/infrastructure.tf b/azure/infrastructure.tf index 0d1f9ff1..ee30db56 100644 --- a/azure/infrastructure.tf +++ b/azure/infrastructure.tf @@ -128,7 +128,9 @@ resource "azurerm_linux_virtual_machine" "instances" { } resource "azurerm_managed_disk" "volumes" { - for_each = module.design.volumes + for_each = { + for x, values in module.design.volumes : x => values if lookup(values, "managed", true) + } name = format("%s-%s", var.cluster_name, each.key) location = var.location resource_group_name = local.resource_group_name @@ -136,10 +138,17 @@ resource "azurerm_managed_disk" "volumes" { create_option = "Empty" disk_size_gb = each.value.size } +data "azurerm_managed_disk" "existing_volumes" { + for_each = { + for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true) + } + name = format("%s-%s", var.cluster_name, each.key) + resource_group_name = local.resource_group_name +} resource "azurerm_virtual_machine_data_disk_attachment" "attachments" { for_each = module.design.volumes - managed_disk_id = azurerm_managed_disk.volumes[each.key].id + managed_disk_id = try(azurerm_managed_disk.volumes[each.key].id, azurerm_managed_disk.existing_volumes[each.key].id) virtual_machine_id = azurerm_linux_virtual_machine.instances[each.value.instance].id lun = index(module.design.volume_per_instance[each.value.instance], replace(each.key, "${each.value.instance}-", "")) caching = "ReadWrite" diff --git a/gcp/infrastructure.tf b/gcp/infrastructure.tf index 8dc7f9d5..07efbc0f 100644 --- a/gcp/infrastructure.tf +++ b/gcp/infrastructure.tf @@ -143,17 +143,25 @@ resource "google_compute_instance" "instances" { } resource "google_compute_disk" "volumes" { - for_each = module.design.volumes + for_each = { + for x, values in module.design.volumes : x => values if lookup(values, "managed", true) + } name = "${var.cluster_name}-${each.key}" type = lookup(each.value, "type", "pd-standard") zone = local.zone size = each.value.size } +data "google_compute_disk" "existing_volumes" { + for_each = { + for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true) + } + name = "${var.cluster_name}-${each.key}" +} resource "google_compute_attached_disk" "attachments" { for_each = module.design.volumes - disk = google_compute_disk.volumes[each.key].self_link - device_name = google_compute_disk.volumes[each.key].name + disk = try(google_compute_disk.volumes[each.key].self_link, google_compute_disk.existing_volumes[each.key].self_link) + device_name = try(google_compute_disk.volumes[each.key].name, google_compute_disk.existing_volumes[each.key].name) mode = "READ_WRITE" instance = google_compute_instance.instances[each.value.instance].self_link }