【问题标题】:helm - error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this contexthelm - 将 YAML 转换为 JSON 时出错:yaml:第 29 行:在此上下文中不允许映射值
【发布时间】:2021-06-29 12:00:04
【问题描述】:

在同一deployment.yml文件顶部定义的临时标签-

{{- define "chart.labels" }} 
  version: v1.0
  method: http
  internet: enabled
{{- end }}

我在模板文件夹中有deployment.yml 文件-

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1-deployment
  namespace: {{ .Values.global.namespace }}
  labels:
    app: app1
    type: microservice1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app1
      type: microservice1
  strategy:
    type: {{ .Values.global.strategytype }}
  template:
    metadata:
      labels:
        app: app1
        type: microservice1
        {{- template "chart.labels" }}

两种方式——一种来自关键字template(以下代码的最后一行)

第二个来自 include 关键字我正在尝试调用模板。

{{include "chart.labels" . | indent 8 }}
  • 我收到 error(当我使用关键字 template 调用时 模板)。

错误:chart/templates/deployment.yml 上的 YAML 解析错误:错误 将 YAML 转换为 JSON:yaml:第 27 行:未找到预期的密钥 helm.go:81: [debug] 将 YAML 转换为 JSON 时出错:yaml: line 27: did 找不到预期的关键 YAML 解析错误 图表/模板/deployment.yml helm.sh/helm/v3/pkg/releaseutil.(*manifestFile).sort helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:146 helm.sh/helm/v3/pkg/releaseutil.SortManifests helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:106 helm.sh/helm/v3/pkg/action.(*Configuration).renderResources helm.sh/helm/v3/pkg/action/action.go:165 helm.sh/helm/v3/pkg/action.(*Install).Run helm.sh/helm/v3/pkg/action/install.go:247

  • 得到另一个 error(当我使用 Include 关键字调用 模板)

错误:chart/templates/deployment.yml 上的 YAML 解析错误:错误 将 YAML 转换为 JSON:yaml:第 29 行:不允许映射值 在这种情况下 helm.go:81: [debug] 将 YAML 转换为 JSON 时出错: yaml:第 29 行:在此上下文 YAML 中不允许映射值 chart/templates/deployment.yml 上的解析错误 helm.sh/helm/v3/pkg/releaseutil.(*manifestFile).sort helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:146 helm.sh/helm/v3/pkg/releaseutil.SortManifests helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:106 helm.sh/helm/v3/pkg/action.(*Configuration).renderResources helm.sh/helm/v3/pkg/action/action.go:165 helm.sh/helm/v3/pkg/action.(*Install).Run helm.sh/helm/v3/pkg/action/install.go:247 main.runInstall

我在这里错过了什么?

【问题讨论】:

  • template --debug 查看渲染文件

标签: kubernetes yaml kubernetes-helm helmfile


【解决方案1】:

您需要遵循合理的缩进。你有:

{{- define "chart.labels" }} 
  version: v1.0
  method: http
  internet: enabled
{{- end }}

请注意,下面的chart.labels 定义中没有双空格。

以下工作:

{{- define "chart.labels" }} 
version: v1.0
method: http
internet: enabled
{{- end }}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "test.fullname" . }}
  labels:
    {{- include "test.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "test.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
      {{- include "test.selectorLabels" . | nindent 8 }}
      {{include "chart.labels" . | nindent 8 }}

编辑:或仅更改 nindent 以匹配模板元中的 chart.labels,如下所示:

{{include "chart.labels" . | nindent 6 }}

【讨论】:

  • 这是缩进问题。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-07-23
  • 2019-09-12
  • 1970-01-01
  • 2020-04-21
  • 2021-06-04
  • 1970-01-01
  • 2020-12-01
  • 2023-03-16
相关资源
最近更新 更多