Kubectl get JSONPath Support: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 1: Line 1:
=External=
* https://kubernetes.io/docs/reference/kubectl/jsonpath/
* https://goessner.net/articles/JsonPath/
=Internal=
=Internal=
* [[Kubectl#JSONPath_Support|kubectl]]
* [[Kubectl#JSONPath_Support|kubectl]]
====Removing Leading and Trailing Single Quotes====
<syntaxhighlight lang='text'>
... | sed -e 's/^'\''//' > ...
</syntaxhighlight>
====Get an Individual Attribute Only====
<font color=darkgray>TODO: https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba</font>
kubectl ... -o jsonpath="{.status.phase}"
kubectl ... -o jsonpath="{.items[?(@.spec.unschedulable)].metadata.name}"
<font color=darkgray>
Alternative, to explore and document:
kubectl get pods  --no-headers -o custom-columns=\":metadata.name\" ...
</font>
====Filter Elements of an Array based on a Key Value====
We assume that the elements of the array are maps, which contain the specified key:
kubectl ... -o jsonpath="{.users[?(@.name=="blue")].user.password}"
kubectl get pod ... -o jsonpath='{.items[0].spec.volumes[?(@.name=="vault")].hostPath.path}' 2>/dev/null
====Select and Combine Two or More Elements====
<syntaxhighlight lang='bash'>
kubectl get pod \
-o jsonpath='{.items[0].spec.volumes[?(@.name=="A")].hostPath.path}'+'{.items[0].spec.volumes[?(@.name=="B")].hostPath.path}'
</syntaxhighlight>
returns "/some/path/a+/some/path/b"
====Same Element from Multiple Resources====
<syntaxhighlight lang='bash'>
kubectl get pod -o jsonpath='{.items[*].metadata.name}'
</syntaxhighlight>
====Array Length====
{{Internal|Jq_Usage#Array_Length|jq Array Length}}
====TODO====
<syntaxhighlight lang='bash'>
kubectl get pods -o=jsonpath='{.items[?(@.metadata.labels.name=="web")].metadata.name}'
</syntaxhighlight>
=====Iterate over the Elements of an Array=====
<syntaxhighlight lang='bash'>
kubectl get nodes -o jsonpath='{.items[*]}'
</syntaxhighlight>
=====Iterate over the Elements of an Array and Select a Specific Key=====
<syntaxhighlight lang='bash'>
kubectl get nodes -o jsonpath='{.items[*].status}'
</syntaxhighlight>
Filter by an element:
<syntaxhighlight lang='bash'>
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")]}'
</syntaxhighlight>
Print the element "address"
<syntaxhighlight lang='bash'>
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'
</syntaxhighlight>

Revision as of 22:42, 15 March 2021

External

Internal




Removing Leading and Trailing Single Quotes

... | sed -e 's/^'\''//' > ...

Get an Individual Attribute Only

TODO: https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba

kubectl ... -o jsonpath="{.status.phase}"
kubectl ... -o jsonpath="{.items[?(@.spec.unschedulable)].metadata.name}"

Alternative, to explore and document:

kubectl get pods  --no-headers -o custom-columns=\":metadata.name\" ...

Filter Elements of an Array based on a Key Value

We assume that the elements of the array are maps, which contain the specified key:

kubectl ... -o jsonpath="{.users[?(@.name=="blue")].user.password}"
kubectl get pod ... -o jsonpath='{.items[0].spec.volumes[?(@.name=="vault")].hostPath.path}' 2>/dev/null

Select and Combine Two or More Elements

kubectl get pod \
-o jsonpath='{.items[0].spec.volumes[?(@.name=="A")].hostPath.path}'+'{.items[0].spec.volumes[?(@.name=="B")].hostPath.path}'

returns "/some/path/a+/some/path/b"

Same Element from Multiple Resources

kubectl get pod -o jsonpath='{.items[*].metadata.name}'

Array Length

jq Array Length

TODO

kubectl get pods -o=jsonpath='{.items[?(@.metadata.labels.name=="web")].metadata.name}'
Iterate over the Elements of an Array
kubectl get nodes -o jsonpath='{.items[*]}'
Iterate over the Elements of an Array and Select a Specific Key
kubectl get nodes -o jsonpath='{.items[*].status}'

Filter by an element:

kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")]}'

Print the element "address"

kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'