Helm部署ConfigMap遇YAML语法错误:找不到预期冒号
问题:Helm部署ConfigMap时出现YAML语法错误,提示"could not find expected ':'"
错误现象
执行Helm模板渲染时,报错提示:
Error: YAML parse error on sonarqube/templates/sonarscanner-cronjob.yaml: error converting YAML to JSON: yaml: line 21: could not find expected ':'
相关ConfigMap配置
apiVersion: v1 kind: ConfigMap metadata: name: {{ .Values.sonarscanner.name }}-{{ .Values.sonarscanner.label }} namespace: {{ .Release.Name }} data: SONAR_HOST_URL: "-Dsonar.host.url=http://{{ .Values.sonarqube.name }}-{{ .Values.service.label }}.{{ .Values.namespace }}:{{ .Values.sonarqube.containerPort }}" SONAR_PROJECT_KEY: "-Dsonar.projectKey={{ .Values.sonarqube.project }}" CODE_PATH: "-Dsonar.sources=." CODE_REPO_URL: {{ .Values.repo.url }} CODE_REPO_NAME: {{ .Values.repo.name }} SONAR_PROJECT_NAME: "-Dsonar.projectName={{ .Values.sonarqube.project }}" RU: {{ .Values.code.httpU }} RT: {{ .Values.code.httpT }} SONAR_TOKEN: {{ .Values.code.sonarT }} repo_checkout.sh: |- #!/bin/bash set -xe apt-get update && apt install dnsutils iputils-ping git curl -yq #Containers that fail to resolve repo url can use below step. repo_url=$(nslookup $CODE_REPO_URL | grep Non -A 2 | grep Name | cut -d: -f2) repo_ip=$(nslookup $CODE_REPO_URL | grep Non -A 2 | grep Address | cut -d: -f2) if grep $repo_url /etc/hosts; then echo "git dns entry exists locally" else echo "Adding dns entry for git inside container" echo $repo_ip $repo_url >> /etc/hosts fi cat /etc/hosts git clone "https://$RU:$RT@$CODE_REPO_URL/r/a/$CODE_REPO_NAME" && (cd "$CODE_REPO_NAME" && mkdir -p .git/hooks && curl -Lo `git rev-parse --git-dir`/hooks/commit-msg https://$RU:$RT@$CODE_REPO_URL/r/tools/hooks/commit-msg; chmod +x `git rev-parse --git-dir`/hooks/commit-msg)
Helm执行报错输出
in ./helmfile.yaml: command "/usr/local/bin/helm" exited with non-zero status: PATH: /usr/local/bin/helm ARGS: 0: helm (4 bytes) 1: template (8 bytes) 2: sonarqube (9 bytes) 3: /var/folders/nf/7ghp7kvd76n7jyzdc34kfkqm0000gp/T/tmp.X5Vhyiol (61 bytes) 4: --namespace (11 bytes) 5: sonarqube (9 bytes) 6: --values (8 bytes) 7: /var/folders/nf/7ghp7kvd76n7jyzdc34kfkqm0000gp/T/helmfile1184228953/sonarqube-sonarqube-values-c8cb44f76 (104 bytes) 8: --values (8 bytes) 9: /var/folders/nf/7ghp7kvd76n7jyzdc34kfkqm0000gp/T/helmfile2806006960/sonarqube-sonarqube-values-7c87b8b85f (105 bytes) 10: --include-crds (14 bytes) 11: --kube-version=1.13.5 (21 bytes) ERROR: exit status 1 EXIT STATUS 1 STDERR: Error: YAML parse error on sonarqube/templates/sonarscanner-cronjob.yaml: error converting YAML to JSON: yaml: line 21: could not find expected ':' Use --debug flag to render out invalid YAML COMBINED OUTPUT: Error: YAML parse error on sonarqube/templates/sonarscanner-cronjob.yaml: error converting YAML to JSON: yaml: line 21: could not find expected ':' Use --debug flag to render out invalid YAML task failed: command "helmfile --selector name=sonarqube --quiet template --include-crds --args --kube-version=1.13.5" failed: exit status 1
解决提示
- 替换HTML转义引号:原配置中使用的
"是HTML转义字符,YAML解析器无法识别,需全部替换为标准双引号"。 - 为动态变量值添加引号:
CODE_REPO_URL、CODE_REPO_NAME、RU、RT、SONAR_TOKEN这些从Helm变量获取的值,可能包含冒号、斜杠等特殊字符,必须用双引号包裹,避免YAML解析时误判为映射结构。修正后示例:CODE_REPO_URL: "{{ .Values.repo.url }}" CODE_REPO_NAME: "{{ .Values.repo.name }}" RU: "{{ .Values.code.httpU }}" RT: "{{ .Values.code.httpT }}" SONAR_TOKEN: "{{ .Values.code.sonarT }}" - 修正脚本shebang:将脚本开头的
#!bin/bash改为#!/bin/bash,缺少根路径/会导致脚本无法被正确执行。 - 修正脚本变量引用:脚本中
nslookup CODE_REPO_URL需改为nslookup $CODE_REPO_URL,git clone中的RT需改为$RT,否则无法正确引用环境变量。
内容的提问来源于stack exchange,提问作者AhmFM
相关产品推荐
相关产品推荐

