【问题标题】:helm join list from values file来自值文件的 helm join 列表
【发布时间】:2022-05-13 17:36:30
【问题描述】:

我正在寻找一种解决方案,将我的 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*"] 

谢谢

【问题讨论】:

    标签: kubernetes-helm


    【解决方案1】:

    .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*"]
    

    【讨论】:

    • 你说得对,只需要转义双引号!谢谢。
    【解决方案2】:

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

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

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

    【讨论】:

    • quote 函数正好适合这种情况
    【解决方案3】:

    Fluentd 允许在其配置 https://docs.fluentd.org/configuration/config-file#supported-data-types-for-values 中对数组使用他们所谓的“速记语法”,这是一个用逗号分隔值的字符串,例如“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>
    

    【讨论】:

      【解决方案4】:

      我是这样解决的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        相关资源
        最近更新 更多