Search in sources :

Example 1 with NodeMetricOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.NodeMetricOperationsImpl in project Taier by DTStack.

the class AbstractK8sResourceInfo method getResource.

public void getResource(KubernetesClient kubernetesClient) {
    List<Node> nodes = kubernetesClient.nodes().list().getItems();
    Map<String, NodeStatus> nodeStatusMap = new HashMap<>(nodes.size());
    for (Node node : nodes) {
        nodeStatusMap.put(node.getMetadata().getName(), node.getStatus());
    }
    NodeMetricOperationsImpl nodeMetricOperations = kubernetesClient.top().nodes();
    List<NodeMetrics> nodeMetrics = nodeMetricOperations.metrics().getItems();
    for (NodeMetrics nodeMetric : nodeMetrics) {
        String nodeName = nodeMetric.getMetadata().getName();
        NodeStatus nodeStatus = nodeStatusMap.get(nodeName);
        Map<String, Quantity> allocatable = nodeStatus.getAllocatable();
        Map<String, Quantity> usage = nodeMetric.getUsage();
        BigDecimal cpuAllocatable = Quantity.getAmountInBytes(allocatable.get(CPU));
        BigDecimal cpuUsage = Quantity.getAmountInBytes(usage.get(CPU));
        BigDecimal menAllocatable = Quantity.getAmountInBytes(allocatable.get(MEMORY));
        BigDecimal menUsage = Quantity.getAmountInBytes(usage.get(MEMORY));
        double freeCores = cpuAllocatable.subtract(cpuUsage).doubleValue();
        double freeMem = menAllocatable.subtract(menUsage).doubleValue();
        this.addNodeResource(new AbstractK8sResourceInfo.NodeResourceDetail(nodeName, cpuAllocatable.doubleValue(), cpuUsage.doubleValue(), freeCores, menAllocatable.doubleValue(), menUsage.doubleValue(), freeMem));
    }
    calc();
}
Also used : HashMap(java.util.HashMap) Node(io.fabric8.kubernetes.api.model.Node) NodeMetrics(io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics) Quantity(io.fabric8.kubernetes.api.model.Quantity) BigDecimal(java.math.BigDecimal) NodeMetricOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.NodeMetricOperationsImpl) NodeStatus(io.fabric8.kubernetes.api.model.NodeStatus)

Aggregations

Node (io.fabric8.kubernetes.api.model.Node)1 NodeStatus (io.fabric8.kubernetes.api.model.NodeStatus)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 NodeMetrics (io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics)1 NodeMetricOperationsImpl (io.fabric8.kubernetes.client.dsl.internal.NodeMetricOperationsImpl)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1