Westporch
Westporch

Reputation: 817

mapping values are not allowed in this context

I'm a novice regarding YAML format and kubernetes.

The following is a dep_prom.yml file.

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    name: prometheus-deployment
  name: prometheus
  #namespace: prometheus
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - image: prom/prometheus:master
        name: prometheus
        command:
        - "/bin/prometheus"
        args:
        - "-config.file=/etc/prometheus/prometheus.yml"
        - "-storage.local.path=/prometheus"
        - "-storage.local.retention=24h"
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - mountPath: "/prometheus"
          name: data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 2500Mi
      volumes:
      - name: data
        hostPath:
          path: /data/prometheus
      - name: config-volume
        configMap:
          name: prometheus
      nodeSelector: westporch-kubeminion-1
        kubernetes.io/hostname: 10.0.24.52
---

However... When I executed kubectl create -f dep_prom.yml

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

Line 47 is nodeSelector: westporch-kubeminion-1

I think that YAML file format is normal.

What is causing this error?

Upvotes: 62

Views: 256192

Answers (12)

Rahul Pal
Rahul Pal

Reputation: 11

line numbers in helm lint/template errors are misleading. reported line numbers could be way off the real source of problem as generated yaml would have difference in lines from the template.

using "helm lint/template" with --debug spits out the generated yaml and allows to pin point to the exact line causing the issue.

and from my personal experience, "helm lint" at times didn't print the generated output with --debug but, "helm template" did.

Upvotes: 0

profimedica
profimedica

Reputation: 2830

If you copy/paste, make sure the dash - is the expected one. In my case, it was a different character.

This is fine:

  volumes:
    - name: docker-data

This is wrong:

  volumes:
    —name: docker-data

Upvotes: 2

Gaurav Singh
Gaurav Singh

Reputation: 31

In my case, I discovered that this is a standard indentation error that goes away if your YAML has proper indentations.

Upvotes: 3

Miguel Reyes
Miguel Reyes

Reputation: 2842

In my case the issue came down to the indentation (format) of the YAML file. I was being pointed to a line that had nothing to do with the error. I had to compare the file with another project I had. The tool at https://www.kubeval.com was useful for verifying that I had found and fixed the problem.

Upvotes: 5

Simon Elms
Simon Elms

Reputation: 19628

Not a direct answer to the question but it may help someone else getting the same error message:

In my case I was doing token substitution in a build pipeline, to set the Docker image tag in the YAML file. And I was reading the value to substitute incorrectly so it was set to an empty string.

So instead of the substitution ending up like:

my_image_name:1.0.0

what I was actually getting was:

my_image_name:

Which was being interpreted as a YAML map or dictionary key without a value, hence the error message.

Upvotes: 3

Suhas Chikkanna
Suhas Chikkanna

Reputation: 1510

Most of the time when you get an error like this (speaking in general and meaningful terms) it is either because of :-

1). A syntax error (in your case it is not) in the yaml file. Use a YAML linter to be sure that your syntax is correct. (Thanks to @rtmy's answer, I now include YAML Linter here.)

2). Or like the error says "mapping values are not allowed in this context". It means that the keys/values you have used in the yaml, may be syntactically right but not semantically.

For example, in you case the context is "deployment" which belongs to "apiversion: extensions/v1beta1", and it expects the node selector to be like below:-

nodeSelector:
  kubernetes.io/hostname: 10.0.24.52

Ideally, you should never use IP like above but a key-value pair like this:- topologyKey: failure-domain.beta.kubernetes.io/zone OR in your case it can be something like this:- kubernetes.io/hostname: zone-a-node

Upvotes: 8

craftsmannadeem
craftsmannadeem

Reputation: 2933

There are couple of issues in the yaml file as mentioned by most, with yaml files normally it gets messy to identify the issue,

fortunately it can be identified easily with tools like yaml lint and you may not require help from the community.

Install it

npm install -g yaml-lint

Here is how you can validate

E:\>yamllint docker-compose.yaml
√ YAML Lint successful.

Upvotes: 3

Radu Gabriel
Radu Gabriel

Reputation: 3201

Sometimes in yaml files if you use special chars like : you need to put entire line in single quotes '

Check out this example from gitlab docs https://docs.gitlab.com/ee/ci/yaml/README.html#script

Sometimes, script commands must be wrapped in single or double quotes. For example, commands that contain a colon (:) must be wrapped in single quotes ('). The YAML parser needs to interpret the text as a string rather than a “key: value” pair.* For example, this script uses a colon:*

job:
  script:
    - curl --request POST --header 'Content-Type: application/json' "https://gitlab/api/v4/projects"

To be considered valid YAML, you must wrap the entire command in single quotes. If the command already uses single quotes, you should change them to double quotes (") if possible:

job:
  script:
    - 'curl --request POST --header "Content-Type: application/json" "https://gitlab/api/v4/projects"'

Upvotes: 9

Rotem jackoby
Rotem jackoby

Reputation: 22068

In my case, the error reason for the error was an empty first line in the yaml.

When encountering this error - I strongly suggest to paste the yaml in YAML Linters (for example this one) that in some cases help to identify the problem more quickly.

Upvotes: 12

Anthon
Anthon

Reputation: 76578

You indicate you think the YAML format is normal, but it is not. This is a YAML error caused by the line at the end of the first document, starting with kubernetes.io/hostname being indented relative to the one before it. Because these are both key-value pair lines (i.e. item pairs of a block-style mapping) instead of

      nodeSelector: westporch-kubeminion-1
        kubernetes.io/hostname: 10.0.24.52

you either need to do:

      nodeSelector: westporch-kubeminion-1
      kubernetes.io/hostname: 10.0.24.52

but it might be that nodeSelector needs a mapping as argument instead of the scalar westporch-kubeminion-1 it has now.

      nodeSelector:
        kubernetes.io/hostname: 10.0.24.52

This error might mask a second one in the file, depending on how lenient kubernetes is. The --- is the end-of-directives marker and since the directives are optional it can occur at the start of a YAML document. The one at the bottom of your example indicates the start of a new document. Once you address the first error, you might get a warning about single documents based on that. (The end-of-document marker consists of three dots: ... at the beginning of a line followed by whitespace.

Of course any changes you make should confirm to what kubernetes is expecting, but the above stream is clearly non-valid as YAML in itself.

Upvotes: 49

Irina
Irina

Reputation: 31

By default, 'kubernetes.io/hostname' is a well known label that contains the Node's name instead of its IP address. This can be checked by running $ kubectl get nodes --show-labels. As such, I recommend to make the following changes:

  nodeSelector: 
    kubernetes.io/hostname: westporch-kubeminion-1

Upvotes: 0

jzeef
jzeef

Reputation: 729

As mentioned, nodeSelector cannot have a value like that. It is to specify a map of key-value pairs. You can read more about the specific usage here. For instance, a proper usage of nodeSelector might be:

    nodeSelector:
       disktype: ssd

Upvotes: 1

Related Questions