【问题标题】:how to send "if" statement in helm array template如何在 helm 数组模板中发送“if”语句
【发布时间】:2020-10-14 05:00:44
【问题描述】:

我想要一个if exists 签入我的 helm 模板的数组对象。我的 YAML 如下所示:

servers:
- hosts: dev
  port: 443
{{- if .Values.stage.enabled -}}
- hosts: stage
  port: 443
{{- end -}}

我收到了error converting YAML to JSON: yaml: mapping values are not allowed in this context

helm 模板中是否不允许嵌套 if 条件?如果是这样,我该如何解决?

【问题讨论】:

    标签: kubernetes-helm


    【解决方案1】:

    花括号内的- 导致Go text/template 引擎吞下与其相邻的空格。如果你在这个(强烈推荐)上运行helm template,你会看到类似的输出

    servers:
    - hosts: dev
      port: 443- hosts: stage
      port: 443and then whatever comes next in the template
    

    如果您像这样将条件放在自己的行上,根据经验,在行的开头包含-(删除之前的换行符)通常效果很好,但不是在结尾行(保留下一行的缩进)。

    servers:
    - hosts: dev
      port: 443
    {{- if .Values.stage.enabled }}{{/* <-- no `-` here */}}
    - hosts: stage
      port: 443
    {{- end }}{{/* <-- no `-` here either */}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      相关资源
      最近更新 更多