helm - 将 YAML 转换为 JSON 时出错:yaml:第 29 行:在此上下文中不允许映射值

helm - error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this context

在同一 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" }}

两种方式-一种来自关键字模板(下面代码的最后一行)

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

{{include "chart.labels" . | indent 8 }}

Error: YAML parse error on chart/templates/deployment.yml: error converting YAML to JSON: yaml: line 27: did not find expected key helm.go:81: [debug] error converting YAML to JSON: yaml: line 27: did not find expected key YAML parse error on 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

Error: YAML parse error on chart/templates/deployment.yml: error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this context helm.go:81: [debug] error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this context YAML parse error on 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

我在这里错过了什么?

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

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

注意下面的 chart.labels 定义中没有双 space。

以下作品:

{{- 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 }}