Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract multiple values from kubectl with jsonpath

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?

like image 384
navicore Avatar asked Sep 01 '25 10:09

navicore


2 Answers

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
like image 112
Pawan Kumar Avatar answered Sep 02 '25 23:09

Pawan Kumar


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
like image 27
Gonzalo Cao Avatar answered Sep 03 '25 00:09

Gonzalo Cao