假設我有來自 2 個不同檔案的 2 個 yaml 值,例如:
蘭博.yaml:
- key: car
value: "lambo"
descriptors:
unit: kmh
topspeed: 300
豐田.yaml:
- key: car
value: "bugatti"
descriptors:
unit: kmh
topspeed: 400
我想形成一個 yaml 值檔案以在 helm 圖表中使用,例如:
結果.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
背景關系:我在 terraform 中有一個 helm_release,它需要一個 ConfigMap(k8s),其值與上述值相同(result.yaml)。在 helm 中合并值檔案是不可能的,我們也希望避免使用任何惰性硬編碼方法(例如創建.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 }}
它有效,但我想知道是否有一種更簡單、更干燥的方法來執行此操作,而無需執行格式和縮進,而僅使用 terraform/helm。另外,我不想弄亂 2 個汽車檔案的 yaml 結構,或者 Configmap 中的模板(我已經嘗試過這個)。
使這個問題更簡潔的提示也非常感謝:)
uj5u.com熱心網友回復:
樹
.
├── 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
資料/CPU/amd.yaml
- key: cpu
value: "amd"
descriptors:
unit: cps
topspeed: 20
資料/CPU/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 }}
值.yaml
car:
domain: supercardomain
descriptors:
- key: supercars
descripPath: data/cars
- key: cpus
descripPath: data/cpus
命令
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: cpu
value: "amd"
descriptors:
unit: cps
topspeed: 20
- key: cpu
value: "intel"
descriptors:
unit: cps
topspeed: 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/494869.html