遍历地图列表的 Golang 模板(helm) [英] Golang template (helm) iterating over a list of maps
问题描述
我正在使用 helm 生成 kubernetes yaml.
I'm using helm to generate kubernetes yamls.
我的 values.yaml 看起来像这样:
My values.yaml looks like this:
...
jobs:
- nme: job1
command: [sh, -c, "/app/deployment/start.sh job1"]
activeDeadlineSeconds: 600
- name: job2
command: [sh, -c, "/app/deployment/start.sh job2"]
activeDeadlineSeconds: 600
...
templates/jobs.yaml
{{ range $i, $job := .Values.jobs -}}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "name" . }}-{{ $job.name }}
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
spec:
activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
template:
metadata:
labels:
app: {{ template "name" . }}-{{ $job.name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: {{ $job.command }}
env:
{{ toYaml .Values.service.env | indent 10 }}
ports:
- containerPort: {{ .Values.service.internalPort }}
{{- end }}
Helm 因此错误而失败:
Helm is failing with this error:
Error: UPGRADE FAILED: render error in "app1/templates/jobs.yaml": template: app1/templates/_helpers.tpl:6:18: executing "name" at <.Chart.Name>: can't evaluate field Name in type interface {}
当我查看 _helpers.tpl 时:
When I look at _helpers.tpl:
{{- define "name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
如果我在 jobs.yaml
中删除范围循环和对 $job 的引用,_helpers.tpl name
模板可以正常工作.当我在循环中添加时,它失败了.
If I remove the range loop and references to $job in my jobs.yaml
, the _helpers.tpl name
template works fine. When I add in the loop, it fails.
似乎在循环中,包含 .Chart
和 .Values
范围的所有点 .
管道都被重新分配给别的东西.
It seems like within the loop, all dot .
pipeline, which contains the scope for .Chart
and .Values
, is reassigned to something else.
我做错了什么?
推荐答案
在循环内部,.
的值被设置为当前元素,你必须使用 $.Chart.名称
以访问您的数据.
Inside the loop the value of the .
is set to the current element and you have to use $.Chart.Name
to access your data.
我问了一个类似的问题,我认为答案https://stackoverflow.com/a/44734585/8131948也会回答你的问题.
I asked a similar question and I think the answer https://stackoverflow.com/a/44734585/8131948 will answer your question too.
这篇关于遍历地图列表的 Golang 模板(helm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!