Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error converting YAML to JSON could not find expected ':' #2004

Closed
Rurouni opened this issue Feb 21, 2017 · 21 comments
Closed

Error converting YAML to JSON could not find expected ':' #2004

Rurouni opened this issue Feb 21, 2017 · 21 comments

Comments

@Rurouni
Copy link

Rurouni commented Feb 21, 2017

I have just run into very curious bug with (.Files.Glob "configOverrides/*").AsConfig
Suddenly one of my services charts started to fail on helm upgrade with obscure

Error: UPGRADE FAILED: YAML parse error on web/templates/config.yaml: error converting YAML to JSON: yaml: line 12: could not find expected ':'

But the weird thing was that other almost identical chart was succeeding.
Both of them have pretty standard

kind: ConfigMap
apiVersion: v1
metadata:
  name: {{.Chart.Name}}
  labels:
    app: {{.Values.Application}}
    release: "{{.Release.Name}}"
    component: {{.Chart.Name}}
    version: "{{.Chart.Version}}"
data:
  {{ (.Files.Glob "configOverrides/*").AsConfig | indent 2 }}

both of them incude appconfig.json file that is valid json and encoded with UTF-8 without BOM
and the only difference in appconfig.json file that I was able to find was one "space" in it.
After some testing: Even having config file as simple as

{<LF>
  "x": "x"<LF>
}

is failing with error above but as soon as I add space at the end of if

{<LF>
  "x": "x" <LF>
}

it works fine...
also it works fine if config has no space but <CRLF> instead of <LF>

Env: Helm version 2.2 running in ubuntu 16.04 based docker container

@technosophos
Copy link
Member

That is a peculiar bug. With the problematic JSON, have you run it through a JSON linter just to be sure there isn't some hidden character causing a problem? Otherwise, it sounds like we might have an error in the upstream YAML parser.

@Rurouni
Copy link
Author

Rurouni commented Feb 23, 2017

It worked fine in online linters I tried. I thought initially that it was related to BOM, but both files(working and not working) did not have it. I have attached minimal chart that is causing it for me:
chart.zip

running
helm upgrade dev-seed-worker /app/bin/worker/kube --install --debug --namespace seed-dev --dry-run
results in

Created tunnel using local port: '37337'
SERVER: "localhost:37337"
Release "dev-seed-worker" does not exist. Installing it now.
CHART PATH: /app/bin/worker/kube
Error: YAML parse error on worker/templates/config.yaml: error converting YAML to JSON: yaml: line 12: could not find expected ':'

---
# Source: worker/templates/config.yaml
kind: ConfigMap
apiVersion: v1
metadata:
  name: worker
  labels:
    app: seed
    release: "dev-seed-worker"
    component: worker
    version: "master_20170221.1_1"
data:
    appsettings.json: |
    {
      "x":"x"
    }

if you add space it seems to produce completely different result

Created tunnel using local port: '37533'
SERVER: "localhost:37533"
Release "dev-seed-worker" does not exist. Installing it now.
CHART PATH: /app/bin/worker/kube
NAME:   dev-seed-worker
REVISION: 1
RELEASED: Thu Feb 23 16:52:45 2017
CHART: worker-master_20170221.1_1
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
Application: seed

HOOKS:
MANIFEST:

---
# Source: worker/templates/config.yaml
kind: ConfigMap
apiVersion: v1
metadata:
  name: worker
  labels:
    app: seed
    release: "dev-seed-worker"
    component: worker
    version: "master_20170221.1_1"
data:
    appsettings.json: "{\n  \"x\":\"x\"\n} \n"

@thomastaylor312
Copy link
Contributor

@Rurouni Are you still having issues with this?

@dgarlitt
Copy link

dgarlitt commented Jun 2, 2017

I'm having a similar issue. When I have a ConfigMap template defined as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-{{ .Chart.Name }}-configs
data:
  config.properties: |-
  {{ .Files.Get "config.properties" | indent 2 }}

And, config.properties contains multiple lines:

property1=some property value
property2=some other property value
property3=yet another property value

I receive the following error: Error: UPGRADE FAILED: YAML parse error on my-chart/templates/my-configs.yaml: error converting YAML to JSON: yaml: line 8: could not find expected ':'

However, it works fine if config.properties contains only one line, as follows:

property1=some property value

@dgarlitt
Copy link

dgarlitt commented Jun 2, 2017

For what it's worth, I found that I no longer get an error with multi-line files if I change my ConfigMap template to the following:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-{{ .Chart.Name }}-configs
data:
  config.properties: |-
{{ .Files.Get "config.properties" | indent 4 }}

Notice that {{ .Files.Get "config.properties" | indent 4 }} is outdented to the same level as the data key.

@Rurouni
Copy link
Author

Rurouni commented Jun 24, 2017

I haven't seen it for a while, so I am closing this

@Rurouni Rurouni closed this as completed Jun 24, 2017
@ravindranathakila
Copy link

Happens due to a minor indent (1 instead of two, or two instead of four), in a wrong place.

@hansharhoff
Copy link

Is there a way to have helm output the bad yaml so we can debug?

@bacongobbler
Copy link
Member

https://github.com/technosophos/helm-template

@thomastaylor312
Copy link
Contributor

☝️ Which has been merged into master and is coming out in 2.7

@hmacias
Copy link

hmacias commented Apr 5, 2018

Thank you @ravindranathakila. Indenting data with 4 spaces worked for me (in my case it required 4 spaces)!

@fracasula
Copy link

The error message in my case was quite cryptic, it was just complaining about a "line 8" without saying what file to look into. Like this basically: Error: UPGRADE FAILED: yaml: line 8: could not find expected ':'

I solved by running helm lint on it which was a lot more verbose in my case and it helped me identify the "bad" line.

The output with the lint was:

$ helm lint reducers
==> Linting reducers
[INFO] Chart.yaml: icon is recommended
[ERROR] templates/: render error in "reducers/templates/configmap.yaml": template: reducers/templates/configmap.yaml:24:27: executing "reducers/templates/configmap.yaml" at <.Values.global.kafka...>: can't evaluate field kafkaUrl in type interface {}

Error: 1 chart(s) linted, 1 chart(s) failed

Which of course has nothing to do with any line 8 but it definitely highlighted the right problem 👍

@GaryCarneiro
Copy link

GaryCarneiro commented Feb 8, 2019

I got this error while writing config maps object using Helm . This error had nothing to do with YAML but more with quotations

I was getting following

 error converting YAML to JSON: yaml: line 12: did not find expected key

Debugging:

I ran helm in debug and dry-run mode to see what was the property file format getting outputted

helm install --debug --name my-release-name ./my-superawesome-chart --dry-run

Solution:

Use quote method in the templates/your-config-map-definition.yaml

$ cat templates/your-config-map-definition.yaml

 ---
apiVersion: v1
kind: ConfigMap
metadata:
 name: my-superawesome-app
data:
 application.properties: {{ .Files.Get "application.properties" | quote }}

@pc-rshetty
Copy link

this seems to work , but how do i make application.properties per environment ? example dev has different contents , qa has different and prod has different

@iamamjad
Copy link

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql8
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql8
spec:
containers:

  • image: mysql:8.0
    name: mysql
    imagePullPolicy: Never
    env:
  • name: MYSQL_ROOT_PASSWORD
    value: .sweetpwd.
  • name: MYSQL_DATABASE
    value: my_db
  • name: MYSQL_USER
    value: db_user
  • name: MYSQL_PASSWORD
    value: .mypwd
    args: ["--default-authentication-plugin=mysql_native_password"]
    ports:
    • containerPort: 3306
      name: mysql8

@abdennour
Copy link

It happen when i didn't add metadata section in the nested template object, namely with cronjob (spec > jobTemplate > spec > template > spec) :

The error appeared when it was :

kind: CronJob
metadata:
  name: scheduler-{{ include "laravel.fullname" . }}
  labels:
    {{- include "laravel.labels" . | nindent 4 }}
spec:
  schedule: "{{ .Values.scheduler.schedule }}"
  jobTemplate:
    spec:
      template:
        spec:
          # containers: ...

And the error disappears when it is :

kind: CronJob
metadata:
  name: scheduler-{{ include "laravel.fullname" . }}
  labels:
    {{- include "laravel.labels" . | nindent 4 }}
spec:
  schedule: "{{ .Values.scheduler.schedule }}"
  jobTemplate:
    metadata:
      labels:
        {{- include "laravel.labels" . | nindent 8 }}
    spec:
      template:
        metadata:
          labels:
            {{- include "laravel.labels" . | nindent 12 }}
        spec:
          # containers: ...

@NitsujY
Copy link

NitsujY commented Nov 2, 2021

I don't know whether this helps

test.json

{
    "asdfasdfasdf": "asd"
}

This return error converting YAML to JSON: yaml: line 8: did not find expected key

apiVersion: v1
kind: ConfigMap
metadata:
  name: profile
data:
  token: |-
    {{ .Files.Get "files/test.json" }}

This return same error

apiVersion: v1
kind: ConfigMap
metadata:
  name: profile
data:
  token: |-
  {{ .Files.Get "files/test.json" | indent 2}}

This works

apiVersion: v1
kind: ConfigMap
metadata:
  name: profile
data:
  token: |-
{{ .Files.Get "files/test.json" | indent 4}}

@maugsbur
Copy link

I had a similar problem with a multiline yaml.

multiline yaml:

my_data: >-
  "some"
  "string"

This returned unable to parse YAML: error converting YAML to JSON:

data:
  token: "{{ .File.my_data }}"

Then taking out the quotes, it worked:

data:
  token: {{ .File.my_data }}

@vignesh-desk
Copy link

vignesh-desk commented Jun 17, 2022

$ cat templates/config-map.yaml


apiVersion: v1
kind: ConfigMap
metadata:
  name: my-schooltest-app
data:
  config_dev.json: |-
{{ .Files.Get "conf/config_dev.json" | indent 4}}
  config_qa.json: |-
{{ .Files.Get "conf/config_qa.json" | indent 4}}    

config_dev.json (same as config_qa.json)

{
  "students": [
    {
      "name": "test1",
      "proffesion": "Engineer",
      "subjectCodes": [
        "SOCIAL",
        "SCIENCE",
        "MATHS"
      ]
    },
    {
      "name": "test2",
      "proffesion": "Engineer",
      "subjectCodes": [
        "ENGLISH",
        "TAMIL"
      ]
    }
  ],
  "allowedSubjects": [
    "SOCIAL",
    "SCIENCE",
    "MATHS",
    "ENGLISH",
	"TAMIL"
  ],
  "school": "trm",
  "headmaster": [
    "Test5"
  ]
}

I am having an issue in update the config file using the below command

kubectl apply -f helm/templates/config-map.yaml -n $NAMESPACE

**

error: error parsing helm/templates/config-map.yaml: error converting YAML to JSON: yaml: line 8: could not find expected ':'

**

Note: When I do the helm install deployment, it works fine. But my requirement is update the configmap without restarting the pod
Tried : is outdented to the same level as the data key as well

@TheInfinity007
Copy link

In my case, I had JSON stringified config in Azure Key Vault. So, during deployment in the helm chart step, it gave me the same error
error converting JSON to YAML: yaml: did not find expected ',' or '}'

As a workaround, I base64 encoded the JSON stringified config and stored in key vault. This worked for me.

Hope someone will find this useful.

@akshaypandhare17
Copy link

I don't know whether this helps

test.json

{
    "asdfasdfasdf": "asd"
}

This return error converting YAML to JSON: yaml: line 8: did not find expected key

apiVersion: v1
kind: ConfigMap
metadata:
  name: profile
data:
  token: |-
    {{ .Files.Get "files/test.json" }}

This return same error

apiVersion: v1
kind: ConfigMap
metadata:
  name: profile
data:
  token: |-
  {{ .Files.Get "files/test.json" | indent 2}}

This works

apiVersion: v1
kind: ConfigMap
metadata:
  name: profile
data:
  token: |-
{{ .Files.Get "files/test.json" | indent 4}}

This workds as expected if you are passing json content in helm values.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests