将YAML转换为JSON时出错:找不到预期的密钥-管道中的错误

时间:2020-11-06 20:10:16

标签: kubernetes yaml kubernetes-helm

我的部署管道出现以下错误

Error: YAML parse error on cnhsst/templates/deployment.yaml: error converting YAML to JSON: yaml: line 38: did not find expected key

与此错误对应的yml文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "fullname" . }}
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ template "fullname" . }}
      release: "{{ .Release.Name }}"
  # We dont need a large deployment history limit as Helm keeps it's own
  # history
  revisionHistoryLimit: 2
  template:
    metadata:
      namespace: {{ .Values.namespace }}
      labels:
        app: {{ template "fullname" . }}
        release: "{{ .Release.Name }}"
      annotations:
        recreatePods: {{ randAlphaNum 8 | quote }}
    spec:
      containers:
      - name: {{ template "fullname" . }}
        image: {{ template "docker-image" . }}
        imagePullPolicy: Always
        ports:
        # The port that our container listens for HTTP requests on
        - containerPort: {{ default 8000 .Values.portOverride }}
          name: http
        
      {{- if .Values.resources }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
      {{- end }}
      {{- if and (.Values.livenessProbe) (.Values.apipod)}}
        livenessProbe:
{{ toYaml .Values.livenessProbe | indent 10 }}
      {{- end }}
      {{- if and (.Values.readinessProbe) (.Values.apipod)}}
        readinessProbe:
{{ toYaml .Values.readinessProbe | indent 10 }}
      {{- end }}
      imagePullSecrets:
      - name: regcred
       securityContext:
        runAsNonRoot: true
        runAsUser: 5000
        runAsGroup: 5000 
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - {{ template "fullname" . }}
              topologyKey: failure-domain.beta.kubernetes.io/zone

这个问题困扰了我几个小时。我浏览了许多帖子,尝试了在线工具来尝试找出语法错误,但不幸的是没有运气。如果有人能够指出问题,那将是非常棒的事情。

1 个答案:

答案 0 :(得分:2)

您可以在regcred下看到不匹配的缩进:

      imagePullSecrets:
      - name: regcred
      # <-- indented "-"
      #VVV not indented
       securityContext:
        runAsNonRoot: true

很幸运,它是输出YAML中的第38行

$ helm template --debug my-chart . 2>&1| sed -e '1,/^apiVersion:/d' | sed -ne 38p
       securityContext: