<p>I am running a <strong>Kubernates Cluster</strong> in bare metal of three nodes.
I have applied a couple of <strong>yaml</strong> files of different services.
Now I would like to make order in the cluster and <strong>clean</strong> some orphaned kube objects.
To do that I need to understand the set of pods or other entities which use or refer a certain <strong>ServiceAccount</strong>.</p>

<p>For example, I can dig ClusterRoleBinding of the, say, admin-user and investigate it:</p>

<pre><code>kubectl get сlusterrolebinding admin-user
</code></pre>

<p>But is there a good <code>kubectl</code> options combination to find <strong>all the usages/references</strong> of some <strong>ServiceAccount</strong>?</p>

<p>You can list all resources using a service account with the following command: </p>

<pre><code>kubectl get rolebinding,clusterrolebinding --all-namespaces -o jsonpath='{range .items[?(@.subjects[0].name=="YOUR_SERVICE_ACCOUNT_NAME")]}[{.roleRef.kind},{.roleRef.name}];{end}' | tr ";" "\n"
</code></pre>

<p>You just need to replace <code>YOUR_SERVICE_ACCOUNT_NAME</code> to the one you are investigating. </p>

<p>I tested this command on my cluster and it works. </p>

<p>Let me know if this solution helped you.</p>
