You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform的helm_release覆盖Helm Values中的对象列表?

用Terraform的helm_release覆盖Helm Values中的对象列表

如果你需要通过Terraform的helm_release资源覆盖Helm Values里的对象列表(比如imagePullSecrets这种结构),可以参考以下正确实现方式:

目标场景示例

要覆盖的Helm Values原结构通常是这样的对象列表:

imagePullSecrets: 
  - name: artifactory-image-pull 

你之前尝试的两种无效方法

  1. 索引式set块(仅当原Values已有对应列表项时才可能生效,空列表或无此键时会失败):
set {
  name  = "imagePullSecrets[0].name"
  value = var.image_pull_secret_name
}
  1. 错误使用set_list(set_list仅支持字符串列表,无法解析对象结构):
set_list {
  name  = "imagePullSecrets"
  value = [{ name : var.image_pull_secret_name }]
}

正确实现方式

方法1:使用带type="yaml"的set块

通过将对象列表以YAML字符串形式传入,并指定类型为yaml,让Helm正确解析结构:

resource "helm_release" "example" {
  name       = "example"
  repository = "https://example-charts.com/"
  chart      = "example-chart"
  version    = "1.0.0"

  set {
    name  = "imagePullSecrets"
    value = <<YAML
- name: ${var.image_pull_secret_name}
YAML
    type  = "yaml"
  }
}

方法2:使用values参数直接传递结构化数据

通过yamlencode将Terraform的对象列表转为YAML字符串,直接合并覆盖Values:

resource "helm_release" "example" {
  name       = "example"
  repository = "https://example-charts.com/"
  chart      = "example-chart"
  version    = "1.0.0"

  values = [
    yamlencode({
      imagePullSecrets = [{
        name = var.image_pull_secret_name
      }]
    })
  ]
}

补充说明

  • 方法1适合仅覆盖单个键值对的场景,灵活性高;
  • 方法2适合批量覆盖多个Values配置的场景,结构更清晰;
  • 若原Values中imagePullSecrets已有其他项,以上方法会完全替换该列表,而非追加。如果需要追加,需先通过helm_template数据源获取原Values,再合并列表后传入。

内容的提问来源于stack exchange,提问作者Jon Carlson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 07:52:35