[英]How to fix `can't evaluate field extraHosts in type interface {}` in _helpers.tpl in helm
I am trying to get some values from Umbrella chart in helm in _helpers.tpl
but I for some reason I am getting the error executing "gluu.ldaplist" at <.Values.ldap.extraHo...>: can't evaluate field extraHosts in type interface {}
我正在尝试从_helpers.tpl
中的头盔伞图中获取一些值,但是由于某些原因,我executing "gluu.ldaplist" at <.Values.ldap.extraHo...>: can't evaluate field extraHosts in type interface {}
时遇到错误executing "gluu.ldaplist" at <.Values.ldap.extraHo...>: can't evaluate field extraHosts in type interface {}
This is what I am trying to do. 这就是我想要做的。 _helpers.ptl
{{- define "gluu.ldaplist" -}}
{{- $hosts := .Values.ldap.extraHosts -}}
{{- $genLdap := dict "host" (printf "%s-%s" .Release.Name .Values.ldapType) "port" .Values.ldapPort -}}
{{- $hosts := prepend $hosts $genLdap -}}
{{- $local := dict "first" true -}}
{{- range $k, $v := $hosts -}}
{{- if not $local.first -}},{{- end -}}{{- printf "%s:%.f" $v.host $v.port -}}{{- $_ := set $local "first" false -}}
{{- end -}}
{{- end -}}
And this is part of values.yml
for the umbrella chart values.yml
这也是部分values.yml
的伞图表values.yml
ldap:
enabled: true
type: opendj
extraHosts: [
host: opendj,
port: 3434
] #array of k,v e.g host: host1, port: port1
Directory structure 目录结构
helm/
charts/
chart_a/
templates/
configMap.yml ----->>> this is where I want to use it
templates/
_helpers.tpl ---->>>> where the failing function is
requirements.yml
values.yml ---------->>> where the ldap values are
The configMap.yml
looks like below configMap.yml
如下所示
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "oxauth.fullname" . }}-cm
data:
GLUU_CONFIG_ADAPTER: {{ .Values.global.configAdapterName | quote }}
GLUU_LDAP_URL: {{ template "gluu.ldaplist" . }}
NOTE: The _helpers.tpl
is under the main/umbrella chart. 注意: _helpers.tpl
在主/伞形图下。 chart_a
is a subchart. chart_a
是一个子图表。
Expected results are something like GLUU_LDAP_URL:"opendj:3434"
预期结果类似于GLUU_LDAP_URL:"opendj:3434"
Helm version: 头盔版本:
Client: &version.Version{SemVer:"v2.10.0", GitCommit:"9ad53aac42165a5fadc6c87be0dea6b115f93090", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.10.0", GitCommit:"9ad53aac42165a5fadc6c87be0dea6b115f93090", GitTreeState:"clean"}
Expected result is that the function {{- define "gluu.ldaplist" -}}
in _helpers.tpl
completes without error even if no values are provided in the array. 预期的结果是,即使数组中未提供任何值, _helpers.tpl
中的函数{{- define "gluu.ldaplist" -}}
_helpers.tpl
完成。 If there are values provided, the expected string is host:port
as output. 如果提供了值,则预期的字符串为host:port
作为输出。
If this can be done in another way, I welcome any suggestion. 如果可以通过其他方式完成此操作,我欢迎任何建议。
This can be solved with global values which allow values in the parent chart to override (or supply unspecified) values in the child subcharts. 这可以通过全局值解决,该全局值允许父图表中的值覆盖(或提供未指定的)子子图中的值。
From the Helm docs on Subcharts and Global Values : 来自有关子图和全局值的Helm文档 :
- A subchart is considered “stand-alone”, which means a subchart can never explicitly depend on its parent chart. 子图表被认为是“独立的”,这意味着子图表永远不能显式依赖其父图表。
- For that reason, a subchart cannot access the values of its parent . 因此, 子图无法访问其父级的值 。
- A parent chart can override values for subcharts. 父图表可以覆盖子图表的值。
- Helm has a concept of global values that can be accessed by all charts . Helm具有全局值的概念,所有图表都可以访问 。
(At first I didn't think to search for "helm subchart" but once I did an Internet search for that term, this was the first or second result) (起初我不打算搜索“ helm subchart”,但是一旦我在互联网上搜索了该术语,这就是第一个或第二个结果)
Here's a minimal example that solves your issue: 这是一个解决您问题的最小示例:
helm
├── Chart.yaml
├── charts
│ └── chart_a
│ ├── Chart.yaml
│ └── templates
│ └── configMap.yml
├── templates
│ └── _helpers.tpl
└── values.yaml
Note: I added Chart.yaml
files to make it actually work, renamed values.yml
to values.yaml
so that it works by default without extra flags, and removed requirements.yml
since it wasn't necessary to reproduce the problem and solution. 注:我添加Chart.yaml
文件,使之能工作,改名values.yml
到values.yaml
使其作品在默认情况下,无需额外的标志,并删除requirements.yml
因为它没有必要来重现问题和解决方案。
values.yaml
global:
ldap:
enabled: true
type: opendj
extraHosts:
- host: opendj
port: 3434
ldapType: xxx
ldapPort: 123
The key was to nest what you had under a special global
key. 关键是将您拥有的内容嵌套在特殊的global
密钥下。 Note, I also added ldapType
and ldapPort
since they were in your _helpers.tpl
, and I fixed the YAML structure you had under extraHosts
. 注意,我还添加了ldapType
和ldapPort
因为它们在您的_helpers.tpl
,并且修复了extraHosts
下的YAML结构。 What was there before didn't actually represent a list of maps with host
and port
keys. 之前的内容实际上并不代表具有host
和port
键的地图列表。 Without this fix, the helm
command doesn't fail but doesn't output what you want either. 没有此修复程序, helm
命令不会失败,但是也不会输出您想要的任何内容。
$ helm template .
---
# Source: helm/charts/chart_a/templates/configMap.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm
data:
GLUU_LDAP_URL: release-name-xxx:123,opendj:3434
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.