Search in sources :

Example 1 with ReadNamespacedPodLog

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

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

the class PodIT method readNamespacedPodLog.

@Test
@DisplayName("readNamespacedPodLog, should wait for pod to start and retrieve logs")
void readNamespacedPodLog() throws IOException {
    // Given
    awaitPodReady();
    // When
    final String podLog = KC.create(CoreV1Api.class).readNamespacedPodLog(podName, NAMESPACE, new ReadNamespacedPodLog().timestamps(true)).get();
    // Then
    assertThat(podLog).contains("Busybox for IT started");
}
Also used : ByteString(okio.ByteString) ReadNamespacedPodLog(com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ReadNamespacedPodLog (com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog)2 KubernetesClient (com.marcnuri.yakc.KubernetesClient)1 NotFoundException (com.marcnuri.yakc.api.NotFoundException)1 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)1 DeleteCollectionNamespacedPod (com.marcnuri.yakc.api.core.v1.CoreV1Api.DeleteCollectionNamespacedPod)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ByteString (okio.ByteString)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1