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:
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user