Search in sources :

Example 1 with NodeMetrics

use of io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics in project kubernetes-client by fabric8io.

the class TopExample method main.

public static void main(String[] args) {
    try (KubernetesClient client = new KubernetesClientBuilder().build()) {
        if (!client.supportsApiPath("/apis/metrics.k8s.io")) {
            logger.warn("Metrics API is not enabled in your cluster");
            return;
        }
        logger.info("==== Node Metrics  ====");
        client.top().nodes().metrics().getItems().forEach(nodeMetrics -> logger.info("{}\tCPU: {}{}\tMemory: {}{}", nodeMetrics.getMetadata().getName(), nodeMetrics.getUsage().get(CPU).getAmount(), nodeMetrics.getUsage().get(CPU).getFormat(), nodeMetrics.getUsage().get(MEMORY).getAmount(), nodeMetrics.getUsage().get(MEMORY).getFormat()));
        final String namespace = Optional.ofNullable(client.getNamespace()).orElse("default");
        logger.info("==== Pod Metrics ====");
        client.top().pods().metrics(namespace).getItems().forEach(podMetrics -> podMetrics.getContainers().forEach(containerMetrics -> logger.info("{}\t{}\tCPU: {}{}\tMemory: {}{}", podMetrics.getMetadata().getName(), containerMetrics.getName(), containerMetrics.getUsage().get(CPU).getAmount(), containerMetrics.getUsage().get(CPU).getFormat(), containerMetrics.getUsage().get(MEMORY).getAmount(), containerMetrics.getUsage().get(MEMORY).getFormat())));
        client.pods().inNamespace(namespace).list().getItems().stream().findFirst().map(pod -> {
            logger.info("==== Individual Pod Metrics ({}) ====", pod.getMetadata().getName());
            try {
                return client.top().pods().metrics(namespace, pod.getMetadata().getName());
            } catch (KubernetesClientException ex) {
                if (ex.getCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                    logger.info(" - Pod has not reported any metrics yet");
                } else {
                    logger.warn(" - Error retrieving Pod metrics: {}", ex.getMessage());
                }
                return null;
            }
        }).ifPresent(podMetrics -> podMetrics.getContainers().forEach(containerMetrics -> logger.info("{}\t{}\tCPU: {}{}\tMemory: {}{}", podMetrics.getMetadata().getName(), containerMetrics.getName(), containerMetrics.getUsage().get(CPU).getAmount(), containerMetrics.getUsage().get(CPU).getFormat(), containerMetrics.getUsage().get(MEMORY).getAmount(), containerMetrics.getUsage().get(MEMORY).getFormat())));
    } catch (KubernetesClientException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) LoggerFactory(org.slf4j.LoggerFactory) Optional(java.util.Optional) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with NodeMetrics

use of io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics in project kubernetes-client by fabric8io.

the class MetricsTest method testNodeMetric.

@Test
void testNodeMetric() {
    server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/nodes/test-node").andReturn(200, getNodeMetric()).once();
    NodeMetrics nodeMetrics = client.top().nodes().metrics("test-node");
    assertEquals("foo", nodeMetrics.getMetadata().getName());
}
Also used : NodeMetrics(io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics) Test(org.junit.jupiter.api.Test)

Example 3 with NodeMetrics

use of io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics 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)

Example 4 with NodeMetrics

use of io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics in project kubernetes-client by fabric8io.

the class NodeMetricsTest method testMetrics.

@Test
void testMetrics() {
    // Given
    server.expect().withPath("/apis/metrics.k8s.io/v1beta1/nodes/the-node").andReturn(HTTP_OK, new NodeMetricsListBuilder().addToItems(new NodeMetricsBuilder().withNewMetadata().withName("the-metric").endMetadata().build()).build()).once();
    // When
    final NodeMetricsList result = client.top().nodes().withName("the-node").metrics();
    // Then
    assertThat(result).extracting(NodeMetricsList::getItems).asList().singleElement().hasFieldOrPropertyWithValue("Kind", "NodeMetrics").hasFieldOrPropertyWithValue("metadata.name", "the-metric");
}
Also used : NodeMetricsListBuilder(io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsListBuilder) NodeMetricsList(io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsList) NodeMetricsBuilder(io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with NodeMetrics

use of io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics in project yakc by manusa.

the class MetricsV1beta1ApiIT method readNodeMetrics.

@Test
@DisplayName("readNodeMetrics, cluster contains at least a Node with some metrics")
void readNodeMetrics() throws IOException, InterruptedException {
    // Given
    final String node = KC.create(CoreV1Api.class).listNode().stream().findFirst().map(Node::getMetadata).map(ObjectMeta::getName).orElseThrow(() -> new AssertionError("No nodes found"));
    // When
    final NodeMetrics result = getWithRetry(() -> KC.create(MetricsV1beta1Api.class).readNodeMetrics(node).get());
    // Then
    assertThat(result).hasFieldOrPropertyWithValue("metadata.name", node).hasFieldOrProperty("usage.cpu").hasFieldOrProperty("usage.memory").hasFieldOrProperty("window").extracting(NodeMetrics::getTimestamp).isNotNull();
}
Also used : Node(com.marcnuri.yakc.model.io.k8s.api.core.v1.Node) NodeMetrics(com.marcnuri.yakc.model.io.k8s.metrics.pkg.apis.metrics.v1beta1.NodeMetrics) MetricsV1beta1Api(com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Test (org.junit.jupiter.api.Test)3 NodeMetrics (io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics)2 MetricsV1beta1Api (com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api)1 Node (com.marcnuri.yakc.model.io.k8s.api.core.v1.Node)1 NodeMetrics (com.marcnuri.yakc.model.io.k8s.metrics.pkg.apis.metrics.v1beta1.NodeMetrics)1 Node (io.fabric8.kubernetes.api.model.Node)1 NodeStatus (io.fabric8.kubernetes.api.model.NodeStatus)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 NodeMetricsBuilder (io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsBuilder)1 NodeMetricsList (io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsList)1 NodeMetricsListBuilder (io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsListBuilder)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 NodeMetricOperationsImpl (io.fabric8.kubernetes.client.dsl.internal.NodeMetricOperationsImpl)1 BigDecimal (java.math.BigDecimal)1 HttpURLConnection (java.net.HttpURLConnection)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 DisplayName (org.junit.jupiter.api.DisplayName)1