<p>I would like to be able to programmatically query Kubernetes to find overcommitted nodes.</p>

<p>If I do <code>kubectl describe nodes</code>, I get human-readable output including information about resource usage that I'm after, e.g.</p>

<pre><code>Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource                   Requests              Limits
  --------                   --------              ------
  cpu                        786m (40%)            5078m (263%)
  memory                     8237973504500m (74%)  13742432Ki (126%)
</code></pre>

<p>However, <code>kubectl describe</code> doesn't support JSON or YAML output, and <code>kubectl get nodes -ojson</code> doesn't include the allocated resource stats. Is there any other way to access this information?</p>

Matt R
<p>If you run any <code>kubectl</code> command with the <code>--v=6</code> option the output will include the kubernetes API calls that make up the output.</p>

<p>In the case of <code>kubectl describe nodes NODE</code> you will see there is a <code>api/v1/pods</code> request that filters pods on the node and removes some "not running" statuses</p>

<pre><code>I0828 13:44:29.310208   55233 round_trippers.go:438] GET https://kubernetes.docker.internal:6443/api/v1/pods?fieldSelector=spec.nodeName%3Ddocker-desktop%2Cstatus.phase%21%3DFailed%2Cstatus.phase%21%3DSucceeded 200 OK in 4 milliseconds
</code></pre>

<p>If you complete this request with authentication information from your <code>~/.kube/config</code> file you should be able to get the output.  In this case, using <code>jq</code> to filter the output down to the <code>resources</code> component of the container spec with CA/Cert/Key auth (base64 decoded). </p>

<pre><code>curl --cacert ~/.kube/docker-desktop.ca \
 --cert ~/.kube/docker-desktop.cert \
 --key ~/.kube/docker-desktop.key \
 https://kubernetes.docker.internal:6443/api/v1/pods?fieldSelector=spec.nodeName%3Ddocker-desktop%2Cstatus.phase%21%3DFailed%2Cstatus.phase%21%3DSucceeded \
| jq '.items[].spec.containers[].resources'
</code></pre>

<pre><code>{}
{}
{
  "limits": {
    "memory": "170Mi"
  },
  "requests": {
    "cpu": "100m",
    "memory": "70Mi"
  }
}
{
  "limits": {
    "memory": "170Mi"
  },
  "requests": {
    "cpu": "100m",
    "memory": "70Mi"
  }
}
{}
{
  "requests": {
    "cpu": "250m"
  }
}
{
  "requests": {
    "cpu": "200m"
  }
}
{}
{
  "requests": {
    "cpu": "100m"
  }
}
{}
{}
</code></pre>

<p>Running these calls and filters will generally be easier with one of the <a href="https://kubernetes.io/docs/reference/using-api/client-libraries/" rel="nofollow noreferrer">kubernetes API clients</a> if you are regularly going to this level. </p>
