Search code examples
kubectljsonpath

kubectl display all (pod name, container name) couples


Suppose I have 2 pods podA and podB, each one having 2 containers. I'd like to diplay:

podA containerA
podA containerB
podB containerA
podB containerB

Using jsonpath, I'm only able to display this kind of output

podA containerA
containerB
podB containerA
containerB

using this command:

kubectl get pods -o jsonpath="{range .items[*]}{.metadata.name}{range .spec.containers[*]}{.name}{'\n'}{end}{end}"

Is it possible to repeat the pod name, using only kubectl command?


Solution

  • I think not.

    There's no support for variable assignment so it's not possible to capture a value for Pod's name from the outer loop (range .items[*]) to access from within the inner loop once the inner range rescopes the pipeline.

    I understand the intent in wanting to use only kubectl but this limits you to kubectl's JSONPath implementation.

    jq is a more general-purpose JSON processor that is reasonable to expect|assume be installed by your users. It would permit capturing the Pod's name.

    I don't have access to a cluster but something like:

    FILTER='
    .items[] 
    | metadata.name as $pod 
    | .spec.containers[].name as $container 
    | $pod+" "+$container
    '
    jq -r "${FILTER}"