Filtering values from kubectl output

11/14/2019

I have a k8s cluster and get the list of images if I run:

kubectl get pods --all-namespaces -o jsonpath="{..image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c

-this works.

Now, I have to list all images which do not start with a particular string, say "random.domain.com"

how to filter out attribute values using jsonpath?

I have tried out below.

kubectl get pods --all-namespaces -o jsonpath="{..image}[?(@.image!="random.domain.com")]" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c

As a workaround, I am using -

kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |sort |uniq -c| grep -v "random.domain.com"

But wanted to know how we can get this done by using jsonpath.

Thanks.

-- Avishek2585835
jq
json
jsonpath
kubectl
kubernetes

1 Answer

11/14/2019

I have no idea how to work with jsonpath (atm). but here how you can do that with jq

kubectl get pods -o json | \ 
 jq '[.items[].spec.containers[].image | select(. | startswith("random.domain.com") | not )] | unique'

AFAIS, according to the documentation, you can't do that with JSON path itself

-- Oleg Butuzov
Source: StackOverflow