Helm模板中的条件语句

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

Conditionals in Helm template

问题

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

  1. {
  2. "default_project": "{{ .Values.ss_api_ob }}",
  3. "roaming": {
  4. "xxx": "false",
  5. "xxx": "xxx.json",
  6. "xxx": "xx.json",
  7. "xxx": "xxx.json",
  8. "xxx": "correspondences.json",
  9. if "{{- eq .Values.myvar "es" -}}"
  10. "xxxx": {{ '[["xxx", "xxx"], ["xxx", "xx"], ["xx", "xxxx"], ["xx", "xx"]]' }},
  11. {{- else -}}
  12. "xxx": {{ '[["yyy", "yyy"], ["yyy", "yyy"], ["yyy", "yyy"]]' }},
  13. {{- end -}}
  14. "xxxx": "http://{{ .Values.kongService }}:9800/xxxx",
  15. "xxx": "http://{{ .Values.kongService }}:9800/xxxxx",
  16. "xxxx": "http://{{ .Values.kongService }}:9800/xxxx"
  17. },
  18. }

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

  1. 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:

  1. {
  2. &quot;default_project&quot;: &quot;{{ .Values.ss_api_ob }}&quot;,
  3. &quot;roaming&quot;: {
  4. &quot;xxx&quot;: &quot;false&quot;,
  5. &quot;xxx&quot;: &quot;xxx.json&quot;,
  6. &quot;xxx&quot;: &quot;xx.json&quot;,
  7. &quot;xxx&quot;: &quot;xxx.json&quot;,
  8. &quot;xxx&quot;: &quot;correspondences.json&quot;,
  9. if &quot;{{- eq .Values.myvar &quot;es&quot; -}}&quot;
  10. &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; }},
  11. {{- else -}}
  12. &quot;xxx&quot;: {{ &#39;[[&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;]]&#39; }},
  13. {{- end -}}
  14. &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;,
  15. &quot;xxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxxx&quot;,
  16. &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;
  17. },
  18. }

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:

  1. 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语句在动作之外。
动作不必要地被引用。

以下代码应该可以工作:

  1. {
  2. "default_project": "{{ .Values.ss_api_ob }}",
  3. "roaming": {
  4. "xxx": "false",
  5. "xxx": "xxx.json",
  6. "xxx": "xx.json",
  7. "xxx": "xxx.json",
  8. "xxx": "correspondences.json",
  9. {{- if eq .Values.myvar "es" -}}
  10. "xxxx": [["xxx", "xxx"], ["xxx", "xx"], ["xx", "xxxx"], ["xx", "xx"]],
  11. {{- else -}}
  12. "xxx": [["yyy", "yyy"], ["yyy", "yyy"], ["yyy", "yyy"]],
  13. {{- end -}}
  14. "xxxx": "http://{{ .Values.kongService }}:9800/xxxx",
  15. "xxx": "http://{{ .Values.kongService }}:9800/xxxxx",
  16. "xxxx": "http://{{ .Values.kongService }}:9800/xxxx"
  17. }
  18. }
英文:
  1. Your if is outside the action.
  2. The action is unnecessarily quoted.

The following ought to work:

  1. {
  2. &quot;default_project&quot;: &quot;{{ .Values.ss_api_ob }}&quot;,
  3. &quot;roaming&quot;: {
  4. &quot;xxx&quot;: &quot;false&quot;,
  5. &quot;xxx&quot;: &quot;xxx.json&quot;,
  6. &quot;xxx&quot;: &quot;xx.json&quot;,
  7. &quot;xxx&quot;: &quot;xxx.json&quot;,
  8. &quot;xxx&quot;: &quot;correspondences.json&quot;,
  9. {{- if eq .Values.myvar &quot;es&quot; -}}
  10. &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;]]` }},
  11. {{- else -}}
  12. &quot;xxx&quot;: [[&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;], [&quot;yyy&quot;, &quot;yyy&quot;]],
  13. {{- end -}}
  14. &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;,
  15. &quot;xxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxxx&quot;,
  16. &quot;xxxx&quot;: &quot;http://{{ .Values.kongService }}:9800/xxxx&quot;
  17. },
  18. }

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:

确定