Search in sources :

Example 1 with PodMetrics

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

the class PodMetricsTest method testMetrics.

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

Example 2 with PodMetrics

use of io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetrics 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 3 with PodMetrics

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

the class MetricsTest method testPodMetricsNamespaceWithName.

@Test
void testPodMetricsNamespaceWithName() throws Exception {
    server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/namespaces/test/pods/test-pod").andReturn(200, getPodMetric()).once();
    PodMetrics podMetrics = client.top().pods().metrics("test", "test-pod");
    assertEquals("foo", podMetrics.getMetadata().getName());
}
Also used : PodMetrics(io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetrics) Test(org.junit.jupiter.api.Test)

Example 4 with PodMetrics

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

the class PodMetricsTest method testMetricsInNamespace.

@Test
void testMetricsInNamespace() {
    // Given
    server.expect().withPath("/apis/metrics.k8s.io/v1beta1/namespaces/the-namespace/pods/the-pod").andReturn(HTTP_OK, new PodMetricsListBuilder().addToItems(new PodMetricsBuilder().withNewMetadata().withName("the-metric").endMetadata().build()).build()).once();
    // When
    final PodMetricsList result = client.top().pods().inNamespace("the-namespace").withName("the-pod").metrics();
    // Then
    assertThat(result).extracting(PodMetricsList::getItems).asList().singleElement().hasFieldOrPropertyWithValue("Kind", "PodMetrics").hasFieldOrPropertyWithValue("metadata.name", "the-metric");
}
Also used : PodMetricsList(io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsList) PodMetricsBuilder(io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsBuilder) PodMetricsListBuilder(io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsListBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with PodMetrics

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

the class MetricsV1beta1ApiIT method readNamespacedPodMetrics.

@Test
@DisplayName("readNamespacedPodMetrics, cluster contains a Pod with some metrics")
void readNamespacedPodMetrics() throws IOException, InterruptedException {
    // Given
    final Pod pod = KC.create(CoreV1Api.class).listPodForAllNamespaces(new CoreV1Api.ListPodForAllNamespaces().labelSelector("k8s-app=metrics-server")).stream().findFirst().orElseThrow(() -> new AssertionError("No Pod found for metrics-server"));
    // When
    final PodMetrics result = getWithRetry(() -> KC.create(MetricsV1beta1Api.class).readNamespacedPodMetrics(pod.getMetadata().getName(), pod.getMetadata().getNamespace()).get());
    // Then
    assertThat(result).hasFieldOrProperty("apiVersion").hasFieldOrProperty("kind").hasFieldOrPropertyWithValue("metadata.name", pod.getMetadata().getName()).hasFieldOrPropertyWithValue("metadata.namespace", pod.getMetadata().getNamespace()).hasFieldOrProperty("window").hasFieldOrProperty("timestamp").extracting(PodMetrics::getContainers).asList().hasSizeGreaterThanOrEqualTo(1).allSatisfy(c -> assertThat(c).hasFieldOrProperty("usage.cpu").hasFieldOrProperty("usage.memory"));
}
Also used : Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) PodMetrics(com.marcnuri.yakc.model.io.k8s.metrics.pkg.apis.metrics.v1beta1.PodMetrics) MetricsV1beta1Api(com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Test (org.junit.jupiter.api.Test)4 PodMetricsBuilder (io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsBuilder)2 PodMetricsList (io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsList)2 PodMetricsListBuilder (io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsListBuilder)2 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)1 MetricsV1beta1Api (com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api)1 Pod (com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod)1 PodMetrics (com.marcnuri.yakc.model.io.k8s.metrics.pkg.apis.metrics.v1beta1.PodMetrics)1 PodMetrics (io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetrics)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 HttpURLConnection (java.net.HttpURLConnection)1 Optional (java.util.Optional)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1