How do I index using a jsonpath key containing a string '.'

194 Views Asked by At

I am working in OCP trying to access the following information from the yaml output generated by oc get nodes my-node -o yaml using the -o jsonpath {} feature.

i.e. The full yaml output

apiVersion: v1
kind: Node
metadata:
  annotations:
    k8s.ovn.org/gateway-mtu-support: "false"
    k8s.ovn.org/host-addresses: '["10.19.45.59","192.168.122.228","2620:52:0:132d:2017:5303:e319:1543"]'
    k8s.ovn.org/l3-gateway-config: '{"default":{"mode":"shared","interface-id":"br-ex_worker-226","mac-address":"10:70:fd:0a:69:44","ip-addresses":["192.168.122.228/24"],"ip-address":"192.168.122.228/24","next-hops":["192.168.122.1"],"next-hop":"192.168.122.1","node-port-enable":"true","vlan-id":"0"}}'
    k8s.ovn.org/node-chassis-id: 72a8f396-fafa-4158-8fc6-5b2066bebb65
    k8s.ovn.org/node-gateway-router-lrp-ifaddr: '{"ipv4":"100.64.0.5/16"}'

I want to access "next-hop" and "ip-address" from k8s.ovn.org/l3-gateway-config.

However when I try the following that I would expect to work here:

oc get nodes $HOSTNAME_BMH_NODE_2 -o jsonpath={.metadata.annotations['k8s.ovn.org/l3-gateway-config']}

I get the error invalid array index k8s.ovn.org/l3-gateway-config

How can I access this data using jsonpath? Is it failing to do the '.' or / or something else? I tried escaping these characters with a leading \ to no avail

1

There are 1 best solutions below

2
larsks On

You need to escape all instances of . in the key. So given:

apiVersion: v1
kind: Node
metadata:
  annotations:
    k8s.ovn.org/gateway-mtu-support: "false"
    k8s.ovn.org/host-addresses: '["10.19.45.59","192.168.122.228","2620:52:0:132d:2017:5303:e319:1543"]'
    k8s.ovn.org/l3-gateway-config: '{"default":{"mode":"shared","interface-id":"br-ex_worker-226","mac-address":"10:70:fd:0a:69:44","ip-addresses":["192.168.122.228/24"],"ip-address":"192.168.122.228/24","next-hops":["192.168.122.1"],"next-hop":"192.168.122.1","node-port-enable":"true","vlan-id":"0"}}'
    k8s.ovn.org/node-chassis-id: 72a8f396-fafa-4158-8fc6-5b2066bebb65
    k8s.ovn.org/node-gateway-router-lrp-ifaddr: '{"ipv4":"100.64.0.5/16"}'

The following should work:

oc get nodes $HOSTNAME_BMH_NODE_2 \
  -o jsonpath='{.metadata.annotations.k8s\.ovn\.org/l3-gateway-config}'

Using my local cluster, I have:

$ oc get node wrk-11 -o jsonpath='{.metadata.annotations}' | jq
{
  "csi.volume.kubernetes.io/nodeid": "{\"openshift-storage.rbd.csi.ceph.com\":\"wrk-11\"}",
  "machine.openshift.io/machine": "openshift-machine-api/shift-mnjtt-worker-0-6tr8t",
  "machineconfiguration.openshift.io/controlPlaneTopology": "HighlyAvailable",
  "machineconfiguration.openshift.io/currentConfig": "rendered-worker-a66afd081a708b2e9d968c3d07c0394e",
  "machineconfiguration.openshift.io/desiredConfig": "rendered-worker-a66afd081a708b2e9d968c3d07c0394e",
  "machineconfiguration.openshift.io/reason": "",
  "machineconfiguration.openshift.io/state": "Done",
  "volumes.kubernetes.io/controller-managed-attach-detach": "true"
}

And I can successfully run:

$ oc get node wrk-11 \
  -o jsonpath='{.metadata.annotations.csi\.volume\.kubernetes\.io/nodeid}'
{"openshift-storage.rbd.csi.ceph.com":"wrk-11"}