5

How can I retrieve a configmap value using jsonpath?

I'm trying to retrieve the value of haproxy.cfg from my configmap, but I can't get it just right:

kubectl get cm -l app=haproxy -o jsonpath="{['items'][0]['data']['haproxy.cfg']}"

The above returns no results. But I can definitely get the configmap if I leave off the haproxy.cfg:

kubectl get cm -l app=haproxy -o jsonpath="{['items'][0]['data']}"

Yields:

map[haproxy.cfg:
global
  user root
  group root
  maxconn 256
...
]

I can use jq:

kubectl get cm -l app=haproxy -o json | jq -r '.items[0].data["haproxy.cfg"]'

which yields exactly what I want:


global
  user root
  group root
  maxconn 256

If you need help creating your ConfigMap, here's the manifest for the one I am using:

apiVersion: v1
data:
    haproxy.cfg: "\nglobal\n  user root\n  group root\n  maxconn 256\n\n"
kind: ConfigMap
metadata:
    annotations:
      meta.helm.sh/release-name: haproxy
      meta.helm.sh/release-namespace: haproxy
    labels:
      app: haproxy
      app-version: 2.4.0
      app.kubernetes.io/managed-by: Helm
      heritage: Helm
      release: haproxy
      version: 0.0.3
    name: haproxy
1
  • try {['items'][0]['data'][0]} or can you provide manifest file to create the cm to play around ? Commented Jun 15, 2021 at 23:32

2 Answers 2

4

Escape the . inside single quotes

kubectl get cm -l app=haproxy -o jsonpath="{.items[0].data['haproxy\.cfg']}"

* This didn't work a long time ago, pre 1.5. Then you needed to use go-template formatting.

Sign up to request clarification or add additional context in comments.

Comments

2

I have created simple configmap.

kubectl get cm game-config-example -o json

returns:

{
    "apiVersion": "v1",
    "data": {
        "game.properties": "enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30",
        "ui.properties": "color.good=purple\ncolor.bad=yellow\nallow.textmode=true\nhow.nice.to.look=fairlyNice\n"
    },
    "kind": "ConfigMap",
    "metadata": {
        "creationTimestamp": "2021-06-16T10:08:28Z",
        "name": "game-config-example",
        "namespace": "default",
        "resourceVersion": "24666141",
        "selfLink": "/api/v1/namespaces/default/configmaps/game-config-example",
        "uid": "3d6d2ba0-8f5a-43a7-953b-91a62dbcd248"
    }
}

I have tested solution with escaping . characters on versions 1.19 and 1.21. Both works fine.

kubectl get cm game-config-example -o jsonpath="{['data']['ui\.properties']}"

gives right output:

color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

Same result I can achive if I use:

  • go-template:
kubectl get cm game-config-example -o 'go-template={{index .data "ui.properties" }}'
  • jq command:
kubectl get cm game-config-example -o json | jq -r '.data."ui.properties"'

You can see also this issue - JSONpath fails to return keys containing dots in a map.

2 Comments

I was able to get your approach to work when I selected the CM by name (and thus did not receive a list back from kube). Here's what worked for me kubectl get cm haproxy -o 'go-template={{index .data "haproxy.cfg" }}'
Selecting my cm by label, I was also able to get the gotemplate to wokr like this kubectl get cm -l app=haproxy -o 'go-template={{ range .items }}{{index .data "haproxy.cfg" }}{{ end }}' - thanks for the go-template suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.