helm 从值文件中加入列表

helm join list from values file

我正在寻找一种解决方案,以将 values.yaml 中的列表转换为逗号分隔列表。

values.yaml

app:
  logfiletoexclude:
    - "/var/log/containers/kube*"
    - "/var/log/containers/tiller*"

_helpers.tpl:

{{- define "pathtoexclude" -}}
{{- join "," .Values.app.logfiletoexclude }}
{{- end -}}

配置图:

<source>
  @type tail
  path /var/log/containers/*.log
  exclude_path [{{ template "pathtoexclude" . }}]
  ...
  ...
</source>

问题是我的结果中缺少引号

 exclude_path [/var/log/containers/kube*,/var/log/containers/tiller*]

我该如何修复才能拥有:

  exclude_path ["/var/log/containers/kube*","/var/log/containers/tiller*"] 

我试过:

{{- join "," .Values.app.logfiletoexclude | quote}}

但这给了我:

exclude_path ["/var/log/containers/kube*,/var/log/containers/tiller*"] 

谢谢

双引号应在 .Values.app.logfiletoexclude 个值中转义。

values.yaml 是:

app:
  logfiletoexclude:
    - '"/var/log/containers/kube*"'
    - '"/var/log/containers/tiller*"'

_helpers.tpl 是:

{{- define "pathtoexclude" -}}
{{- join "," .Values.app.logfiletoexclude }}
{{- end -}}

最后我们有:

exclude_path ["/var/log/containers/kube*","/var/log/containers/tiller*"]

或者,以下内容也适用于我,无需额外引用输入值:

"{{- join "\",\"" .Values.myArrayField }}"

当然,这只适用于非空数组,并为空数组生成一个空的引用值。有人知道可以在这里集成一个简单的守卫吗?

Fluentd 允许在其配置 https://docs.fluentd.org/configuration/config-file#supported-data-types-for-values 中对数组使用所谓的“shorthand 语法”,这是一个用逗号分隔值的字符串,例如“value1,value2,value3”。

因此,如果您可以假设您的值中没有逗号,您就可以避免使用 '"..."' 双引号引起的头痛,只需执行以下操作:

在你的 values.yaml:

app:
  logfiletoexclude:
    - /var/log/containers/kube*
    - /var/log/containers/tiller*

在你的_helpers.tpl中:

{{- define "pathtoexclude" -}}
{{- join "," .Values.app.logfiletoexclude }}
{{- end -}}

在您的配置图中:

<source>
  @type tail
  path /var/log/containers/*.log
  exclude_path {{ template "pathtoexclude" . }}
  ...
</source>

我是这样解决的:

in values.yaml(引号无关紧要):

elements:
- first element
- "second element"
- 'third element'

_helpers.tpl中:

{{- define "mychart.commaJoinedQuotedList" -}}
{{- $list := list }}
{{- range .Values.elements }}
{{- $list = append $list (printf "\"%s\"" .) }}
{{- end }}
{{- join ", " $list }}
{{- end }}

templates/mytemplate.yaml 中:

elements: {{ include "mychart.commaJoinedQuotedList" . }}