繁体   English   中英

如何修复头盔中_helpers.tpl中类型接口{}中的“无法评估字段ExtraHosts”

[英]How to fix `can't evaluate field extraHosts in type interface {}` in _helpers.tpl in helm

我正在尝试从_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 {}

这就是我想要做的。 _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 -}}

这也是部分values.yml的伞图表values.yml

ldap:
  enabled: true
  type: opendj
  extraHosts: [
    host: opendj,
    port: 3434
  ] #array of k,v e.g host: host1, port: port1

目录结构

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

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" . }}

注意: _helpers.tpl在主/伞形图下。 chart_a是一个子图表。

预期结果类似于GLUU_LDAP_URL:"opendj:3434"

头盔版本:

Client: &version.Version{SemVer:"v2.10.0", GitCommit:"9ad53aac42165a5fadc6c87be0dea6b115f93090", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.10.0", GitCommit:"9ad53aac42165a5fadc6c87be0dea6b115f93090", GitTreeState:"clean"}

预期的结果是,即使数组中未提供任何值, _helpers.tpl中的函数{{- define "gluu.ldaplist" -}} _helpers.tpl完成。 如果提供了值,则预期的字符串为host:port作为输出。

如果可以通过其他方式完成此操作,我欢迎任何建议。

这可以通过全局值解决,该全局值允许父图表中的值覆盖(或提供未指定的)子子图中的值。

来自有关子图和全局值Helm文档

  1. 子图表被认为是“独立的”,这意味着子图表永远不能显式依赖其父图表。
  2. 因此, 子图无法访问其父级的值
  3. 父图表可以覆盖子图表的值。
  4. Helm具有全局值的概念,所有图表都可以访问

(起初我不打算搜索“ helm subchart”,但是一旦我在互联网上搜索了该术语,这就是第一个或第二个结果)

这是一个解决您问题的最小示例:

目录结构

helm
├── Chart.yaml
├── charts
│   └── chart_a
│       ├── Chart.yaml
│       └── templates
│           └── configMap.yml
├── templates
│   └── _helpers.tpl
└── values.yaml

注:我添加Chart.yaml文件,使之能工作,改名values.ymlvalues.yaml使其作品在默认情况下,无需额外的标志,并删除requirements.yml因为它没有必要来重现问题和解决方案。

values.yaml

global:
  ldap:
    enabled: true
    type: opendj
    extraHosts:
    - host: opendj
      port: 3434
  ldapType: xxx
  ldapPort: 123

关键是将您拥有的内容嵌套在特殊的global密钥下。 注意,我还添加了ldapTypeldapPort因为它们在您的_helpers.tpl ,并且修复了extraHosts下的YAML结构。 之前的内容实际上并不代表具有hostport键的地图列表。 没有此修复程序, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM