Golang模板(helm)遍历一个映射列表。

huangapple go评论137阅读模式
英文:

Golang template (helm) iterating over a list of maps

问题

我正在使用Helm生成Kubernetes的YAML文件。

我的values.yaml文件如下所示:

  1. ...
  2. jobs:
  3. - name: job1
  4. command: [sh, -c, "/app/deployment/start.sh job1"]
  5. activeDeadlineSeconds: 600
  6. - name: job2
  7. command: [sh, -c, "/app/deployment/start.sh job2"]
  8. activeDeadlineSeconds: 600
  9. ...

templates/jobs.yaml

  1. {{ range $i, $job := .Values.jobs -}}
  2. apiVersion: batch/v1
  3. kind: Job
  4. metadata:
  5. name: {{ template "name" . }}-{{ $job.name }}
  6. labels:
  7. chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
  8. spec:
  9. activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
  10. template:
  11. metadata:
  12. labels:
  13. app: {{ template "name" . }}-{{ $job.name }}
  14. spec:
  15. containers:
  16. - name: {{ .Chart.Name }}
  17. image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
  18. imagePullPolicy: {{ .Values.image.pullPolicy }}
  19. command: {{ $job.command }}
  20. env:
  21. {{ toYaml .Values.service.env | indent 10 }}
  22. ports:
  23. - containerPort: {{ .Values.service.internalPort }}
  24. {{- end }}

Helm报错如下:

  1. 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文件时:

  1. {{- define "name" -}}
  2. {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
  3. {{- end -}}

如果我移除range循环和jobs.yaml中对$job的引用,_helpers.tpl中的name模板就能正常工作。但是当我添加循环时,就会失败。

似乎在循环内部,所有包含.Chart和.Values的点.管道都被重新分配给其他内容。

我做错了什么?

英文:

I'm using helm to generate kubernetes yamls.

My values.yaml looks like this:

  1. ...
  2. jobs:
  3. - nme: job1
  4. command: [sh, -c, &quot;/app/deployment/start.sh job1&quot;]
  5. activeDeadlineSeconds: 600
  6. - name: job2
  7. command: [sh, -c, &quot;/app/deployment/start.sh job2&quot;]
  8. activeDeadlineSeconds: 600
  9. ...

templates/jobs.yaml

  1. {{ range $i, $job := .Values.jobs -}}
  2. apiVersion: batch/v1
  3. kind: Job
  4. metadata:
  5. name: {{ template &quot;name&quot; . }}-{{ $job.name }}
  6. labels:
  7. chart: &quot;{{ .Chart.Name }}-{{ .Chart.Version | replace &quot;+&quot; &quot;_&quot; }}&quot;
  8. spec:
  9. activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
  10. template:
  11. metadata:
  12. labels:
  13. app: {{ template &quot;name&quot; . }}-{{ $job.name }}
  14. spec:
  15. containers:
  16. - name: {{ .Chart.Name }}
  17. image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
  18. imagePullPolicy: {{ .Values.image.pullPolicy }}
  19. command: {{ $job.command }}
  20. env:
  21. {{ toYaml .Values.service.env | indent 10 }}
  22. ports:
  23. - containerPort: {{ .Values.service.internalPort }}
  24. {{- end }}

Helm is failing with this error:

  1. Error: UPGRADE FAILED: render error in &quot;app1/templates/jobs.yaml&quot;: template: app1/templates/_helpers.tpl:6:18: executing &quot;name&quot; at &lt;.Chart.Name&gt;: can&#39;t evaluate field Name in type interface {}

When I look at _helpers.tpl:

  1. {{- define &quot;name&quot; -}}
  2. {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix &quot;-&quot; -}}
  3. {{- end -}}

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.

It seems like within the loop, all dot . pipeline, which contains the scope for .Chart and .Values, is reassigned to something else.

What am I doing wrong?

答案1

得分: 12

在循环内部,.的值被设置为当前元素,你需要使用$.Chart.Name来访问你的数据。

我曾经问过一个类似的问题,我认为这个答案https://stackoverflow.com/a/44734585/8131948也会回答你的问题。

英文:

Inside the loop the value of the . is set to the current element and you have to use $.Chart.Name to access your data.

I asked a similar question and I think the answer https://stackoverflow.com/a/44734585/8131948 will answer your question too.

答案2

得分: 4

我最终保存了全局上下文,然后按照以下方式更新了所有的引用:

  1. {{ $global := . }}
  2. {{ range $i, $job := .Values.jobs -}}
  3. apiVersion: batch/v1
  4. kind: Job
  5. metadata:
  6. name: {{ template "name" $global }}-{{ $job.name }}
  7. labels:
  8. chart: "{{ $global.Chart.Name }}-{{ $global.Chart.Version | replace "+" "_" }}"
  9. spec:
  10. activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
  11. template:
  12. metadata:
  13. labels:
  14. app: {{ template "name" $global }}-{{ $job.name }}
  15. spec:
  16. containers:
  17. - name: {{ $global.Chart.Name }}
  18. image: "{{ $global.Values.image.repository }}:{{ $global.Values.image.tag }}"
  19. imagePullPolicy: {{ $global.Values.image.pullPolicy }}
  20. command: {{ $job.command }}
  21. env:
  22. {{ toYaml $global.Values.service.env | indent 10 }}
  23. ports:
  24. - containerPort: {{ $global.Values.service.internalPort }}
  25. {{- end }}
英文:

I ended up saving the global context and then updating all of my references like this:

  1. {{ $global := . }}
  2. {{ range $i, $job := .Values.jobs -}}
  3. apiVersion: batch/v1
  4. kind: Job
  5. metadata:
  6. name: {{ template &quot;name&quot; $global }}-{{ $job.name }}
  7. labels:
  8. chart: &quot;{{ $global.Chart.Name }}-{{ $global.Chart.Version | replace &quot;+&quot; &quot;_&quot; }}&quot;
  9. spec:
  10. activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
  11. template:
  12. metadata:
  13. labels:
  14. app: {{ template &quot;name&quot; $global }}-{{ $job.name }}
  15. spec:
  16. containers:
  17. - name: {{ $global.Chart.Name }}
  18. image: &quot;{{ $global.Values.image.repository }}:{{ $global.Values.image.tag }}&quot;
  19. imagePullPolicy: {{ $global.Values.image.pullPolicy }}
  20. command: {{ $job.command }}
  21. env:
  22. {{ toYaml $global.Values.service.env | indent 10 }}
  23. ports:
  24. - containerPort: {{ $global.Values.service.internalPort }}
  25. {{- end }}

huangapple
  • 本文由 发表于 2017年7月20日 02:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/45198856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定