【问题标题】:combining 2 yaml files to generate one values file for helm in terraform合并 2 个 yaml 文件以在 terraform 中为 helm 生成一个值文件
【发布时间】:2022-06-22 11:07:58
【问题描述】:

假设我有来自 2 个不同文件的 2 个 yaml 值,例如:

lambo.yaml:

    - key: car
      value: "lambo"
      descriptors:
        unit: kmh
        topspeed: 300

toyota.yaml:

    - key: car
      value: "bugatti"
      descriptors:
        unit: kmh
        topspeed: 400

并且我想形成一个 yaml 值文件以在 helm 图表中使用,例如:

result.yaml:

    domain: supercardomain
    descriptors:
      - key: supercars
        descriptors:
        - key: car
          value: "lambo"
          descriptors:
            unit: kmh
            topspeed: 300
        - key: car
          value: "bugatti"
          descriptors:
            unit: kmh
            topspeed: 400

CONTEXT:我在 terraform 中有一个 helm_release,它需要一个 ConfigMap(k8s),其值与上述值相同 (result.yaml)。在 helm 中合并值文件是 not possible,我们也希望避免使用任何惰性硬编码方法(例如创建 .Values.lambo.Values.toyota yaml 结构并将它们附加到 configmap helm 模板)。

我试过的是:

car_descriptor=indent(2, format("car:\ndescriptors:\n%s\n%s", var.lambo_descriptor, var.buggati_descriptor)

var.<car>_descriptor 是汽车 yaml 值的字符串表示形式。

然后它被 helm_release 用作值文件:

resource "helm_release" "my_helm_release" {
  name             = "my_helm_release"
  ...

  values = [
    var.car_descriptor
  ]

然后像这样模板化到 Configmap 中:

apiVersion: v1
kind: ConfigMap
metadata:
  name: car-config
data:
  config.yaml: |
    domain: supercardomain
    descriptors:
    - key: supercars
      descriptors:
{{- if .Values.car.descriptors }}
{{ toYaml .Values.car.descriptors | indent 6 }}
{{- end }}

它有效,但我想知道是否有一种更简单、更 DRY 的方式来执行此操作,而无需执行格式和缩进,而仅使用 terraform/helm。另外,我不想弄乱 2 个汽车文件的 yaml 结构,或者 Configmap (I have already tried this) 中的模板。

也非常感谢使这个问题更简洁的提示:)

【问题讨论】:

  • 可能是yamlencodejoin 的某种组合?
  • car_descriptor= join("", yamlencode(var.lambo_descriptor), yamlencode(var.buggati_descriptor))

标签: kubernetes terraform yaml kubernetes-helm


【解决方案1】:

.
├── Chart.yaml
├── charts
├── data
│   ├── cars
│   │   ├── lambo.yaml
│   │   └── toyota.yaml
│   └── cpus
│       ├── amd.yaml
│       └── intel.yaml
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── configmap.yaml
│   └── deployment.yaml
└── values.yaml

数据/汽车/lambo.yaml

- key: car
  value: "lambo"
  descriptors:
    unit: kmh
    topspeed: 300

数据/汽车/toyota.yaml

- key: car
  value: "bugatti"
  descriptors:
    unit: kmh
    topspeed: 400

数据/cpus/amd.yaml

- key: car
  value: "amd"
  descriptors:
    unit: cps
    topspeed: 20

数据/cpus/intel.yaml

- key: cpu
  value: "intel"
  descriptors:
    unit: cps
    topspeed: 10

模板/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  config.yaml: |
    domain: supercardomain
    descriptors:
    {{- range .Values.car.descriptors }}
      {{- $item := . }}
      - key: {{ .key }}
        descriptors:
        {{- range $path, $_ :=  $.Files.Glob  "data/**" }}
        {{- if eq (dir $path) $item.descripPath }}
        {{- $.Files.Get $path | nindent 10 }}
        {{- end }}
        {{- end }}
    {{- end }}

命令

helm install test .

输出

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  config.yaml: |
    domain: supercardomain
    descriptors:
      - key: supercars
        descriptors:
          - key: car
            value: "lambo"
            descriptors:
              unit: kmh
              topspeed: 300
          - key: car
            value: "bugatti"
            descriptors:
              unit: kmh
              topspeed: 400
      - key: cpus
        descriptors:
          - key: car
            value: "amd"
            descriptors:
              unit: cps
              topspeed: 20
          - key: cpu
            value: "intel"
            descriptors:
              unit: cps
              topspeed: 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2020-09-01
    • 2013-01-22
    相关资源
    最近更新 更多