<p>kubectl top nodes<br>
NAME                               CPU(cores)      CPU%      MEMORY(bytes)      MEMORY%<br> <br />
gsdjsgfhdsgfz-12345665-jisj000000    934m                  24%         10439Mi                  82%<br>
gsdjsgfhdsgfz-12345665-jisj000001   717m                  18%          9132Mi                    72%<br>
gsdjsgfhdsgfz-12345665-jisj000002   1099m                 28%           7614Mi                 60%<br></p>
<p>how to get the CPU% and MEMORY% value using java io.fabric8 kubernetes-client library.</p>
<pre><code>try (KubernetesClient k8s = new DefaultKubernetesClient()) {

NodeMetricsList nodeMetricsList = k8s.top().nodes().metrics();
for (NodeMetrics nodeMetrics : nodeMetricsList.getItems()) {
      logger.info(&quot;{} {} {}&quot;, nodeMetrics.getMetadata().getName(),
        nodeMetrics.getUsage().get(&quot;cpu&quot;),
        nodeMetrics.getUsage().get(&quot;memory&quot;));
}
</code></pre>
<p>}</p>
<p>getting output is:-<br>
node name<br>
cpu:-1094942089n<br>
memory:-7830672Ki<br><br></p>
<p>how will take the percentage values?</p>

shanees mk
<p>I had to implement this same feature recently, unfortunately I didn't find a way to get the percentages just by using the <code>top()</code> API alone, I had to perform two calls, one to <code>nodes()</code> in order to retrieve total capacity and another one to <code>top()</code> to retrieve used capacity. Then it was just a matter of calculating percentage.</p>
<p>A snippet of the working code:</p>
<pre><code>public static void main(String[] args) {
    KubernetesClient kubernetesClient = new DefaultKubernetesClient();
    Map&lt;String, Node&gt; nodeMap = kubernetesClient.nodes().list().getItems()
        .stream()
        .collect(Collectors.toMap(node -&gt; node.getMetadata().getName(), Function.identity()));
    List&lt;NodeUsage&gt; usageList = kubernetesClient.top().nodes().metrics().getItems()
        .stream()
        .map(metric -&gt; new NodeUsage(nodeMap.get(metric.getMetadata().getName()), metric.getUsage()))
        .collect(Collectors.toList());
    System.out.println(usageList);
  }

  private static class NodeUsage {
    private final Node node;
    private final BigDecimal cpuPercentage;
    private final BigDecimal memoryPercentage;

    private NodeUsage(Node node, Map&lt;String, Quantity&gt; used) {
      this.node = node;
      cpuPercentage = calculateUsage(used.get(&quot;cpu&quot;), node.getStatus().getAllocatable().get(&quot;cpu&quot;));
      memoryPercentage = calculateUsage(used.get(&quot;memory&quot;), node.getStatus().getAllocatable().get(&quot;memory&quot;));
    }

    private static BigDecimal calculateUsage(Quantity used, Quantity total) {
      return Quantity.getAmountInBytes(used)
          .divide(Quantity.getAmountInBytes(total), 2, RoundingMode.FLOOR)
          .multiply(BigDecimal.valueOf(100));
    }

    public Node getNode() {
      return node;
    }

    public BigDecimal getCpuPercentage() {
      return cpuPercentage;
    }

    public BigDecimal getMemoryPercentage() {
      return memoryPercentage;
    }
  }
</code></pre>
