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

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

Golang template (helm) iterating over a list of maps

问题

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

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

...
jobs:
  - name: 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报错如下:

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

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

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

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

我做错了什么?

英文:

I'm using helm to generate kubernetes yamls.

My values.yaml looks like this:

...
jobs:
  - nme: job1
    command: [sh, -c, &quot;/app/deployment/start.sh job1&quot;]
    activeDeadlineSeconds: 600
  - name: job2
    command: [sh, -c, &quot;/app/deployment/start.sh job2&quot;]
    activeDeadlineSeconds: 600
...

templates/jobs.yaml

{{ range $i, $job := .Values.jobs -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template &quot;name&quot; . }}-{{ $job.name }}
  labels:
    chart: &quot;{{ .Chart.Name }}-{{ .Chart.Version | replace &quot;+&quot; &quot;_&quot; }}&quot;
spec:
  activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
  template:
    metadata:
      labels:
        app: {{ template &quot;name&quot; . }}-{{ $job.name }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: {{ $job.command }}
        env:
{{ toYaml .Values.service.env | indent 10 }}
        ports:
        - containerPort: {{ .Values.service.internalPort }}
{{- end }}

Helm is failing with this error:

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:

{{- define &quot;name&quot; -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- 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

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

{{ $global := . }}
{{ range $i, $job := .Values.jobs -}}
apiVersion: batch/v1
kind: Job
metadata:
    name: {{ template "name" $global }}-{{ $job.name }}
    labels:
        chart: "{{ $global.Chart.Name }}-{{ $global.Chart.Version | replace "+" "_" }}"
spec:
    activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
    template:
        metadata:
            labels:
                app: {{ template "name" $global }}-{{ $job.name }}
        spec:
            containers:
            - name: {{ $global.Chart.Name }}
              image: "{{ $global.Values.image.repository }}:{{ $global.Values.image.tag }}"
              imagePullPolicy: {{ $global.Values.image.pullPolicy }}
              command: {{ $job.command }}
              env:
{{ toYaml $global.Values.service.env | indent 10 }}
              ports:
              - containerPort: {{ $global.Values.service.internalPort }}
{{- end }}
英文:

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

{{ $global := . }}
{{ range $i, $job := .Values.jobs -}}
apiVersion: batch/v1
kind: Job
metadata:
    name: {{ template &quot;name&quot; $global }}-{{ $job.name }}
    labels:
    chart: &quot;{{ $global.Chart.Name }}-{{ $global.Chart.Version | replace &quot;+&quot; &quot;_&quot; }}&quot;
spec:
    activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
    template:
    metadata:
        labels:
        app: {{ template &quot;name&quot; $global }}-{{ $job.name }}
    spec:
        containers:
        - name: {{ $global.Chart.Name }}
        image: &quot;{{ $global.Values.image.repository }}:{{ $global.Values.image.tag }}&quot;
        imagePullPolicy: {{ $global.Values.image.pullPolicy }}
        command: {{ $job.command }}
        env:
{{ toYaml $global.Values.service.env | indent 10 }}
        ports:
        - containerPort: {{ $global.Values.service.internalPort }}
{{- 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:

确定