Skip to content

Instantly share code, notes, and snippets.

@noseka1
Last active October 4, 2024 22:14
Show Gist options
  • 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"}{end}' -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

@php-coder
Copy link

kubectl get pods -o jsonpath='{range .items[].spec.containers[]}{.image}{"\n"}'

The {end} is missing here.

@noseka1
Copy link
Author

noseka1 commented Jul 19, 2024

@php-coder Thanks! I corrected it.

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