问题描述
在使用Helm charts时遇到了一个问题,他无法从子图表中访问主图表中定义的一些命名模板。根据文档,每个定义的命名模板都是全局的,他想知道自己在这里做错了什么。
以下是主图表中在_helper.tpl
中定义的命名模板,他试图在子图表的configMap
中访问它,但结果是模板不存在的错误。
_helper.tpl inside chart/templates | |
{{- define "main.postgres"}} | |
POSTGRES_URL: 172.30.30.39 | |
POSTGRES_PORT: 5432 | |
{{- end }} |
configMap.yaml inside chart/subchart/templates | |
apiVerions: v1 | |
kind: ConfigMap | |
metadata: | |
name: subchart-config | |
data: | |
{{- template "main.postgres" .}} |
错误信息:
error calling include: template: no template "main.postgres" associated with template "gotpl"
解决方案
请注意以下操作注意版本差异及修改前做好备份。
方案1
根据你提供的信息,我发现了Helm模板中的问题。Helm图表中的charts目录应该是charts
,而不是chart
。我尝试模拟了你的问题,并成功修复了它。
我重新组织了目录结构如下:
. | |
├── Chart.yaml | |
├── charts | |
│ └── subchart | |
│ ├── Chart.yaml | |
│ ├── charts | |
│ ├── templates | |
│ │ └── configMap.yaml | |
│ └── values.yaml | |
├── templates | |
│ └── _helper.tpl | |
└── values.yaml |
我使用了你提供的_helper.tpl
文件。
{{- define "main.postgres"}} | |
POSTGRES_URL: 172.30.30.39 | |
POSTGRES_PORT: 5432 | |
{{- end }} |
当我运行helm template .
命令时,我得到了你期望的正确结果。
helm template . | |
# Source: test/charts/subchart/templates/configMap.yaml | |
apiVerions: v1 | |
kind: ConfigMap | |
metadata: | |
name: subchart-config | |
data: | |
POSTGRES_URL: 172.30.30.39 | |
POSTGRES_PORT: 5432 |
方案2
请注意以下操作注意版本差异及修改前做好备份。
另一种可能的问题是目录结构不正确。请确保目录名为templates
,而不是template
。
templates | |
└── _helper.tpl |
希望这些解决方案能帮助到你解决问题。如果还有其他问题,请随时提问。
正文完