avoid duplicated PVC value: only one pvc of the same namespace/name will be created but multiple can be reused

This commit is contained in:
2025-11-24 21:01:23 +00:00
parent ad69271f01
commit b778662fa5
+19 -1
View File
@@ -22,6 +22,7 @@ variable "kubernetes" {
annotations_fileType = optional(string) annotations_fileType = optional(string)
existing_pvc = optional(bool, false) existing_pvc = optional(bool, false)
mount_path = string mount_path = string
sub_path = optional(string)
}))) })))
environment = optional(map(object({ environment = optional(map(object({
name = optional(string) name = optional(string)
@@ -65,9 +66,10 @@ variable "kubernetes" {
} }
locals { locals {
pvc_details = flatten([ pvc_list = flatten([
for key, value in var.kubernetes : [ for key, value in var.kubernetes : [
for index, pvc in value.pvc : { for index, pvc in value.pvc : {
key = "${coalesce(value.namespace, value.connected_namespace, key)}/${pvc.name}"
index = index index = index
pvc = pvc.pvc pvc = pvc.pvc
name = pvc.name name = pvc.name
@@ -76,9 +78,24 @@ locals {
storage_class_name = pvc.storage_class_name storage_class_name = pvc.storage_class_name
annotations_fileType = pvc.annotations_fileType annotations_fileType = pvc.annotations_fileType
existing_pvc = pvc.existing_pvc existing_pvc = pvc.existing_pvc
sub_path = pvc.sub_path
namespace = coalesce(value.namespace, value.connected_namespace, key) namespace = coalesce(value.namespace, value.connected_namespace, key)
}] if value.enabled == true && value.pvc != null }] if value.enabled == true && value.pvc != null
]) ])
# group duplicates instead of throwing an error
pvc_map_grouped = {
for p in local.pvc_list :
p.key => p...
}
# keep only one PVC per key (take the first)
pvc_map = {
for k, list in local.pvc_map_grouped :
k => list[0]
}
pvc_details = values(local.pvc_map)
} }
locals { locals {
@@ -332,6 +349,7 @@ resource "kubernetes_deployment_v1" "automatic" {
content { content {
mount_path = volume_mount.value.mount_path mount_path = volume_mount.value.mount_path
name = "${volume_mount.value.name}-pvc" name = "${volume_mount.value.name}-pvc"
sub_path = volume_mount.value.sub_path != null ? volume_mount.value.sub_path : ""
} }
} }