Helm默认值引发错误将YAML转换为JSON。

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

Helm default value throws error converting YAML to JSON

问题

以下是您要翻译的内容:

我有一个基本的Kubernetes Helm模板,如下所示

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
data:
  config.tpl: |
        {{- default ((tpl .Values.configTpl .) | indent 4)  (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

和values.yml文件

configTpl: |
      {{ x=8gwifi.org }}

当我应用Helm图表时,它抛出错误

❯ helm upgrade  mychart . --namespace=test --create-namespace --debug
upgrade.go:142: [debug] preparing upgrade for mychart
Error: UPGRADE FAILED: YAML parse error on 8gwifi.org-test/templates/configmap-logfilegen.yaml: error converting YAML to JSON: yaml: line 11: did not find expected comment or line break
helm.go:84: [debug] error converting YAML to JSON: yaml: line 11: did not find expected comment or line break

我尝试了不同的配置

config.tpl: |
        {{- default (tpl .Values.configTpl . | indent 4) (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

仍然导致相同的错误,是否有一种方法可以指定配置值,如果没有传递则使用硬编码的值

我确信这是一个YAML语法问题,但无法弄清楚,已经检查了所有情况

根据David的建议

模板调试显示了这个

data:
  config.tpl: |-
              x := 8gwifi.org
   y := "functions"

我可以清楚地看到y没有缩进,导致YAML语法错误,不确定如何修复这个

这是更新后的定义

data:
  config.tpl: |-
        {{ (tpl .Values.configTpl . | indent 4)  | default (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

values.yml

configTpl: |
   x := "8gwifi.org"
   y := "function"   

注意:我已经将原文中的引号和标点符号进行了修正,以使其更符合YAML语法。

英文:

I have a basic kubernetes helm template like below

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
data:
  config.tpl: |
{{- default ((tpl .Values.configTpl .) | indent 4)  (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

and the values.yml file

configTpl: |
   {{ x=8gwifi.org }}

When i apply the helm chart it throw me an error

❯ helm upgrade  mychart . --namespace=test --create-namespace --debug
upgrade.go:142: [debug] preparing upgrade for mychart
Error: UPGRADE FAILED: YAML parse error on 8gwifi.org-test/templates/configmap-logfilegen.yaml: error converting YAML to JSON: yaml: line 11: did not find expected comment or line break
helm.go:84: [debug] error converting YAML to JSON: yaml: line 11: did not find expected comment or line break

I tried different configuration

config.tpl: |
    {{- default (tpl .Values.configTpl . | indent 4) (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

still resulting in same error, Is there a way to specify a config value if none is passed then used the hardcoded one

I'm sure it's an YAML syntx issue couldn't figure it out checked all cases

Based on David suggestion

Template debug is showing this

data:
  config.tpl: |-
       x := 8gwifi.org
   y := "functions"

I can cleary see y is not indent and throwing YAML syntax error, not sure how to fix this

This is the updated definition

data:
  config.tpl: |-
    {{ (tpl .Values.configTpl . | indent 4)  | default (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

values.yml

configTpl: |
   x := "8gwifi.org"
   y := "function"

答案1

得分: 1

You're hitting problems with whitespace in the first line of the block scalar. You should check two things:

  1. The template block containing indent must not itself be indented, it must start at the first column of its line; and
  2. The template block containing indent must not have a - inside the opening curly braces.
{{- $configTpl := .Values.configTpl | default (.Files.Get "tiles/config.tpl") }}
  config.tpl: |
{{ tpl $configTpl . | indent 4 }}

The templating language isn't really aware of YAML syntax as it runs. If you have spaces in front of the indent line, they will get emitted, and then indent adds its own leading space, resulting in the last output you get where the first line is indented extra. The - whitespace control marker will also consume the preceding newline, resulting in the first line of the output being on the same line as the YAML block scalar marker.

英文:

You're hitting problems with whitespace in the first line of the block scalar. You should check two things:

  1. The template block containing indent must not itself be indented, it must start at the first column of its line; and
  2. The template block containing indent must not have a - inside the opening curly braces.
{{- $configTpl := .Values.configTpl | default (.Files.Get "tiles/config.tpl") }}
  config.tpl: |
{{ tpl $configTpl . | indent 4 }}

The templating language isn't really aware of YAML syntax as it runs. If you have spaces in front of the indent line, they will get emitted, and then indent adds its own leading space, resulting in the last output you get where the first line is indented extra. The - whitespace control marker will also consume the preceding newline, resulting in the first line of the output being on the same line as the YAML block scalar marker.

huangapple
  • 本文由 发表于 2023年2月14日 02:38:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439985.html
匿名

发表评论

匿名网友

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

确定