Display logs of an initContainer running inside github actions

4/16/2021

I have a pod which embed an initContainer named initdb. Is there a kubectl command which returns true if initdb is started or else false? I need it to display logs of initdb in Github Action CI (kubectl log <pod> -c initdb crashes if initd is not started yet).

-- Fabrice Jammes
continuous-integration
github-actions
kubectl
kubernetes

1 Answer

4/16/2021

If you have a single init container in the Pod, you could so something like the following:

k get pod pod-name --output="jsonpath={.status.initContainerStatuses[0].ready}"

This will return true if the init container is in status Ready, but this means only that the init container is ready, it could be already terminated (because it completed the execution) or still running. I'm not completely sure but if an init container is ready, requesting its logs should work without errors)

You can use jsonpath to select specific sections of Pods definitions exactly for the scope of automating certain checks.

To see the full definition of your Pod, just use:

k get pod pod-name -oyaml

and maybe select what you are interested from there. If you want to wait until the init container is terminated or started, you could check its state section which explains the current state in detail and basically create a more finer check on what you are expecting to see.

-- AndD
Source: StackOverflow