kubernetes Helm将YAML转换为JSON时出错:yaml:第20行:找不到预期的键

wvyml7n5  于 2023-04-05  发布在  Kubernetes
关注(0)|答案(3)|浏览(303)

我真的不知道这里的错误是什么,是一个简单的helm部署a _helpers.tpl,它没有意义,可能是一个愚蠢的错误,代码:
deploy.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
{{ include "metadata.name" . }}-deploy
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        vars: {{- include "envs.var" .Values.secret.data }}

_助手.tpl

{{- define "envs.var"}}
{{- range $key := . }}
- name: {{ $key | upper | quote}}
  valueFrom:
    secretKeyRef:
      key: {{ $key | lower }}
      name: {{ $key }}-auth
{{- end }}
{{- end }}

values.yaml

secret:
  data:
    username: root
    password: test

错误

Error: YAML parse error on mychart/templates/deploy.yaml: error converting YAML to JSON: yaml: line 21: did not find expected key
fv2wmkja

fv2wmkja1#

这里这个问题的发生是因为缩进。你可以通过更新

env: {{- include "envs.var" .Values.secret.data | nindent 12  }}
7cwmlq89

7cwmlq892#

解决这类问题最简单的方法就是使用工具。
这些大多是缩进问题,使用正确的工具可以很容易地解决

npm install -g yaml-lint

yaml-lint就是这样一种工具

PS E:\vsc-workspaces\grafana-1> yamllint .\grafana.yaml
× YAML Lint failed for C:/Users/mnadeem6/vsc-workspaces/grafana-1/grafana.yaml
× bad indentation of a mapping entry at line 137, column 11:
          restartPolicy: Always
          ^
PS E:\vsc-workspaces\grafana-1> yamllint .\grafana.yaml
√ YAML Lint successful.
sg2wtvxw

sg2wtvxw3#

错误:解析.\email-service-values.yaml失败:将YAML转换为JSON时出错:yaml:第14行:找不到预期的“:”
我在练习“Techworld with Nana”Bootcamp的用例时遇到了上述错误。在我的情况下,缩进是问题所在。当我提到3(或)更多哈希值(---)来分隔部署和服务相关的键值时,出现了这个错误。我的问题解决了,一旦我删除了这些哈希值,只在部署和服务相关参数之间保留了一个空行。

appName: emailservice
appReplicas: 2
appImage: gcr.io/google-samples/microservices-demo/emailservice
appVersion: v0.2.3
containerPort: 8080
containerEnvVars: 
- name: PORT
  value: "8080"
- name: DISABLE_TRACING
  value: "1"
- name: DISABLE_PROFILER
  value: "1"
---
servicePort: 5000
serviceType: ClusterIP

相关问题