variable "kubernetes_input" { type = map(object({ name = optional(string) docker = optional(bool) namespace = optional(string) connected_namespace = optional(string, "") service_port = optional(list(number)) service_type = optional(string, "ClusterIP") domain_name = optional(list(string)) legacy_domain = optional(bool, false) exposed = optional(bool, true) enabled = optional(bool, true) })) } resource "kubernetes_ingress_v1" "automatic_ingress" { # Keep your top-level filter for filtering active services for_each = { for key, value in var.kubernetes_input : key => value if value.service_port != null && value.enabled == true && value.docker != true && value.exposed == true } metadata { name = "${each.key}-ingress" namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) annotations = { "cert-manager.io/cluster-issuer" = "letsencrypt-prod" "traefik.ingress.kubernetes.io/router.entrypoints" = "websecure" } } spec { ingress_class_name = "traefik" # --- DYNAMIC TLS BLOCK --- # Generates a separate TLS block + secret per domain entry dynamic "tls" { for_each = toset(coalesce(each.value.domain_name, [each.key])) content { hosts = [join(".", [tls.value, "geeraert.eu"])] secret_name = "${each.key}-${tls.value}-tls-secret" } } # --- DYNAMIC RULE BLOCK --- # Loops through the domain list and creates an HTTP path routing rule for each dynamic "rule" { for_each = toset(coalesce(each.value.domain_name, [each.key])) content { host = join(".", [rule.value, "geeraert.eu"]) http { path { path = "/" backend { service { name = "${each.key}-svc" port { number = each.value.service_port[0] } } } } } } } } } resource "kubernetes_ingress_v1" "automatic_ingress_legacy" { for_each = { for key, value in var.kubernetes_input : key => value if value.service_port != null && value.enabled == true && value.docker != true && value.exposed == true && value.legacy_domain == true } metadata { name = "${each.key}-ingress" namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) annotations = { "cert-manager.io/cluster-issuer" = "letsencrypt-prod" "traefik.ingress.kubernetes.io/router.entrypoints" = "websecure" } } spec { ingress_class_name = "traefik" # --- DYNAMIC TLS BLOCK --- # Generates a separate TLS block + secret per domain entry dynamic "tls" { for_each = toset(coalesce(each.value.domain_name, [each.key])) content { hosts = [join(".", [tls.value, "geeraert.eu"])] secret_name = "${each.key}-${tls.value}-tls-secret" } } # --- DYNAMIC RULE BLOCK --- # Loops through the domain list and creates an HTTP path routing rule for each dynamic "rule" { for_each = toset(coalesce(each.value.domain_name, [each.key])) content { host = join(".", [rule.value, "geeraert.eu"]) http { path { path = "/" backend { service { name = "${each.key}-svc" port { number = each.value.service_port[0] } } } } } } } } }