Search in sources :

Example 1 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.

the class PodIT method deleteNamespacedPod.

@Test
@DisplayName("deleteNamespacedPod, should delete existing Pod")
void deleteNamespacedPod() throws IOException {
    // When
    final Pod result = KC.create(CoreV1Api.class).deleteNamespacedPod(podName, NAMESPACE).get();
    // Then
    assertThat(result).isNotNull().extracting(Pod::getMetadata).hasFieldOrPropertyWithValue("name", podName).extracting(ObjectMeta::getDeletionTimestamp).isNotNull();
}
Also used : Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.

the class PodIT method awaitCreateWatch.

@Test
@DisplayName("listNamespacedPod.watch, should await for notification of newly created Pod")
void awaitCreateWatch() throws IOException {
    // Given
    final AtomicBoolean hasError = new AtomicBoolean(false);
    final AtomicBoolean hasCompleted = new AtomicBoolean(false);
    // When
    final Disposable d = KC.create(CoreV1Api.class).listNamespacedPod(NAMESPACE).watch().filter(we -> we.getObject().getMetadata().getName().equals(podName)).takeUntil(we -> we.getType() == Type.ADDED).timeout(20, TimeUnit.SECONDS).subscribe(we -> hasCompleted.set(true), we -> hasError.set(true));
    // Then
    assertThat(d).isNotNull();
    assertThat(hasError.get()).as("Watch subscribe ended with an error").isFalse();
    assertThat(hasCompleted.get()).as("Watch subscribe did not complete").isTrue();
}
Also used : Disposable(io.reactivex.disposables.Disposable) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Container(com.marcnuri.yakc.model.io.k8s.api.core.v1.Container) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PodSpec(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec) WebSocketListener(okhttp3.WebSocketListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ObjectMeta(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta) DeleteOptions(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions) NotFoundException(com.marcnuri.yakc.api.NotFoundException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ByteString(okio.ByteString) Response(okhttp3.Response) ExecMessage(com.marcnuri.yakc.api.ExecMessage) StandardStream(com.marcnuri.yakc.api.ExecMessage.StandardStream) Type(com.marcnuri.yakc.api.WatchEvent.Type) ExtendedCoreV1Api(com.marcnuri.yakc.apiextensions.ExtendedCoreV1Api) KC(com.marcnuri.yakc.KubernetesClientExtension.KC) WebSocket(okhttp3.WebSocket) IOException(java.io.IOException) UUID(java.util.UUID) Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) StandardCharsets(java.nio.charset.StandardCharsets) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) CountDownLatch(java.util.concurrent.CountDownLatch) Disposable(io.reactivex.disposables.Disposable) AfterEach(org.junit.jupiter.api.AfterEach) ReadNamespacedPodLog(com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog) KubernetesException(com.marcnuri.yakc.api.KubernetesException) Awaitility(org.awaitility.Awaitility) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod 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 4 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project java by kubernetes-client.

the class ProtoExample method main.

public static void main(String[] args) throws IOException, ApiException, InterruptedException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    ProtoClient pc = new ProtoClient(client);
    ObjectOrStatus<PodList> list = pc.list(PodList.newBuilder(), "/api/v1/namespaces/default/pods");
    if (list.object.getItemsCount() > 0) {
        Pod p = list.object.getItems(0);
        System.out.println(p);
    }
    Namespace namespace = Namespace.newBuilder().setMetadata(ObjectMeta.newBuilder().setName("test").build()).build();
    ObjectOrStatus<Namespace> ns = pc.create(namespace, "/api/v1/namespaces", "v1", "Namespace");
    System.out.println(ns);
    if (ns.object != null) {
        namespace = ns.object.toBuilder().setSpec(NamespaceSpec.newBuilder().addFinalizers("test").build()).build();
        // This is how you would update an object, but you can't actually
        // update namespaces, so this returns a 405
        ns = pc.update(namespace, "/api/v1/namespaces/test", "v1", "Namespace");
        System.out.println(ns.status);
    }
    ns = pc.delete(Namespace.newBuilder(), "/api/v1/namespaces/test");
    System.out.println(ns);
}
Also used : PodList(io.kubernetes.client.proto.V1.PodList) Pod(io.kubernetes.client.proto.V1.Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) Namespace(io.kubernetes.client.proto.V1.Namespace) ProtoClient(io.kubernetes.client.ProtoClient)

Example 5 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod 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)

Aggregations

Pod (com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod)7 Test (org.junit.jupiter.api.Test)6 DisplayName (org.junit.jupiter.api.DisplayName)5 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)4 NotFoundException (com.marcnuri.yakc.api.NotFoundException)3 KubernetesClient (com.marcnuri.yakc.KubernetesClient)2 ListPodForAllNamespaces (com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)2 Container (com.marcnuri.yakc.model.io.k8s.api.core.v1.Container)2 PodSpec (com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec)2 Disposable (io.reactivex.disposables.Disposable)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 KC (com.marcnuri.yakc.KubernetesClientExtension.KC)1 ExecMessage (com.marcnuri.yakc.api.ExecMessage)1 StandardStream (com.marcnuri.yakc.api.ExecMessage.StandardStream)1 KubernetesException (com.marcnuri.yakc.api.KubernetesException)1 Type (com.marcnuri.yakc.api.WatchEvent.Type)1 AppsV1Api (com.marcnuri.yakc.api.apps.v1.AppsV1Api)1 ReadNamespacedPodLog (com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog)1 MetricsV1beta1Api (com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api)1