Hello, I am in the middle of building a module for our kubernetes deployments, We are in the process of going from many modules to one common module with dynamic resources/blocks etc.
Here in lies my problem, I have been using the experimental optional types in my variable declaration for example
Terraform Version: v1.2.6
variable "ports" {
description = "list of ports to expose from the container"
type = list(object({
name = string
container_port = number
is_ingress = optional(object({
ingress_class = string
domain_name = string
domain_path = string
tls_cluster_issuer = string
enforce_https = bool
proxy_body_size = string
}))
}))
default = null
}
You can see from the following that I am defining a list of objects where isIngress is an optional additional object, I have everything setup dynamically except for the ingress resource itself what I am attempting to do is use the for_each in the ingress resource to dynamically create an ingress resource for each isIngress != null in this list of objects
resource "kubernetes_ingress_v1" "app" {
depends_on = [kubernetes_service.app]
for_each = { for p in var.ports : p.name => p }
metadata {
name = var.name
namespace = var.namespace
annotations = {
"kubernetes.io/ingress.class" = each.value.is_ingress.ingress_class
"cert-manager.io/cluster-issuer" = each.value.is_ingress.tls_cluster_issuer
"nginx.ingress.kubernetes.io/ssl-redirect" = each.value.is_ingress.enforce_https
"nginx.ingress.kubernetes.io/proxy-body-size" = each.value.is_ingress.proxy_body_size
}
}
spec {
tls {
hosts = [each.value.is_ingress.domain_name]
secret_name = "${var.name}-tls-cert"
}
rule {
host = each.value.is_ingress.domain_name
http {
path {
path = each.value.is_ingress.domain_path
backend {
service {
name = kubernetes_service.app[0].metadata.0.name
port {
name = each.value.name
}
}
}
}
}
}
}
}
So the result when I would use this in a module would be something like this where app-port(8080) & app-port-2(8081) would both create seperate ingress resources and app-port-3(8082) would not.
module "deployment_test" {
source = "../../../modules/goeasycare/terraform-k8s-deployment-template"
namespace = "default"
image_url = "nginx"
image_tag = "latest"
name = "nginx"
ports = [
{
name = "app-port"
container_port = 8080
is_ingress = {
ingress_class = "INGRESS_CLASS"
domain_name = "DOMAIN_NAME"
domain_path = "/"
tls_cluster_issuer = "TLS_CLUSTER_ISSUER"
enforce_https = true
proxy_body_size = "PROXY_BODY_SIZE"
}
},
{
name = "app-port-2"
container_port = 8081
is_ingress = {
ingress_class = "INGRESS_CLASS"
domain_name = "DOMAIN_NAME"
domain_path = "/api"
tls_cluster_issuer = "TLS_CLUSTER_ISSUER"
enforce_https = true
proxy_body_size = "PROXY_BODY_SIZE"
}
},
{
name = "app-port-3"
container_port = 8082
},
]
}