英文:
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:
{
  "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 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 "api/templates/configmap.yaml" at <tpl (.Files.Glob "files/*").AsConfig .>:  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"
  }
}
英文:
- Your 
ifis outside the action. - The action is unnecessarily quoted.
 
The following ought to work:
{
  "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"
        },
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论