Skip to content

Instantly share code, notes, and snippets.

@noseka1
Last active September 27, 2023 15:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save noseka1/231105ca1e39b304c7e737323378825a to your computer and use it in GitHub Desktop.
Save noseka1/231105ca1e39b304c7e737323378825a 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