kubernetes 资源定义中的Terraform if语句

mpbci0fu  于 2023-06-21  发布在  Kubernetes
关注(0)|答案(1)|浏览(127)

我正在使用helm_release terraform模块将应用程序部署到我的kubernetes集群。我试图获取存储在AWS SSM中的单个值,以将其传递到Helm Chart值文件中。此特定值仅适用于一个舵图。我不想为所有helm图表检索或传递此值,所以如果chart_name变量等于我需要设置值的图表,我将尝试在helm_release资源中设置if语句。例如,我想做这样的事情:

resource "helm_release" "example" {
  name  = var.chart_name
  repository = var.repository
  chart      = var.chart_name
  namespace = var.namespace
  create_namespace = var.create_namespace
  verify = false
  timeout = var.timeout
  repository_username = var.repo_username
  repository_password = var.repo_token
  
  # custom value from SSM would be set here. This is where I would like to perform the conditional check:
  # i.e. 
  # if var.chart_name = "some-chart":
  set_sensitive = [
    {
      path  = "firstEndpoints"
      value = data.aws_ssm_parameter.first_endpoints
    },
    {
      path  = "secondEndpoints"
      value = data.aws_ssm_parameter.second_endpoints
    },
  ]

  # dynamic values for all charts are passed in through terragrunt inputs (not super relevant here)
  dynamic "set" {
    for_each = var.dynamic_values
    
    content {
      name = set.value.name
      value = set.value.value
    }
  }
}

长话短说,有没有可能在一个terraform资源中定义条件,而不是在整个资源上定义条件?

uttx8gqw

uttx8gqw1#

您可以使用具有预期语法的三进制在资源参数中指定条件逻辑:

set_sensitive = var.chart_name == "some-chart" ? [
  {
    path  = "firstEndpoints"
    value = data.aws_ssm_parameter.first_endpoints
  },
  {
    path  = "secondEndpoints"
    value = data.aws_ssm_parameter.second_endpoints
  },
] : null

正如预期的那样,这将为指定的图表名称分配自定义的list(object)值,否则该参数将被完全省略。

相关问题