1

我正在尝试从我的 terragrunt.hcl 输入中传递一个列表,其中包含容器的环境变量。但我无法让它工作。

terragrunt.hcl:

inputs = {
    containers_env = {
        "ENV_TEST" = "testing"
        "ENV_TEST_2" = "testing2"
    }
}

主文件:

resource "kubernetes_cron_job" "cronjob" {
    metadata {
        name      = var.metadata_name
        namespace = var.metadata_namespace
    }
    spec {
        schedule                      = var.spec_schedule
        concurrency_policy            = var.spec_concurrencyPolicy
        failed_jobs_history_limit     = var.spec_failedJobsHistoryLimit
        successful_jobs_history_limit = var.spec_successfulJobsHistoryLimit
        job_template {
            metadata {}
            spec {
                template {
                    metadata {}
                    spec {
                        restart_policy                      = var.template_spec_restartPolicy
                        termination_grace_period_seconds    = var.template_spec_terminationGracePeriodSeconds
                        container {
                            name                = var.containers_name
                            image               = var.containers_image
                            image_pull_policy   = var.containers_imagePullPolicy
                            command             = var.containers_command
                            env                 = var.containers_env
                        }
                    }
                }
            }
        }
    }
}

变量.tf:

variable "containers_env" {
    description = ""
    type        = map(string)
}

我收到错误:

Error: Unsupported argument

  on main.tf line 41, in resource "kubernetes_cron_job" "cronjob":
  41:                                                   env                             = var.containers_env

An argument named "env" is not expected here. Did you mean to define a block
of type "env"?

Terraform:v0.12.13 Terragrunt:v0.21.5

4

1 回答 1

3

块类型似乎需要将container环境变量定义为一系列嵌套env块。手动写出,它们可能看起来像这样:

env {
  name  = "ENV_TEST"
  value = "testing"
}
env {
  name  = "ENV_TEST_2"
  value = "testing2"
}

要基于 动态生成这些块var.containers_env,我们可以使用dynamicvar.containers_env其重复表达式为:

dynamic "env" {
  for_each = var.containers_env
  content {
    name  = env.key
    value = env.value
  }
}
于 2019-11-12T17:40:35.720 回答