From 8890d057cb160a392db403d35d3e67fc0b257ea2 Mon Sep 17 00:00:00 2001 From: pierre Date: Mon, 18 Nov 2024 17:08:23 +0000 Subject: [PATCH] loop through env variables to get the secret DOC : https://stackoverflow.com/questions/69945533/creating-a-dynamic-secret-variable-block-within-terraform-for-cloud-run --- module.tf | 54 ++++++++++++++++++++++++++++++++++++++----- variables.auto.tfvars | 27 +++++++++++++++------- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/module.tf b/module.tf index 1929741..70fc6d2 100644 --- a/module.tf +++ b/module.tf @@ -19,6 +19,17 @@ variable "kubernetes" { pvc_size = string mount_path = string }))) + environment = optional(map(object({ + name = string + value = optional(string) + value_from = optional(object({ + secret_key_ref = object({ + name = string + key = string + }) + })) + # secret_name = optional(string) + }))) replicas = optional(number, 1) namespace = optional(string) service_port = optional(number) @@ -51,8 +62,23 @@ locals { ]) } -output "pvc_test" { - value = { for key, value in local.pvc_details : key => value } +locals { + env_details = flatten([ + for key, value in var.kubernetes : [ + for index, env in value.environment_variables : { + index = index + name = env + }] if value.enabled == true && value.environment_variables != null + ]) +} + +#output "pvc_test" { +# value = { for key, value in local.pvc_details : key => value } +#} + +output "env_test" { + value = { for key, value in local.env_details : key => value } + } resource "kubernetes_namespace" "automatic" { @@ -70,7 +96,7 @@ resource "kubernetes_namespace" "automatic" { resource "kubernetes_persistent_volume_claim" "automatic-pvc" { - for_each = { for key, value in local.pvc_details : "${value.index}" => merge(value, {mew_key = value.index}) } + 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" @@ -169,10 +195,26 @@ resource "kubernetes_deployment_v1" "automatic" { dynamic "env" { - for_each = each.value.environment_variables != null ? each.value.environment_variables : {} + for_each = each.value.environment != null ? each.value.environment : {} content { - name = env.key - value = env.value + name = env.key + dynamic "value_from" { + for_each = env.value.value_from != null ? env.value.value_from : {} + content { + dynamic "secret_key_ref" { + for_each = [1] + content { + name = env.value.value_from.secret_key_ref.name + key = env.value.value_from.secret_key_ref.key + } + } + } + } + + # value = env.value + # value_from = { + # secret_key_ref = { + # name = test } } } } diff --git a/variables.auto.tfvars b/variables.auto.tfvars index e6ab502..19e18da 100644 --- a/variables.auto.tfvars +++ b/variables.auto.tfvars @@ -31,23 +31,34 @@ kubernetes = { requests-cpu = "25m" requests-memory = "25Mi" } - environment_variables = { PORT = 80 } - pvc = { - first = { - name = "onitama" - pvc_size = "1Mi" - mount_path = "/code" + # environment_variables = { PORT = 80, test = 22 } + environment = { + MY_VAR = { + name = "MY_VAR" + value_from = { + secret_key_ref = { + name = "my_secret" + key = "latest" + } + } } - second = { + } + pvc = { + onitamadeux = { name = "onitamadeux" pvc_size = "2Mi" mount_path = "/deuxcode" } + onitama = { + name = "onitama" + pvc_size = "1Mi" + mount_path = "/code" + } } } nginx = { - enabled = true + enabled = false namespace = "testnginx" #image = "localhost:31795/django-python" image = "nginx:latest"