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
This commit is contained in:
@@ -19,6 +19,17 @@ variable "kubernetes" {
|
|||||||
pvc_size = string
|
pvc_size = string
|
||||||
mount_path = 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)
|
replicas = optional(number, 1)
|
||||||
namespace = optional(string)
|
namespace = optional(string)
|
||||||
service_port = optional(number)
|
service_port = optional(number)
|
||||||
@@ -51,8 +62,23 @@ locals {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
output "pvc_test" {
|
locals {
|
||||||
value = { for key, value in local.pvc_details : key => value }
|
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" {
|
resource "kubernetes_namespace" "automatic" {
|
||||||
@@ -70,7 +96,7 @@ resource "kubernetes_namespace" "automatic" {
|
|||||||
|
|
||||||
|
|
||||||
resource "kubernetes_persistent_volume_claim" "automatic-pvc" {
|
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 {
|
metadata {
|
||||||
namespace = each.value.namespace
|
namespace = each.value.namespace
|
||||||
name = "${each.value.name}-pvc"
|
name = "${each.value.name}-pvc"
|
||||||
@@ -169,10 +195,26 @@ resource "kubernetes_deployment_v1" "automatic" {
|
|||||||
|
|
||||||
|
|
||||||
dynamic "env" {
|
dynamic "env" {
|
||||||
for_each = each.value.environment_variables != null ? each.value.environment_variables : {}
|
for_each = each.value.environment != null ? each.value.environment : {}
|
||||||
content {
|
content {
|
||||||
name = env.key
|
name = env.key
|
||||||
value = env.value
|
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 } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-8
@@ -31,23 +31,34 @@ kubernetes = {
|
|||||||
requests-cpu = "25m"
|
requests-cpu = "25m"
|
||||||
requests-memory = "25Mi"
|
requests-memory = "25Mi"
|
||||||
}
|
}
|
||||||
environment_variables = { PORT = 80 }
|
# environment_variables = { PORT = 80, test = 22 }
|
||||||
pvc = {
|
environment = {
|
||||||
first = {
|
MY_VAR = {
|
||||||
name = "onitama"
|
name = "MY_VAR"
|
||||||
pvc_size = "1Mi"
|
value_from = {
|
||||||
mount_path = "/code"
|
secret_key_ref = {
|
||||||
|
name = "my_secret"
|
||||||
|
key = "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
second = {
|
}
|
||||||
|
pvc = {
|
||||||
|
onitamadeux = {
|
||||||
name = "onitamadeux"
|
name = "onitamadeux"
|
||||||
pvc_size = "2Mi"
|
pvc_size = "2Mi"
|
||||||
mount_path = "/deuxcode"
|
mount_path = "/deuxcode"
|
||||||
}
|
}
|
||||||
|
onitama = {
|
||||||
|
name = "onitama"
|
||||||
|
pvc_size = "1Mi"
|
||||||
|
mount_path = "/code"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nginx = {
|
nginx = {
|
||||||
enabled = true
|
enabled = false
|
||||||
namespace = "testnginx"
|
namespace = "testnginx"
|
||||||
#image = "localhost:31795/django-python"
|
#image = "localhost:31795/django-python"
|
||||||
image = "nginx:latest"
|
image = "nginx:latest"
|
||||||
|
|||||||
Reference in New Issue
Block a user