首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数不能为空,在动态块中。

参数不能为空,在动态块中。
EN

Stack Overflow用户
提问于 2022-07-07 20:17:26
回答 1查看 165关注 0票数 1

我正在使用kubernetes_network_policy资源。我有大约十个network_poilicy,每个都是不同的。一项政策只允许进入,另一项政策只有白鹭,很少有政策同时拥有入口和出口。当我在动态块中有一个空值时,我会在错误下面,有什么方法可以克服这个错误。就像只在变量(ingress_number)有值的情况下执行动态块一样?

代码语言:javascript
复制
 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.

我的资源

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
  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"]
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-08 00:25:51

是的,你应该可以在你的for_each上设定条件

代码语言:javascript
复制
for_each = each.value.a == null ? [] : range(length(each.value.a))

或者更短一些,但更粗俗:

代码语言:javascript
复制
for_each = try(range(length(each.value.a)), [])

你也可以做其他的检查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72903749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档