Skip to content

Instantly share code, notes, and snippets.

@davistran86
Forked from noseka1/JSONPath in kubectl CLI.md
Created December 29, 2022 04:08
Show Gist options
  • Save davistran86/b9f8e2bc43cb202562c7aac870c241cb to your computer and use it in GitHub Desktop.
Save davistran86/b9f8e2bc43cb202562c7aac870c241cb to your computer and use it in GitHub Desktop.
JSONPath in kubectl CLI examples

Examples

$ kubectl get pods -o json
$ kubectl get pods -o jsonpath='{@}'
$ kubectl get pods -o jsonpath='{.items[0]}'
$ kubectl get pods -o jsonpath='{.items[0].metadata.name}'
$ kubectl get pods -o jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
$ kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
$ kubectl get pods -o jsonpath='{.items[*].status.podIP}'
$ kubectl get pods -o jsonpath='{range .items[*]}{.status.podIP}{"\n"}{end}'
$ kubectl get pods -o jsonpath='{range .items[*].spec.containers[*]}{.image}{"\n"}' -A
$ oc get kubeapiserver cluster -o=jsonpath='{range .status.conditions[?(@.type=="NodeInstallerProgressing")]}{.reason}{"\n"}{.message}{"\n"}{end}'

kubectl does not support regular expressions for JSONpath output. The following command does NOT work:

$ kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}'

The following command achieves the desired result:

$ kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'

References

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