Search in sources :

Example 1 with KubernetesClient

use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.

the class CrdQuickstart method main.

public static void main(String[] args) {
    System.out.println("Complete Custom Resource Definition (CRD) example:\n" + " - Creates a namespace\n" + " - Deletes CRD if exists\n" + " - Creates CRD\n" + " - Creates custom resource - TV Show\n" + " - List entries for custom resource - TV Show\n" + " - Patch entry for custom resource - TV Show\n" + " - List patched entries for custom resource - TV Show\n" + "Starting...\n");
    try (KubernetesClient kc = new KubernetesClient()) {
        final CoreV1Api core = kc.create(CoreV1Api.class);
        final ApiextensionsV1Api extensions = kc.create(ApiextensionsV1Api.class);
        final ShowsV1Api shows = kc.create(ShowsV1Api.class);
        createNamespace(core);
        deleteCrdIfExists(extensions);
        createCrd(extensions);
        createShows(shows);
        listShows(shows);
        patchShow(shows);
        listShows(shows);
        deleteCrd(extensions);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : ApiextensionsV1Api(com.marcnuri.yakc.api.apiextensions.v1.ApiextensionsV1Api) KubernetesClient(com.marcnuri.yakc.KubernetesClient) IOException(java.io.IOException) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api)

Example 2 with KubernetesClient

use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.

the class PodExec method main.

public static void main(String[] args) {
    try (KubernetesClient kc = new KubernetesClient()) {
        final ExtendedCoreV1Api api = kc.create(ExtendedCoreV1Api.class);
        createPod(api);
        api.execInNamespacedPod(POD_NAME, NAMESPACE, Collections.singletonList("pwd")).exec().skip(1).subscribe(em -> System.out.printf("Current container Directory: %s", em.toString()));
        System.out.println("Running memory monitor for 5s in the background");
        api.execInNamespacedPod(POD_NAME, NAMESPACE, Arrays.asList("/bin/sh", "-c", "for i in $(seq 1 5); do cat /proc/meminfo | grep MemFree & sleep 1; done")).exec().subscribeOn(Schedulers.io()).skip(1).subscribe(t -> System.out.printf("         ++ %s", t.toString()), Throwable::printStackTrace);
        api.execInNamespacedPod(POD_NAME, NAMESPACE, Arrays.asList("/bin/sh", "-c", "echo 'Waiting 5S' && sleep 5" + "&& echo 'Hello World in error standard stream' >> /dev/stderr" + "&& echo 'Container execution completed, bye!'")).exec().skip(1).subscribe(t -> System.out.printf("%s: %s", t.getStandardStream(), t.toString()), Throwable::printStackTrace);
        System.out.println("Subscriptions completed, cleaning up.");
        deletePod(api);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : KubernetesClient(com.marcnuri.yakc.KubernetesClient) ExtendedCoreV1Api(com.marcnuri.yakc.apiextensions.ExtendedCoreV1Api) IOException(java.io.IOException) NotFoundException(com.marcnuri.yakc.api.NotFoundException)

Example 3 with KubernetesClient

use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.

the class PodLogs method main.

public static void main(String[] args) {
    final boolean follow = Stream.of(args).anyMatch(s -> s.matches("-*?follow"));
    try (KubernetesClient kc = new KubernetesClient()) {
        final CoreV1Api api = kc.create(CoreV1Api.class);
        createPod(api);
        final KubernetesCall<String> podLogCall = api.readNamespacedPodLog(POD_NAME, NAMESPACE, new ReadNamespacedPodLog().follow(follow).pretty("true").timestamps(true));
        if (follow) {
            final ExecutorService readLogService = Executors.newSingleThreadExecutor();
            final Future<Void> logReader = readLogService.submit(new LogReader(podLogCall));
            System.out.println("Log reading started in a parallel thread, following logs for a couple of seconds");
            Thread.sleep(10500L);
            logReader.cancel(true);
            readLogService.shutdownNow();
            System.out.println("Log thread finished");
        } else {
            System.out.println(podLogCall.get());
        }
        System.out.println("Cleaning up");
        api.deleteCollectionNamespacedPod(NAMESPACE, new DeleteCollectionNamespacedPod().gracePeriodSeconds(0).labelSelector(String.format("app=%s", POD_NAME))).get();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : KubernetesClient(com.marcnuri.yakc.KubernetesClient) DeleteCollectionNamespacedPod(com.marcnuri.yakc.api.core.v1.CoreV1Api.DeleteCollectionNamespacedPod) ExecutorService(java.util.concurrent.ExecutorService) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) ReadNamespacedPodLog(com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog) IOException(java.io.IOException) NotFoundException(com.marcnuri.yakc.api.NotFoundException)

Example 4 with KubernetesClient

use of com.marcnuri.yakc.KubernetesClient 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 5 with KubernetesClient

use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.

the class PodQuickstart method main.

public static void main(String[] args) {
    System.out.println("Complete POD example:\n" + " - Creates a namespace\n" + " - Deletes POD if exists\n" + " - Creates POD\n" + " - Patches POD's labels\n" + " - Updates POD removing annotations and adding another label\n" + "Starting...\n");
    try (KubernetesClient kc = new KubernetesClient()) {
        final CoreV1Api api = kc.create(CoreV1Api.class);
        final Disposable podMonitor = monitorPods(api);
        createNamespace(api);
        deletePodIfExists(api);
        createPod(api);
        final Pod patchedPod = patchPodLabels(api);
        System.out.printf("POD labels patched [%s]%n", patchedPod.getMetadata().getLabels());
        final Pod updatedPod = replacePod(patchedPod, api);
        System.out.printf("POD replaced: annotations removed, labels modified  [%s]%n", updatedPod.getMetadata().getLabels());
        podMonitor.dispose();
        System.out.println("Pod demo concluded successfully!");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) KubernetesClient(com.marcnuri.yakc.KubernetesClient) Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) IOException(java.io.IOException) NotFoundException(com.marcnuri.yakc.api.NotFoundException)

Aggregations

KubernetesClient (com.marcnuri.yakc.KubernetesClient)5 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)4 IOException (java.io.IOException)4 NotFoundException (com.marcnuri.yakc.api.NotFoundException)3 Pod (com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod)2 ApiextensionsV1Api (com.marcnuri.yakc.api.apiextensions.v1.ApiextensionsV1Api)1 DeleteCollectionNamespacedPod (com.marcnuri.yakc.api.core.v1.CoreV1Api.DeleteCollectionNamespacedPod)1 ListPodForAllNamespaces (com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)1 ReadNamespacedPodLog (com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog)1 ExtendedCoreV1Api (com.marcnuri.yakc.apiextensions.ExtendedCoreV1Api)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 Disposable (io.reactivex.disposables.Disposable)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 ExecutorService (java.util.concurrent.ExecutorService)1 Collectors (java.util.stream.Collectors)1