首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Helm helper.tpl从values.yaml或Chart.yaml设置存储库和映像

使用Helm helper.tpl从values.yaml或Chart.yaml设置存储库和映像
EN

Stack Overflow用户
提问于 2022-03-31 15:52:50
回答 1查看 2.6K关注 0票数 0

我有几张不同产品的图表。在第一个版本中,编写了一个帮助程序来构建回购、图像名称和标记/版本。这是可行的,但由于另一个图表是完全不同的,我经历了一个更简单的方法,但它不起作用。我知道错误了,

代码语言:javascript
运行
复制
error calling include: template: MYChart/templates/_helpers.tpl:94:28: executing "getImageName" at <.Values.registryName>: nil pointer evaluating interface {}.registryName

这是帮手。

代码语言:javascript
运行
复制
{{/*
This allows us to not have image: .Values.xxxx.ssss/.Values.xxx.xxx:.Values.ssss
in every single template.
*/}}
{{- define "imageName" -}}
{{- $registryName := .Values.registryName -}}
{{- $imageName := .Values.imageName -}}
{{- $tag := .Chart.AppVersion -}}
{{- printf "%s/%s:%s" $registryName $imageName $tag -}}
{{- end -}}

这些都是

代码语言:javascript
运行
复制
registry:
  registryName: "index.docker.io/myrepo"
  image_Name: "myimage"

在_helper.tpl中调用上述值应该有效,有很多使用这种方法的示例。我遗漏了什么?

模板文件:

代码语言:javascript
运行
复制
{{- $root := . -}}
{{- $FullChartName := include "myapp.fullname" . -}}
{{- $ChartName := include "myapp.name" . -}}
{{- range $worker, $parameter := .Values.workerPods }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $parameter.name }}-worker
spec:
  replicas: {{ $parameter.replicas }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ $parameter.name }}-worker
      app.kubernetes.io/instance: {{ $root.Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ $parameter.name }}-worker
        app.kubernetes.io/instance: {{ $root.Release.Name }}
        autoscale: "true"
      annotations:
      {{- if $root.Values.worker.annotations }}
      {{ toYaml $root.Values.worker.annotations | indent 8 }}
      {{- end }}
    spec:
      imagePullSecrets:
        - name: myapp-registry-credentials
      containers:
        - name: {{ $parameter.name }}-worker
          image: {{ template "imageName" . }}
          imagePullPolicy: {{ $root.Values.worker.image.pullPolicy }}
          command: ["/bin/sh"]
          args: ["-c", "until /usr/bin/pg_isready -h $DATABASE_HOST; do sleep 2; done; bundle exec rake jobs:work"]
          {{- range $container, $containerResources := $root.Values.containers }}
          {{- if eq $container $parameter.size }}
          resources:
            {{- toYaml $containerResources.resources | nindent 12 }}
          {{- end }}
          {{- end }}
          envFrom:
            - configMapRef:
                name: common-env
            - secretRef:
                name: myapp-secrets          

          volumeMounts:
          - name: mnt-data
            mountPath: "/mnt/data"
      volumes:
        - name: mnt-data
          persistentVolumeClaim:
            claimName: myapp-pvc
      {{- with $root.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with $root.Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with $root.Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
{{- end }}

我也尝试过这种方法,并在Chart.yaml中添加了以下内容,但也出现了类似的错误,老实说,我不确定这是否有效,但我想听听其他人的想法。

代码语言:javascript
运行
复制
annotations:
  image: "myimage"
  registry: "index.docker.io/myrepo"

帮手长得像这样。

代码语言:javascript
运行
复制
{{/*
This allows us to not have image: .Values.xxxx.ssss/.Values.xxx.xxx:.Values.ssss
in every single template.
*/}}
{{- define "imageName" -}}
{{- $registryName := .Chart.Annotations.registry -}}
{{- $imageName := .Chart.Annotations.image -}}
{{- $tag := .Chart.AppVersion -}}
{{- printf "%s/%s:%s" $registryName $imageName $tag -}}
{{- end -}}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-04 13:38:01

使用错误的参数调用模板。将Helm模板文件减少到最低限度,以演示这一点:

代码语言:javascript
运行
复制
{{- $root := . -}}
{{- range $worker, $parameter := .Values.workerPods }}
image: {{ template "imageName" . }}
imagePullPolicy: {{ $root.Values.worker.image.pullPolicy }}
{{- end }}

标准的Go text/template range语句重新绑定.变量(我相信与$parameter相同)。因此,当您调用imageName模板时,它的参数不是Helm根值,而是来自值文件的块;.Values是未定义的,并返回nil;然后.Values.registryName是对nil的查找,这会产生您看到的错误。

解决这一问题的一个标准方法是将.保存到range循环之外的变量中,并在您使用.的任何地方使用该变量。实际上,您已经这样做了,下面一行中的$root.Values.worker...引用应该可以正常工作。你只需要在呼叫时改变它:

代码语言:javascript
运行
复制
image: {{ template "imageName" $root }}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71695632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档