I have the following command:
kubectl get pod -A -o=json | jq -r '.items[]|select(any( .status.containerStatuses[]; .state.waiting or .state.terminated))|[.metadata.namespace, .metadata.name]|@csv'
This command works great. It outputs both the namespace and name of my failing pods.
But now I want to add one more column to the results. The column I want is located in one (and only one) of two places:
- .status.containerStatuses[].state.waiting.reason
- .status.containerStatuses[].state.terminated.reason
I first tried adding .status.containerStatuses[].state.*.reason
to the results fields array. But that gave me an unexpected '*'
compile error.
I then got to thinking about how I would do this with SQL or another programming language. They frequently have a function that will return the first non-null value of its parameters. (This is usually called coalesce). However I could not find any such command for jq
.
How can I return the reason
as a result of my query?