kubernetes 地形中的条件标记

pftdvrlh  于 2023-01-20  发布在  Kubernetes
关注(0)|答案(2)|浏览(104)

我有一个在EKS中创建节点的资源任务。当我试图根据特定变量的值定义一些标记时,我的问题就开始了,如果没有,就不要声明标记。类似于:

resource "aws_eks_node_group" "managed_workers" {
  for_each = var.nodegroups[terraform.workspace]

  cluster_name    = aws_eks_cluster.cluster.name
  node_group_name = each.value.Name
  node_role_arn   = aws_iam_role.managed_workers.arn
  subnet_ids      = aws_subnet.private.*.id

  tags = merge(
      var.tags[terraform.workspace], {
      
    if somevar = value then
      "topology.kubernetes.io/zone" = "us-east-2a"  <--- THIS TAG IS CONDITIONAL
    fi
      "type" = each.value.type
      "Name" = each.value.Name
      "ob"   = each.value.ob
      "platform" = each.value.platform
  })

有可能吗?

ztigrdn8

ztigrdn81#

是的,您可以这样做。例如:

resource "aws_eks_node_group" "managed_workers" {
  for_each = var.nodegroups[terraform.workspace]

  cluster_name    = aws_eks_cluster.cluster.name
  node_group_name = each.value.Name
  node_role_arn   = aws_iam_role.managed_workers.arn
  subnet_ids      = aws_subnet.private.*.id

  tags = merge(
      var.tags[terraform.workspace], {      
      "type" = each.value.type
      "Name" = each.value.Name
      "ob"   = each.value.ob
      "platform" = each.value.platform},
      [somevar == value  ? {"topology.kubernetes.io/zone" = "us-east-2a"} : null]...)
}

...用于扩展函数参数。

a11xaf1n

a11xaf1n2#

下面是一个不使用...操作符的例子,它在terraform v1.3.1中工作:

resource "aws_eks_node_group" "managed_workers" {
  for_each = var.nodegroups[terraform.workspace]

  cluster_name    = aws_eks_cluster.cluster.name
  node_group_name = each.value.Name
  node_role_arn   = aws_iam_role.managed_workers.arn
  subnet_ids      = aws_subnet.private.*.id

  tags = merge(
      var.tags[terraform.workspace], 
      {      
        "type" = each.value.type
        "Name" = each.value.Name
        "ob"   = each.value.ob
        "platform" = each.value.platform
      },
      var.somevar != "value" ? {} :
      {
        "topology.kubernetes.io/zone" = "us-east-2a"
      },
  )
}

相关问题