Helm模板中的条件语句

huangapple go评论85阅读模式
英文:

Conditionals in Helm template

问题

我正在尝试使用if条件来定义一个Helm模板,但是我无法实现。JSON模板位于我的Helm图表的文件目录中,内容如下:

{
  "default_project": "{{ .Values.ss_api_ob }}",
       "roaming": {
          "xxx": "false",
          "xxx": "xxx.json",
          "xxx": "xx.json",
          "xxx": "xxx.json",
          "xxx": "correspondences.json",
         if "{{- eq .Values.myvar "es" -}}"
          "xxxx": {{ '[["xxx", "xxx"], ["xxx", "xx"], ["xx", "xxxx"], ["xx", "xx"]]' }},
          {{- else -}}
         "xxx": {{ '[["yyy", "yyy"], ["yyy", "yyy"], ["yyy", "yyy"]]' }},
         {{- end -}}
          "xxxx": "http://{{ .Values.kongService }}:9800/xxxx",
          "xxx": "http://{{ .Values.kongService }}:9800/xxxxx",
          "xxxx": "http://{{ .Values.kongService }}:9800/xxxx"
        },
}

myvar = es,所以JSON文件应该定义第一个选项。但是当我尝试安装时,我得到了一个错误,似乎有什么地方出错了:

Error: template: api/templates/configmap.yaml:7:7: executing "api/templates/configmap.yaml" at <tpl (.Files.Glob "files/*").AsConfig .>:  parse error at (api/templates/configmap.yaml:5): unclosed action

我已经检查了我的JSON模板,但是我找不到问题出在哪里。有什么想法吗?

英文:

I'm trying to define a helm template with an if conditional, but I'm not being able to achieve it. The JSON template are inside the files directory inside my helm-chart and it is like that:

{
  &quot;default_project&quot;: &quot;{{ .Values.ss_api_ob }}&quot;,
       &quot;roaming&quot;: {
          &quot;xxx&quot;: &quot;false&quot;,
          &quot;xxx&quot;: &quot;xxx.json&quot;,
          &quot;xxx&quot;: &quot;xx.json&quot;,
          &quot;xxx&quot;: &quot;xxx.json&quot;,
          &quot;xxx&quot;: &quot;correspondences.json&quot;,
         if  &quot;{{- eq .Values.myvar &quot;es&quot; -}}&quot;
          &quot;xxxx&quot;: {{ &#39;[[&quot;xxx&quot;, &quot;xxx&quot;], [&quot;xxx&quot;, &quot;xx&quot;], [&quot;xx&quot;, &quot;xxxx&quot;], [&quot;xx&quot;, &quot;xx&quot;]]&#39; }},
          {{- else -}}
         &quot;xxx&quot;: {{ &#39;[[&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;]]&#39; }},
         {{- end -}}
          &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;,
          &quot;xxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxxx&quot;,
          &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;
        },
}

myvar = es so the json file should define the first choice. But when I try to install it I'm getting an error like something is wrong:

Error: template: api/templates/configmap.yaml:7:7: executing &quot;api/templates/configmap.yaml&quot; at &lt;tpl (.Files.Glob &quot;files/*&quot;).AsConfig .&gt;:  parse error at (api/templates/configmap.yaml:5): unclosed action

I have checked my JSON template but I don't find where is the problem. Any idea?

答案1

得分: 1

你的if语句在动作之外。
动作不必要地被引用。

以下代码应该可以工作:

{
  "default_project": "{{ .Values.ss_api_ob }}",
  "roaming": {
    "xxx": "false",
    "xxx": "xxx.json",
    "xxx": "xx.json",
    "xxx": "xxx.json",
    "xxx": "correspondences.json",
    {{- if eq .Values.myvar "es" -}}
    "xxxx": [["xxx", "xxx"], ["xxx", "xx"], ["xx", "xxxx"], ["xx", "xx"]],
    {{- else -}}
    "xxx": [["yyy", "yyy"], ["yyy", "yyy"], ["yyy", "yyy"]],
    {{- end -}}
    "xxxx": "http://{{ .Values.kongService }}:9800/xxxx",
    "xxx": "http://{{ .Values.kongService }}:9800/xxxxx",
    "xxxx": "http://{{ .Values.kongService }}:9800/xxxx"
  }
}
英文:
  1. Your if is outside the action.
  2. The action is unnecessarily quoted.

The following ought to work:

{
  &quot;default_project&quot;: &quot;{{ .Values.ss_api_ob }}&quot;,
       &quot;roaming&quot;: {
          &quot;xxx&quot;: &quot;false&quot;,
          &quot;xxx&quot;: &quot;xxx.json&quot;,
          &quot;xxx&quot;: &quot;xx.json&quot;,
          &quot;xxx&quot;: &quot;xxx.json&quot;,
          &quot;xxx&quot;: &quot;correspondences.json&quot;,
          {{- if eq .Values.myvar &quot;es&quot; -}}
          &quot;xxxx&quot;: {{ `[[&quot;xxx&quot;, &quot;xxx&quot;], [&quot;xxx&quot;, &quot;xx&quot;], [&quot;xx&quot;, &quot;xxxx&quot;], [&quot;xx&quot;, &quot;xx&quot;]]` }},
          {{- else -}}
          &quot;xxx&quot;: [[&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;]],
          {{- end -}}
          &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;,
          &quot;xxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxxx&quot;,
          &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;
        },
}

huangapple
  • 本文由 发表于 2021年10月20日 19:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/69644983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定