From d2943fff6daa8bf53fcd06a3acc7a1b1f460e429 Mon Sep 17 00:00:00 2001 From: pierre Date: Mon, 18 Nov 2024 15:51:31 +0000 Subject: [PATCH] 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 --- module.tf | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/module.tf b/module.tf index 3934b57..1929741 100644 --- a/module.tf +++ b/module.tf @@ -41,7 +41,8 @@ variable "kubernetes" { locals { pvc_details = flatten([ for key, value in var.kubernetes : [ - for pvc in value.pvc : { + for index, pvc in value.pvc : { + index = index name = pvc.name pvc_size = pvc.pvc_size mount_path = pvc.mount_path @@ -60,17 +61,22 @@ resource "kubernetes_namespace" "automatic" { annotations = { name = coalesce(each.value.namespace, each.key) } + labels = { + deployment_method = "Terraform" + } name = coalesce(each.value.namespace, each.key) } } 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 { namespace = each.value.namespace name = "${each.value.name}-pvc" + labels = { + deployment_method = "Terraform" + } } spec { access_modes = ["ReadWriteMany"] @@ -89,6 +95,9 @@ resource "kubernetes_service" "automatic_service" { metadata { name = "${coalesce(each.value.namespace, each.key)}-svc" namespace = coalesce(each.value.namespace, each.key) + labels = { + deployment_method = "Terraform" + } } spec { selector = {