Search in sources :

Example 1 with ListPodForAllNamespaces

use of com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces in project yakc by manusa.

the class WipIT method deployment.

@Test
void deployment() throws IOException {
    final AppsV1Api api = KC.create(AppsV1Api.class);
    final String deploymentName = "java-test";
    try {
        api.deleteNamespacedDeployment(deploymentName, "default").get();
        System.out.println("Deployment created");
    } catch (NotFoundException ex) {
        System.out.println("Deployment not found, deletion not needed");
    }
    final Deployment deployment = api.createNamespacedDeployment("default", Deployment.builder().metadata(ObjectMeta.builder().name(deploymentName).build()).spec(DeploymentSpec.builder().replicas(1).selector(LabelSelector.builder().putInMatchLabels("k8s-app", "test-java").build()).template(PodTemplateSpec.builder().metadata(ObjectMeta.builder().name("java-test-pod").putInLabels("k8s-app", "test-java").build()).spec(PodSpec.builder().addToContainers(Container.builder().image("containous/whoami").name("java-test-pod").build()).build()).build()).build()).build()).get();
    System.out.println(deployment);
    api.patchNamespacedDeployment(deploymentName, "default", Deployment.builder().spec(deployment.getSpec().toBuilder().replicas(2).build()).build()).get();
    final PodList selectedPodList = KC.create(CoreV1Api.class).listPodForAllNamespaces(new ListPodForAllNamespaces().labelSelector("k8s-app=test-pod")).get();
    System.out.println(selectedPodList);
}
Also used : PodList(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodList) AppsV1Api(com.marcnuri.yakc.api.apps.v1.AppsV1Api) NotFoundException(com.marcnuri.yakc.api.NotFoundException) Deployment(com.marcnuri.yakc.model.io.k8s.api.apps.v1.Deployment) ListPodForAllNamespaces(com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces) Test(org.junit.jupiter.api.Test)

Example 2 with ListPodForAllNamespaces

use of com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces in project yakc by manusa.

the class TopNodes method main.

public static void main(String[] args) {
    try (KubernetesClient kc = new KubernetesClient()) {
        final Node node = kc.create(CoreV1Api.class).listNode().stream().findFirst().orElseThrow(() -> new IllegalStateException("No nodes found in cluster"));
        final Map<String, String> nodeAllocatable = node.getStatus().getAllocatable();
        final double allocatableCpu = Resource.CPU.get(nodeAllocatable);
        final double allocatableMemory = Resource.MEMORY.get(nodeAllocatable);
        final double allocatablePods = Resource.PODS.get(nodeAllocatable);
        System.out.printf("Node %s allocatable resources, cpu: %s, memory: %s, pods %s%n", node.getMetadata().getName(), allocatableCpu, bytesToHumanReadable(allocatableMemory), (int) allocatablePods);
        System.out.printf(LOG_FORMAT, "POD (CONTAINER)", "CPU / LIMIT", "MEM / LIMIT");
        logSeparator();
        double totalCpu = 0D;
        double totalCpuLimit = 0D;
        double totalMemory = 0D;
        double totalMemoryLimit = 0D;
        final List<PodContainer> containers = kc.create(CoreV1Api.class).listPodForAllNamespaces(new ListPodForAllNamespaces().fieldSelector("spec.nodeName=" + node.getMetadata().getName())).stream().flatMap(p -> p.getSpec().getContainers().stream().map(c -> new PodContainer(p, c))).collect(Collectors.toList());
        for (PodContainer c : containers) {
            final Map<String, String> containerRequests = c.container.getResources().getRequests();
            final double cpu = Resource.CPU.get(containerRequests);
            final double memory = Resource.MEMORY.get(containerRequests);
            final Map<String, String> containerLimits = c.container.getResources().getLimits();
            final double cpuLimit = Resource.CPU.get(containerLimits);
            final double memoryLimit = Resource.MEMORY.get(containerLimits);
            System.out.printf(LOG_FORMAT, String.format("%s (%s)", c.pod.getMetadata().getName(), c.container.getName()), cpu + " / " + cpuLimit, bytesToHumanReadable(memory) + " / " + bytesToHumanReadable(memoryLimit));
            totalCpu += cpu;
            totalMemory += memory;
            totalCpuLimit += cpuLimit;
            totalMemoryLimit += memoryLimit;
        }
        logSeparator();
        System.out.printf(LOG_FORMAT, "TOTAL", totalCpu + " / " + totalCpuLimit, bytesToHumanReadable(totalMemory) + " / " + bytesToHumanReadable(totalMemoryLimit));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Node(com.marcnuri.yakc.model.io.k8s.api.core.v1.Node) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) List(java.util.List) Locale(java.util.Locale) ListPodForAllNamespaces(com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces) Map(java.util.Map) Container(com.marcnuri.yakc.model.io.k8s.api.core.v1.Container) Optional(java.util.Optional) KubernetesClient(com.marcnuri.yakc.KubernetesClient) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) KubernetesClient(com.marcnuri.yakc.KubernetesClient) Node(com.marcnuri.yakc.model.io.k8s.api.core.v1.Node) ListPodForAllNamespaces(com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)

Example 3 with ListPodForAllNamespaces

use of com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces 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

CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)2 ListPodForAllNamespaces (com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)2 Pod (com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod)2 Test (org.junit.jupiter.api.Test)2 KubernetesClient (com.marcnuri.yakc.KubernetesClient)1 NotFoundException (com.marcnuri.yakc.api.NotFoundException)1 AppsV1Api (com.marcnuri.yakc.api.apps.v1.AppsV1Api)1 MetricsV1beta1Api (com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api)1 Deployment (com.marcnuri.yakc.model.io.k8s.api.apps.v1.Deployment)1 Container (com.marcnuri.yakc.model.io.k8s.api.core.v1.Container)1 Node (com.marcnuri.yakc.model.io.k8s.api.core.v1.Node)1 PodList (com.marcnuri.yakc.model.io.k8s.api.core.v1.PodList)1 PodMetrics (com.marcnuri.yakc.model.io.k8s.metrics.pkg.apis.metrics.v1beta1.PodMetrics)1 Collections (java.util.Collections)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 DisplayName (org.junit.jupiter.api.DisplayName)1