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");
}
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);
}
}
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());
}
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");
}
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"));
}
Aggregations