英文:
How to fix Helm "installation failed" complaning about a nil pointer evaluating interface on fullnameOverride
问题
我正在构建一个新的Helm图表(mychart),我试图安装它。
存在一个values.yaml
文件,其内容指定了fullnameOverride
:
fullnameOverride: "myapp"
我运行了以下命令:
helm install --dry-run -f "mychart-stack/values.yaml" mychart-stack1 ./mychart-stack
但是它给我报错:
template: mychart-stack/templates/persistentvolume.local-storage.range.yml:5:14: 执行"mychart-stack/templates/persistentvolume.local-storage.range.yml"时出错,位置在<include "mychart-stack.fullname" .>,调用错误: 模板:mychart-stack/templates/_helpers.tpl:14:14: 执行"mychart-stack.fullname"时出错,位置在<.Values.fullnameOverride>: 空指针评估接口{}.fullnameOverride
mychart-stack/templates/_helpers.tpl:14:14
是当您要求Helm生成图表示例时预生成的内容。
错误(14:14)与以下自动生成的代码的第一行相关:
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
稍微提供一些背景信息,因为它在检查persistentvolume.local-storage.range.yml
时出错,以下是该文件的内容:
{{- range .Values.persistentVolume.localStorage }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-{{ include "mychart-stack.fullname" }}-{{ .name }}
spec:
capacity:
storage: 20Gi
# volumeMode字段需要启用Alpha BlockVolume的“feature gate”
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage-{{ include "mychart-stack.fullname" }}--{{ .name }}
local:
path: {{ .Values.persistentVolume.basePath }}/{{ .name }}
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- {{ .Values.hostName }}
{{- end }}
我不知道哪里出了问题,代码似乎表明它没有正确定义。我尝试以--debug模式运行它,但没有帮助(仍然出现相同的错误)。
英文:
I am building a new Helm chart (mychart) that I'm trying to install.
A values.yaml
exists and its contents specify the fullnameOverride:
fullnameOverride: "myapp"
I run the following command
helm install --dry-run -f "mychart-stack/values.yaml" mychart-stack1 ./mychart-stack
And it's giving me the error:
> template: mychart-stack/templates/persistentvolume.local-storage.range.yml:5:14: executing "mychart-stack/templates/persistentvolume.local-storage.range.yml" at <include "mychart-stack.fullname" .>: error calling include: template: mychart-stack/templates/_helpers.tpl:14:14: executing "mychart-stack.fullname" at <.Values.fullnameOverride>: nil pointer evaluating interface {}.fullnameOverride
The mychart-stack/templates/_helpers.tpl:14:14
is the pregenerated one when you're asking Helm to produce a Chart example.
The error (14:14) is associated at the first line of the following auto generated code:
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
A little more context, as it's throwing an error while checking the persistentvolume.local-storage.range.yml, here are the contents of the file:
{{- range .Values.persistentVolume.localStorage }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-{{ include "mychart-stack.fullname" }}-{{ .name }}
spec:
capacity:
storage: 20Gi
# le champ volumeMode requiert l'activation de la "feature gate" Alpha BlockVolume
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage-{{ include "mychart-stack.fullname" }}--{{ .name }}
local:
path: {{ .Values.persistentVolume.basePath }}/{{ .name }}
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- {{ .Values.hostName }}
{{- end }}
I don't know what's wrong, the code seems to indicate that it's not defined properly.
I tried to run it in --debug mode but it doesn't help (same error).
答案1
得分: 2
最后的问题并不是values.yaml设置不正确,而是在模板中使用的方式不对。
当使用来自.tpl文件(这个文件是Helm自动生成的)的定义时,我们必须小心不要进入范围。
我创建了一系列的资源,因此似乎代码将在范围的上下文中运行。
您的条件逻辑是在范围循环内评估的。这意味着您用来访问Values的. 不是您期望的那个,因为它在每个范围迭代评估中都被覆盖。
参考:https://stackoverflow.com/questions/57475521/ingress-yaml-template-returns-error-in-renderring-nil-pointer-evaluating-int
这意味着我们应该使用$
而不是.
符号,因为它引用全局范围。
示例:
{{- include "mychart-stack.fullname" $ }}
英文:
Finally the problem wasn't the values.yaml that was not set correctly but more the way it was used within the template.
When using an include of a definition coming from a .tpl file (this one was the autogenerated by Helm), we must be careful to not be in a range.
I was creating a range of assets so it seems that it will run the code in the context of the range.
> Your conditional logic is being evaluated inside a range loop. This means . you're using to access Values is not the one you expect it to be, as it's overridden for each range iteration evaluation.
That means that we should use $
instead of .
notation because it references the global scope.
Example:
{{- include "mychart-stack.fullname" $ }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论