登录
首页 >  Golang >  Go问答

用于 configmap 的 Helm 模板预定义数组迭代

来源:stackoverflow

时间:2024-02-12 13:45:23 411浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《用于 configmap 的 Helm 模板预定义数组迭代》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

您好,我们在values.yaml中有listenerprocessor数组,如下所示

listenerprocessor:
- name: "nbi-sbi"
  port: 99
  allowedpath:
  - "/sbi/test"
  - "/sbi/test123"

为此,我们需要合并我们预定义的listenerprocessor数组,该数组未通过values.yaml公开,如下所示

listenerprocessor:
- name: nbinorc
  port: 9910
  allowedpath: 
  - /nbi/norc/tmf641/v4/orders/
  - /nbi/norc/tmf645/v4/servicequalification
- name: nbiuiv
  port: 9920
  allowedpath: 
  - /nbi/uiv/tmf640/v4/services/

为了将这两个属性合并为单个属性并对其进行迭代,在 _helper.tpl 中定义了一个变量

{{- define "nifi.listener-processor.spec" -}}
listenerprocessor:
- name: nbinorc
  port: 9910
  allowedpath: 
  - /nbi/norc/tmf641/v4/orders/
  - /nbi/norc/tmf645/v4/servicequalification
- name: nbiuiv
  port: 9920
  allowedpath: 
  - /nbi/uiv/tmf640/v4/services/
{{ toyaml .values.listenerprocessor.ports }}
{{- end -}}

现在我需要在notes.txt中迭代.name.port,但它给出错误“ <.name>:无法评估类型中的字段名称界面 {}”。 在notes.txt我有这个

{{- $listenerprocessor := (include "nifi.listener-processor.spec" . ) }}
{{- range $index, $element :=  fromyaml $listenerprocessor }}
  {{ .name }}
  {{ .port }}
{{- end }}

当我只是打印 $element$index 时,这些值显示在输出下方

NOTES:
$NIFI_CONF_DIRECTORY_PATH/extensions directory"
  
 [map[allowedPath:[/nbi/norc/tmf641/v4/orders/ /nbi/norc/tmf645/v4/serviceQualification] name:nbinorc port:9910] map[allowedPath:[/nbi/uiv/tmf640/v4/services/] name:nbiuiv port:9920] map[allowedPath:[/sbi/test /sbi/test123] name:nbi-sbi port:99]]

监听器处理器


正确答案


明白了,使用下面的代码,我们需要另一个变量,然后将其传递给范围超出

{{- $listenerProcessor :=  (include "nifi.listener-processor.spec" . ) }}
{{- $test := fromYaml $listenerProcessor }}
{{- range $index, $element := $test.listenerProcessor }}
  {{ .name }} {{ .port }} {{ .allowedPath }}
{{- end }}

到这里,我们也就讲完了《用于 configmap 的 Helm 模板预定义数组迭代》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>