diff --git a/infrastructure/deployment.tf b/infrastructure/deployment.tf index 872128f..584c571 100644 --- a/infrastructure/deployment.tf +++ b/infrastructure/deployment.tf @@ -35,6 +35,7 @@ resource "kubernetes_service_v1" "automatic_service" { } + depends_on = [resource.kubernetes_namespace_v1.automatic] } diff --git a/infrastructure/module.tf b/infrastructure/module.tf index 8926b28..3f5b82e 100644 --- a/infrastructure/module.tf +++ b/infrastructure/module.tf @@ -80,6 +80,14 @@ variable "kubernetes" { service_account_name = optional(string) environment_variables = optional(map(string), { name = "value" }) security_context = optional(bool, false) + import_database_secret = optional(object({ + #putting them a non optional as the code doesn't allow optional value + host = string + name = string + password = string + port = string + username = string + })) })) } diff --git a/infrastructure/secrets.tf b/infrastructure/secrets.tf index af6a015..247ec71 100644 --- a/infrastructure/secrets.tf +++ b/infrastructure/secrets.tf @@ -155,23 +155,44 @@ resource "bitwarden-secrets_secret" "automatic_bitwarden_secret_db_name" { ] } } + +# 1. Move the loop logic to a local variable first +locals { + database_secrets_to_import = { + for key, value in var.kubernetes : key => value + if value.database != null && value.enabled == true && try(value.import_database_secret, "") != "" + } +} + +# 2. Reference the local map in your import block using each.key and each.value import { - to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_host["django-testing"] - id = "097f10de-63ee-405c-b46a-b41001781e07" + for_each = local.database_secrets_to_import + + to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_host[each.key] + id = each.value.import_database_secret.host +} + +import { + for_each = local.database_secrets_to_import + + to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_name[each.key] + id = each.value.import_database_secret.name } import { - to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_name["django-testing"] - id = "20530a85-e446-4d79-8985-b41001781e07" + for_each = local.database_secrets_to_import + + to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_password[each.key] + id = each.value.import_database_secret.password } import { - to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_password["django-testing"] - id = "a3385f49-ba92-4478-b6f1-b4e100ff01fb" + for_each = local.database_secrets_to_import + + to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_port[each.key] + id = each.value.import_database_secret.port } import { - to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_port["django-testing"] - id = "e15e00eb-db1e-47ea-ab0e-b41001781e09" -} -import { - to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_username["django-testing"] - id = "3c67d310-7113-45b7-8f3f-b41001781e08" + for_each = local.database_secrets_to_import + + to = bitwarden-secrets_secret.automatic_bitwarden_secret_db_username[each.key] + id = each.value.import_database_secret.username } diff --git a/infrastructure/variables.auto.tfvars b/infrastructure/variables.auto.tfvars index 46a49b4..e219840 100644 --- a/infrastructure/variables.auto.tfvars +++ b/infrastructure/variables.auto.tfvars @@ -173,7 +173,7 @@ kubernetes = { } django-testing = { - enabled = false + #enabled = false image = "gitea.tryton.geeraert.eu/pi-ux-ce/django-testing:latest" service_port = [8000] database = {} @@ -184,7 +184,7 @@ kubernetes = { requests-cpu = "10m" requests-memory = "300Mi" } - extra_manifest = ["/opt/kubernetes/Terraform_kube/external_manifests/django/kubeconfig-testing.secret.yaml"] + extra_manifest = ["../external_manifests/django/kubeconfig-testing.secret.yaml"] secret_ref = ["django-bitwarden-credentials"] secret_import = ["68dce7aa-8e5f-4483-9083-b474016e34fc"] environment = { @@ -212,6 +212,15 @@ kubernetes = { } } + import_database_secret = { + host = "097f10de-63ee-405c-b46a-b41001781e07" + name = "20530a85-e446-4d79-8985-b41001781e07" + password = "a3385f49-ba92-4478-b6f1-b4e100ff01fb" + port = "e15e00eb-db1e-47ea-ab0e-b41001781e09" + username = "3c67d310-7113-45b7-8f3f-b41001781e08" + + } + }