loop and add index so the pvc ressource is indexed with the name instead of a random number DOC : https://stackoverflow.com/questions/76941399/terraform-flatten-generate-new-index

This commit is contained in:
2024-11-18 15:51:31 +00:00
parent 5e66177351
commit d2943fff6d
+12 -3
View File
@@ -41,7 +41,8 @@ variable "kubernetes" {
locals { locals {
pvc_details = flatten([ pvc_details = flatten([
for key, value in var.kubernetes : [ for key, value in var.kubernetes : [
for pvc in value.pvc : { for index, pvc in value.pvc : {
index = index
name = pvc.name name = pvc.name
pvc_size = pvc.pvc_size pvc_size = pvc.pvc_size
mount_path = pvc.mount_path mount_path = pvc.mount_path
@@ -60,17 +61,22 @@ resource "kubernetes_namespace" "automatic" {
annotations = { annotations = {
name = coalesce(each.value.namespace, each.key) name = coalesce(each.value.namespace, each.key)
} }
labels = {
deployment_method = "Terraform"
}
name = coalesce(each.value.namespace, each.key) name = coalesce(each.value.namespace, each.key)
} }
} }
resource "kubernetes_persistent_volume_claim" "automatic-pvc" { resource "kubernetes_persistent_volume_claim" "automatic-pvc" {
for_each = { for key, value in local.pvc_details : key => value } for_each = { for key, value in local.pvc_details : "${value.index}" => merge(value, {mew_key = value.index}) }
metadata { metadata {
namespace = each.value.namespace namespace = each.value.namespace
name = "${each.value.name}-pvc" name = "${each.value.name}-pvc"
labels = {
deployment_method = "Terraform"
}
} }
spec { spec {
access_modes = ["ReadWriteMany"] access_modes = ["ReadWriteMany"]
@@ -89,6 +95,9 @@ resource "kubernetes_service" "automatic_service" {
metadata { metadata {
name = "${coalesce(each.value.namespace, each.key)}-svc" name = "${coalesce(each.value.namespace, each.key)}-svc"
namespace = coalesce(each.value.namespace, each.key) namespace = coalesce(each.value.namespace, each.key)
labels = {
deployment_method = "Terraform"
}
} }
spec { spec {
selector = { selector = {