<p>Problem Statement:</p>
<p>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 <code>ownerReference</code> and then going up the chain recursively finding for the root parent workload that initiated the pod.</p>
<p>Is there a way I can directly know which root parent workload initiated the Pod?</p>

ashu8912
<p>First, please remember that pods created by specific workload have this workload's name in the pod name. For example, pods defined in <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">deployments</a> have following pod naming convention:</p>
<p><code>&lt;replicaset-name&gt;-&lt;some-string&gt;</code></p>
<p>and replica set name is:</p>
<p><code>&lt;deployment-name&gt;-&lt;some-string&gt;</code></p>
<p>So for example:</p>
<p><em>Pod name</em>:  <code>nginx-66b6c48dd5-84rml</code>
<em>Replica set name</em>: <code>nginx-66b6c48dd5</code>
<strong>Deployment name</strong>: <code>nginx</code></p>
<p>So the first part of the name which doesn't seem to be some random letters / number is the root workload name.</p>
<p>Only pods <a href="https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#pods-in-a-statefulset" rel="nofollow noreferrer">defined in StatefulSet have ordinal indexes</a>, as follows:</p>
<p><code>&lt;statefulset-name&gt;-&lt;ordinal index&gt;</code></p>
<p>For example:</p>
<p><em>Pod name</em>: <code>web-0</code>
<strong>StafeulSet name</strong>: <code>web</code></p>
<p>Of 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.</p>
<hr />
<p>Well, not taking into account the pod's name, it seems that your thinking is correct, the only way to find a &quot;root&quot; workload is to go through the chain recursively and find the next &quot;parents&quot; workloads.</p>
<p>When you run the command <code>kubectl get pod {pod-name} -o json</code> (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).</p>
<p>I wrote a small bash script that recursively checks every workload's <code>ownerReferences</code> until it finds &quot;root&quot; workload (the root workload does not have <code>ownerRefernces</code>). It requires you to have <a href="https://github.com/stedolan/jq" rel="nofollow noreferrer"><code>jq</code> utility</a> installed on your system. Check this:</p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/bash

function get_root_owner_reference {
    # Set kind, name and namespace
    kind=$1
    name=$2
    namespace=$3

    # Get ownerReferences
    owner_references=$(kubectl get $kind $name -o json -n $namespace | jq -r 'try (.metadata.ownerReferences[])')

    # If ownerReferences does not exists assume that it is root workload; if exists run get_root_owner_reference function
    if [[ -z &quot;$owner_references&quot; ]]; then
        resource_json=$(kubectl get $kind $name -o json -n $namespace)
        echo &quot;Kind: $(echo $resource_json | jq -r '.kind')&quot;
        echo &quot;Name: $(echo $resource_json | jq -r '.metadata.name')&quot;
    else
        get_root_owner_reference $(echo $owner_references | jq -r '.kind') $(echo $owner_references | jq -r '.name') $namespace
    fi

}

# Get namespace if set
if [[ -z $3 ]]; then
    namespace=&quot;default&quot;
else
    namespace=$3
fi

get_root_owner_reference $1 $2 $namespace
</code></pre>
<p>You need to provide two arguments - resource and name of the resource. Namespace name is optional (if not given it will use Kubernetes <code>default</code> namespace).</p>
<p><strong>Examples</strong>:
Pod defined in deployment:</p>
<pre><code>user@cloudshell:~/ownerRefernce$ ./get_root_owner_reference.sh pod nginx-66b6c48dd5-84rml
Kind: Deployment
Name: nginx
</code></pre>
<p>Pod created from CronJob:</p>
<pre><code>user@cloudshell:~/ownerRefernce$ ./get_root_owner_reference.sh pod hello-27247072-mv4l9
Kind: CronJob
Name: hello
</code></pre>
<p>Pod created straight from pod definition:</p>
<pre><code>user@cloudshell:~/ownerRefernce$ ./get_root_owner_reference.sh pod hostipc-exec-pod
Kind: Pod
Name: hostipc-exec-pod
</code></pre>
<p>Pod from other namespace:</p>
<pre><code>user@cloudshell:~/ownerRefernce$ ./get_root_owner_reference.sh pod kube-dns-679799b55c-7pzr7 kube-system
Kind: Deployment
Name: kube-dns
</code></pre>
