Search code examples
kuberneteskubectljsonpath

What does empty square bracket mean in jsonpath with Kubernetes?


The following kubectl expression returns the image version:

kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'

But what does the '[]' signify exactly? i get that it is saying image is a child of containers which is a child of spec but why the empty square bracket?


Solution

  • {.spec.containers} will return an array. Using empty square bracket means the desired value is an array.

    you can validate this by running the following, the output of the query is an array:

     kubectl get po mongo  -o jsonpath='{.spec.containers}{"\n"}'
    [{"image":"mongo","imagePullPolicy":"Always","name":"mongo","ports":[{"containerPort":27107,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]},{"image":"nginx","imagePullPolicy":"Always","name":"nginx","ports":[{"containerPort":80,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]}]
    

    when used with [], we expect the output of .spec.containers to be array and process it already :

    kubectl get po mongo  -o jsonpath='{.spec.containers[]}{"\n"}'
    {"image":"mongo","imagePullPolicy":"Always","name":"mongo","ports":[{"containerPort":27107,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]}