我有这个资源和这个带有这个变量的动态块:
variable "services" {
description = "Map of the Services"
type = map(object({
port = number
uri = optional(string)
}))
}
services = {
"auth-service" = {
port = 3050
uri = "example1"
}
"clone-service" = {
port = 3040
}
}
resource "kubernetes_ingress_v1" "eks_global_ingress" {
[...]
dynamic "rule" {
# I make this expression to bypass the services that doesn't have URI.
for_each = { for k, v in var.services : k => v if v["uri"] != null }
content {
http {
path {
path = "/${rule.value["uri"]}/*"
backend {
service {
name = "${rule.key}-i-svc"
port {
number = rule.value["port"]
}
}
}
}
}
}
}
此时一切正常,但现在我想为同一个服务添加另一个 URI。所以我在 URI 中做了一个列表,如下所示:
services = {
"auth-service" = {
port = 3050
uri = ["example1","example2]
"clone-service" = {
port = 3040
但是有了这个我不能在动态块中包含另一个 for_each 。我如何过滤空 URI 并同时将代码与 2 个或更多 URI 作为列表循环?
我希望该规则被重复“x”次,超过列表中存在的 URI 数量,并且如果地图上不存在 URI 值(null),则“for_each”会绕过此规则。
dynamic "rule" {
# I make this expression to bypass the services that doesn't have URI.
for_each = { for k, v in var.services : k => v if length(v["uri"]) > 0 }
content {
http {
dynamic "path" {
for_each = toset(rule.value["uri"])
content {
path = "/${path.key}/*"
backend {
service {
name = "${rule.key}-i-svc"
port {
number = rule.value["port"]
}
}
}
}
}
}
}
}
注意:不要忘记将 uri 类型从字符串更改为列表(字符串)