Problem Statement:
I have a Pod which belongs to a workload, now I want to know the workload that initiated that Pod. One way of doing it right now is going through the ownerReference and then going up the chain recursively finding for the root parent workload that initiated the pod.
Is there a way I can directly know which root parent workload initiated the Pod?
First, please remember that pods created by specific workload have this workload's name in the pod name. For example, pods defined in deployments have following pod naming convention:
<replicaset-name>-<some-string>and replica set name is:
<deployment-name>-<some-string>So for example:
Pod name:
nginx-66b6c48dd5-84rmlReplica set name:nginx-66b6c48dd5Deployment name:nginxSo the first part of the name which doesn't seem to be some random letters / number is the root workload name.
Only pods defined in StatefulSet have ordinal indexes, as follows:
<statefulset-name>-<ordinal index>For example:
Pod name:
web-0StafeulSet name:webOf course, based on workload name we are not able to know what kind of workload it is. Check the second part of my answer below.
Well, not taking into account the pod's name, it seems that your thinking is correct, the only way to find a "root" workload is to go through the chain recursively and find the next "parents" workloads.
When you run the command
kubectl get pod {pod-name} -o json(to get all information about pod) there is only information about above level (as you said in case of pod defined in deployment, in pod information there is only information about replica set).I wrote a small bash script that recursively checks every workload's
ownerReferencesuntil it finds "root" workload (the root workload does not haveownerRefernces). It requires you to havejqutility installed on your system. Check this:You need to provide two arguments - resource and name of the resource. Namespace name is optional (if not given it will use Kubernetes
defaultnamespace).Examples: Pod defined in deployment:
Pod created from CronJob:
Pod created straight from pod definition:
Pod from other namespace: