我正在使用kubernetes_network_policy资源。我有大约十个network_poilicy,每个都是不同的。一项政策只允许进入,另一项政策只有白鹭,很少有政策同时拥有入口和出口。当我在动态块中有一个空值时,我会在错误下面,有什么方法可以克服这个错误。就像只在变量(ingress_number)有值的情况下执行动态块一样?
Error: Invalid function argument
│
│ on main.tf line 16, in resource "kubernetes_network_policy" "example-policy":
│ 16: for_each = range(length(each.value.ingress_number))
│ ├────────────────
│ │ each.value.ingress_number is null
│
│ Invalid value for "value" parameter: argument must not be null.我的资源
resource "kubernetes_network_policy" "example-policy" {
for_each = var.inputs
metadata {
name = each.value.name
namespace = each.value.namespace
}
spec {
pod_selector {
match_labels = {
app = each.value.selector
}
}
policy_types = each.value.policy
dynamic "ingress" {
for_each = range(length(each.value.ingress_number))
content {
ports {
port = each.value.ingress_number[ingress.value]
protocol = each.value.ingress_protocol[ingress.value]
}
to {
namespace_selector {
match_labels = {
app = each.value.ingress_label
}
}
}
}
}
dynamic "egress" {
for_each = range(length(each.value.egress_number))
content {
ports {
port = each.value.egress_number[egress.value]
protocol = each.value.egress_protocol[egress.value]
}
to {
namespace_selector {
match_labels = {
app = each.value.egress_label
}
}
}
}
}
}
}My varibale.tf
variable "inputs" {
type = map(object({
name = string
namespace = string
selector = string
policy = list(string)
ingress_number = optional(list(string))
ingress_protocol = optional(list(string))
ingress_label = optional(string)
egress_number = optional(list(string))
egress_protocol = optional(list(string))
egress_label = optional(string)
}))
default = {}
}我的tfvars
inputs = {
app1 = {
name = "apache"
namespace = "default"
selector = "apache-app"
policy = ["ingrees", "Egress"]
egress_label = "example"
egress_number = ["443", "8080"]
egress_protocol = ["TCP", "TCP"]
}
app2 = {
name = "nignx"
namespace = "default"
selector = "nignix-app"
policy = ["ingrees", "Egress"]
ingress_label = "play"
ingress_number = ["9080", "9320"]
ingress_protocol = ["TCP", "TCP"]
egress_label = "example"
egress_number = ["443", "8080"]
egress_protocol = ["TCP", "TCP"]
}
}发布于 2022-07-08 00:25:51
是的,你应该可以在你的for_each上设定条件
for_each = each.value.a == null ? [] : range(length(each.value.a))或者更短一些,但更粗俗:
for_each = try(range(length(each.value.a)), [])你也可以做其他的检查。
https://stackoverflow.com/questions/72903749
复制相似问题