2

I am using jq to get some data in desired format, complete json data of a pod is pasted here https://pastebin.com/UMJA7xd5. This data is one of the pod in json format. I am going to run it on all pods, so that I can list the info like,

podname,1st-container-name,cpu-limit,memory-limit,2nd-container-name,cpu-limit,memory-limit--so-on-based-on-number-ofcontainer

Based on number of containers in a pod. Number of containers in pods are random, some pods have 1 or some have upto 5 containers.

I am able to create upto here.

oc get po -o json | jq -j   '.items[] | "\(.metadata.name), \(.spec.containers[] | .name +" , "+ .resources.limits.cpu +" , "+ .resources.limits.memory)\n" '

which give me output like below:-

kafka-0, kafka , 16 , 16Gi
kafka-0, filebeat , 2 , 256Mi

In above example data, printing data of two container for a single pod kafka-0. kafka pod have two container so producing two line.

Expected Result:

kafka-0, kafka , 16 , 16Gi, filebeat , 2 , 256Mi

I am new to jq and tried a lot of option, but could not succeed, might be doing something wrong. In this value of container are varying, so could not join these lines.

1 Answer 1

1
$ cat test.jq
# Iterate over "items"

.items[] |

# Place each extracted element into an array

[
    # Select elements

    .metadata.name,

    # Use parentheses to group
    ( .spec.containers[] | .name, .resources.limits["cpu","memory"] )
]

# Join the array together

| join(", ")

$ <input jq -r -f test.jq
kafka-0, kafka, 16, 16Gi, filebeat, 2, 256Mi
1
  • Thank you very much, it is working. Can you share some link where I can learn jq this way. It will be very helpful. Thanks again. Apr 17, 2020 at 4:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .