<p>I need to get a list of all pods that were not created by a controller so I can decide how to handle them before doing a drain on a node. </p>

<p>Otherwise I get the message: </p>

<pre><code>error: cannot delete Pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet (use --force to override) while running the drain.
</code></pre>

<p>I can find the information by running <code>kubectl describe &lt;pod&gt;</code> and looking to see if the Controlled By: is missing but I want to programmatically search all pods on the node and since <code>kubectl describe</code> is not designed for that. I need to find an alternative method.</p>

Tom Crozier
<p>You can relly on the <code>ownerReferences</code> API object to find this:</p>

<blockquote>
  <p>$ kubectl explain pod.metadata.ownerReferences</p>
  
  <p>KIND:     Pod</p>
  
  <p>VERSION: v1</p>
  
  <p>RESOURCE: ownerReferences &lt;[]Object></p>
  
  <p>DESCRIPTION:
       List of objects depended by this object. If ALL objects in the list have
       been deleted, this object will be garbage collected. If this object is
       managed by a controller, then an entry in this list will point to this
       controller, with the controller field set to true. There cannot be more
       than one managing controller.</p>
</blockquote>

<p>Bare pods (i.e., pods without controllers/owners) will not contain the <code>ownerReferences</code> field, so you can use the <code>--custom-columns</code> to find out which pods are controlled or not:</p>

<pre><code>$ kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.name,CONTROLLER:.metadata.ownerReferences[].kind,NAMESPACE:.metadata.namespace
NAME                               CONTROLLER   NAMESPACE
nginx-85ff79dd56-tvpts             ReplicaSet   default
static-pod1                        &lt;none&gt;       default
static-pod2                        &lt;none&gt;       default
coredns-5644d7b6d9-6hg82           ReplicaSet   kube-system
coredns-5644d7b6d9-wtph7           ReplicaSet   kube-system
etcd-minikube                      &lt;none&gt;       kube-system
kube-addon-manager-minikube        &lt;none&gt;       kube-system
kube-apiserver-minikube            &lt;none&gt;       kube-system
kube-controller-manager-minikube   &lt;none&gt;       kube-system
kube-proxy-fff5c                   DaemonSet    kube-system
kube-scheduler-minikube            &lt;none&gt;       kube-system
storage-provisioner                &lt;none&gt;       kube-system
tiller-deploy-55c9c4b4df-hgzwm     ReplicaSet   kube-system
</code></pre>

<hr>

<p>If you want only the pod names that are not owned by a controller manager, you can process the output of <code>kubectl get -o json</code> with <a href="https://github.com/stedolan/jq" rel="noreferrer">jq</a> (very useful for post script processing):</p>

<pre><code>$ kubectl get pods --all-namespaces -o json | jq -r '.items | map(select(.metadata.ownerReferences == null ) | .metadata.name) | .[]'
static-pod1
static-pod1
etcd-minikube
kube-addon-manager-minikube
kube-apiserver-minikube
kube-controller-manager-minikube
kube-scheduler-minikube
storage-provisioner
</code></pre>
