Reputation: 2109
I've found jsonpath examples for testing multiple values but not extracting multiple values.
I want to get image
and name
from kubectl get pods
.
this gets me name
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].name}' | xargs -n 1
this gets me image
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}' | xargs -n 1
but
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].[name,image}' | xargs -n 2
complains invalid array index image
- is there a syntax for getting a list of node-adjacent values?
Upvotes: 34
Views: 24523
Reputation: 21
Anyone trying the above commands on Windows will get an error message like this:
error: error parsing jsonpath {range .items[*]}{@.metadata.name}{" "}{@.spec.template.spec.containers[].image}{"\n"}{end}, unrecognized character in action: U+005C '\'
To make it work, use double quotes at the beginning and end of the JSONPath template, and single quotes for the literals, like this:
kubectl get pods -Ao jsonpath="{range .items[*]}{@.metadata.name}{' '}{@.spec.template.spec.containers[].image}{'\n'}{end}"
...as described in the kubectl reference:
Note: On Windows, you must double quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template
Upvotes: 2
Reputation: 21
Thanks! I had to change a little bit, but this worked for me:
#!/bin/bash
releases=$(kubectl get deployment -A --output=jsonpath='{range .items[*]}{@.metadata.namespace}{"|"}{@.metadata.name}{"\n"}{end}')
for release in $releases; do
namespace=$( echo $release | cut -d "|" -f 1)
deployment=$( echo $release | cut -d "|" -f 2)
kubectl rollout restart deployments -n "${namespace}" "${deployment}"
done
Upvotes: 2
Reputation: 890
Use below command to get name and image:
kubectl get pods -Ao jsonpath='{range .items[*]}{@.metadata.name}{" "}{@.spec.template.spec.containers[].image}{"\n"}{end}'
It will give output like below:
name image
Upvotes: 46
Reputation: 2424
Useful command, I had to modify it a little to make it work (failed with -a flag). Also, I added a filter to app label and one more field to get: namespace, pod name, image
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{@.metadata.namespace}{"\t"}{@.metadata.name}{"\t"}{@.spec.containers[*].image}{"\n"}{end}' -l app=nginx
Upvotes: 8