skip to Main Content

In my setup I have two Pods. Each running a single container with busybox image.

I want to parse Pod name and its container’s image name using Kubectl. First I tried to get these fields individually. That works fine. Then I tried to combine ‘Pod name’ with ‘dns policy’ field. And that also works. But if I try to combine the ‘Pod name’ and ‘container image’ fields, then I am getting the error.

Could you please help me understand why I am getting error with final command?

Thanks,

test-cloud@user1-c1-cp1:~$ k get pods -o jsonpath="{.items[*].spec.containers[*].image}"
busybox busybox

test-cloud@user1-c1-cp1:~$
test-cloud@user1-c1-cp1:~$ k get pods -o jsonpath="{.items[*]['.metadata.name']}"
b2 b4

test-cloud@user1-c1-cp1:~$ k get pods -o jsonpath="{.items[*]['.metadata.name', '.spec.dnsPolicy']}"
b2 b4 ClusterFirst ClusterFirst

test-cloud@user1-c1-cp1:~$ k get pods -o jsonpath="{.items[*]['.metadata.name}" '.spec.containers[*].image']}
error: error parsing jsonpath {.items[*]['.metadata.name', '.spec.containers[*].image']}, invalid array index '.spec.containers[*
'''


2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the replies. While posted answers would work, I found below more suitable:

    k get pods -o jsonpath="{.items[*].metadata.name}, {.items[*].spec.containers[*].image}"
    

    b2 b4, busybox busybox


  2. I would try to keep it simple by using custom-columns:

    kubectl get pod -o custom-columns="POD-NAME":.metadata.name,"NAMESPACE":.metadata.namespace,"CONTAINER-IMAGES":.spec.containers[*].image,"DNS-POLICY":.spec.dnsPolicy
    POD-NAME   NAMESPACE   CONTAINER-IMAGES   DNS-POLICY
    bar        default     nginx              ClusterFirst
    foo        default     nginx              ClusterFirst
    zoo        default     nginx,ubuntu       ClusterFirst
    

    Using jsonpath:

    kubectl get pod -o jsonpath='{range .items[*]}{.metadata.name},{.spec.containers[*].image},{.spec.dnsPolicy}{"n"}{end}'
    bar,nginx,ClusterFirst
    foo,nginx,ClusterFirst
    zoo,nginx ubuntu,ClusterFirst
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search