Golang模板(头盔)在地图列表上进行迭代 [英] Golang template (helm) iterating over a list of maps

查看:133
本文介绍了Golang模板(头盔)在地图列表上进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用掌舵来生成kubernetes yamls.

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 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.Name来访问数据.

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模板(头盔)在地图列表上进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆