<p>I am trying to get the cluster utilization values (CPU Utilization, Memory, File System, Network transfer, pod count) in a go program. Do we have api's to get the cluster data directly from k8s or openshift which can be called from a go program ? Or can we get the individual node data and add it up to get the total cluster utilization values ?</p>
<p>Executing kubectl commands using os/exec in the program is not able to fetch the values.</p>

<p>The Kubernetes <a href="https://kubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline" rel="nofollow noreferrer">metrics server</a> offers all of this information via the <a href="https://github.com/kubernetes/kubernetes/blob/7d309e0104fedb57280b261e5677d919cb2a0e2d/staging/src/k8s.io/kubelet/pkg/apis/stats/v1alpha1/types.go#L23-L29" rel="nofollow noreferrer">Summary API</a>.</p>
<p>Invoking this API is straight-forward using the command line.</p>
<pre><code>kubectl get --raw &quot;/api/v1/nodes/${NODE_NAME}/proxy/stats/summary&quot;
</code></pre>
<p>And from go, if you are using <a href="https://github.com/kubernetes/client-go/" rel="nofollow noreferrer">client-go</a>, this API can be invoked with:</p>
<pre class="lang-golang prettyprint-override"><code>request := clientset.CoreV1().RESTClient().Get().Resource(&quot;nodes&quot;).Name(node.Name).SubResource(&quot;proxy&quot;).Suffix(&quot;stats/summary&quot;)
responseRawArrayOfBytes, err := request.DoRaw(context.Background())
if err != nil {
    return nil, errors.Wrap(err, &quot;failed to get stats from node&quot;)
}
</code></pre>
<p>Note that, according to the official metrics server documentation, the <a href="https://kubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline/#summary-api-source" rel="nofollow noreferrer">endpoint will be changing</a> to <code>/metrics/resource</code> in metrics-server 0.6.x.</p>
