From 0493b4b3b4f7c6cf6452dcb6cfbe96fe47ebf422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Niesiob=C4=99dzki?= Date: Sat, 16 Nov 2024 10:18:49 +0000 Subject: [PATCH] Fix non-empty plan after mixing CloudSQL with other mounts --- modules/cloud-run-v2/job.tf | 33 ++++++++++++++++++++++++--------- modules/cloud-run-v2/service.tf | 32 ++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/modules/cloud-run-v2/job.tf b/modules/cloud-run-v2/job.tf index cc97a4315e..74b170408b 100644 --- a/modules/cloud-run-v2/job.tf +++ b/modules/cloud-run-v2/job.tf @@ -87,7 +87,15 @@ resource "google_cloud_run_v2_job" "job" { } } dynamic "volume_mounts" { - for_each = coalesce(containers.value.volume_mounts, tomap({})) + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k != "cloudsql" } + content { + name = volume_mounts.key + mount_path = volume_mounts.value + } + } + # CloudSQL is the last mount in the list returned by API + dynamic "volume_mounts" { + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k == "cloudsql" } content { name = volume_mounts.key mount_path = volume_mounts.value @@ -96,7 +104,7 @@ resource "google_cloud_run_v2_job" "job" { } } dynamic "volumes" { - for_each = var.volumes + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances == null } content { name = volumes.key dynamic "secret" { @@ -114,12 +122,7 @@ resource "google_cloud_run_v2_job" "job" { } } } - dynamic "cloud_sql_instance" { - for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] - content { - instances = volumes.value.cloud_sql_instances - } - } + dynamic "empty_dir" { for_each = volumes.value.empty_dir_size == null ? [] : [""] content { @@ -144,6 +147,19 @@ resource "google_cloud_run_v2_job" "job" { } } } + # CloudSQL is the last volume in the list returned by API + dynamic "volumes" { + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances != null } + content { + name = volumes.key + dynamic "cloud_sql_instance" { + for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] + content { + instances = volumes.value.cloud_sql_instances + } + } + } + } } } @@ -162,4 +178,3 @@ resource "google_cloud_run_v2_job_iam_binding" "binding" { role = each.key members = each.value } - diff --git a/modules/cloud-run-v2/service.tf b/modules/cloud-run-v2/service.tf index 0906f24a55..0e35be1707 100644 --- a/modules/cloud-run-v2/service.tf +++ b/modules/cloud-run-v2/service.tf @@ -101,7 +101,15 @@ resource "google_cloud_run_v2_service" "service" { } } dynamic "volume_mounts" { - for_each = coalesce(containers.value.volume_mounts, tomap({})) + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k != "cloudsql" } + content { + name = volume_mounts.key + mount_path = volume_mounts.value + } + } + # CloudSQL is the last mount in the list returned by API + dynamic "volume_mounts" { + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k == "cloudsql" } content { name = volume_mounts.key mount_path = volume_mounts.value @@ -174,7 +182,7 @@ resource "google_cloud_run_v2_service" "service" { } } dynamic "volumes" { - for_each = var.volumes + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances == null } content { name = volumes.key dynamic "secret" { @@ -192,12 +200,7 @@ resource "google_cloud_run_v2_service" "service" { } } } - dynamic "cloud_sql_instance" { - for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] - content { - instances = volumes.value.cloud_sql_instances - } - } + dynamic "empty_dir" { for_each = volumes.value.empty_dir_size == null ? [] : [""] content { @@ -222,6 +225,19 @@ resource "google_cloud_run_v2_service" "service" { } } } + # CloudSQL is the last volume in the list returned by API + dynamic "volumes" { + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances != null } + content { + name = volumes.key + dynamic "cloud_sql_instance" { + for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] + content { + instances = volumes.value.cloud_sql_instances + } + } + } + } } deletion_protection = var.deletion_protection