Search in sources :

Example 61 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class DeltaFIFOTest method testDeltaFIFODedup.

@Test
public void testDeltaFIFODedup() {
    V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default").resourceVersion("ver"));
    Cache cache = new Cache();
    DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);
    Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> deltas;
    // add-delete dedup
    deltaFIFO.add(foo1);
    deltaFIFO.delete(foo1);
    deltas = deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
    assertEquals(DeltaFIFO.DeltaType.Deleted, deltas.peekLast().getLeft());
    assertEquals(foo1, deltas.peekLast().getRight());
    assertEquals(DeltaFIFO.DeltaType.Added, deltas.peekFirst().getLeft());
    assertEquals(foo1, deltas.peekFirst().getRight());
    assertEquals(2, deltas.size());
    deltaFIFO.getItems().remove(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
    // add-delete-delete dedup
    deltaFIFO.add(foo1);
    deltaFIFO.delete(foo1);
    deltaFIFO.delete(foo1);
    deltas = deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
    assertEquals(DeltaFIFO.DeltaType.Deleted, deltas.peekLast().getLeft());
    assertEquals(foo1, deltas.peekLast().getRight());
    assertEquals(DeltaFIFO.DeltaType.Added, deltas.peekFirst().getLeft());
    assertEquals(foo1, deltas.peekFirst().getRight());
    assertEquals(2, deltas.size());
    deltaFIFO.getItems().remove(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
    // add-sync dedupe
    deltaFIFO.add(foo1);
    deltaFIFO.replace(Collections.singletonList(foo1), foo1.getMetadata().getResourceVersion());
    deltas = deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
    assertEquals(1, deltas.size());
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 62 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class ProcessorListenerTest method testNotificationHandling.

@Test
public void testNotificationHandling() throws InterruptedException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name("foo").namespace("default"));
    final CountDownLatch cLatch = new CountDownLatch(3);
    ProcessorListener<V1Pod> listener = new ProcessorListener<>(new ResourceEventHandler<V1Pod>() {

        @Override
        public void onAdd(V1Pod obj) {
            assertEquals(pod, obj);
            addNotificationReceived = true;
            cLatch.countDown();
        }

        @Override
        public void onUpdate(V1Pod oldObj, V1Pod newObj) {
            assertEquals(pod, newObj);
            updateNotificationReceived = true;
            cLatch.countDown();
        }

        @Override
        public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
            assertEquals(pod, obj);
            deleteNotificationReceived = true;
            cLatch.countDown();
        }
    }, 0);
    listener.add(new ProcessorListener.AddNotification<>(pod));
    listener.add(new ProcessorListener.UpdateNotification<>(null, pod));
    listener.add(new ProcessorListener.DeleteNotification<>(pod));
    Thread listenerThread = new Thread(listener::run);
    listenerThread.setDaemon(true);
    listenerThread.start();
    // wait until consumption of notifications from queue
    cLatch.await();
    assertTrue(addNotificationReceived);
    assertTrue(updateNotificationReceived);
    assertTrue(deleteNotificationReceived);
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) CountDownLatch(java.util.concurrent.CountDownLatch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 63 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class ProcessorListenerTest method testMultipleNotificationsHandling.

@Test
public void testMultipleNotificationsHandling() throws InterruptedException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name("foo").namespace("default"));
    final int[] count = { 0 };
    int notificationCount = 2000;
    final CountDownLatch cLatch = new CountDownLatch(notificationCount);
    ProcessorListener<V1Pod> listener = new ProcessorListener<>(new ResourceEventHandler<V1Pod>() {

        @Override
        public void onAdd(V1Pod obj) {
            assertEquals(pod, obj);
            count[0]++;
            cLatch.countDown();
        }

        @Override
        public void onUpdate(V1Pod oldObj, V1Pod newObj) {
        }

        @Override
        public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
        }
    }, 0);
    for (int i = 0; i < notificationCount; i++) {
        listener.add(new ProcessorListener.AddNotification<>(pod));
    }
    Thread listenerThread = new Thread(listener);
    listenerThread.setDaemon(true);
    listenerThread.start();
    cLatch.await();
    assertEquals(count[0], 2000);
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 64 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class SharedProcessorTest method testListenerAddition.

@Test
public void testListenerAddition() throws InterruptedException {
    SharedProcessor<V1Pod> sharedProcessor = new SharedProcessor<>();
    V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
    ProcessorListener.Notification<V1Pod> addNotification = new ProcessorListener.AddNotification<V1Pod>(foo1);
    ProcessorListener.Notification<V1Pod> updateNotification = new ProcessorListener.UpdateNotification<V1Pod>(null, foo1);
    ProcessorListener.Notification<V1Pod> deleteNotification = new ProcessorListener.DeleteNotification<V1Pod>(foo1);
    ExpectingNoticationHandler<V1Pod> expectAddHandler = new ExpectingNoticationHandler<V1Pod>(addNotification);
    ExpectingNoticationHandler<V1Pod> expectUpdateHandler = new ExpectingNoticationHandler<V1Pod>(updateNotification);
    ExpectingNoticationHandler<V1Pod> expectDeleteHandler = new ExpectingNoticationHandler<V1Pod>(deleteNotification);
    sharedProcessor.addAndStartListener(expectAddHandler);
    sharedProcessor.addAndStartListener(expectUpdateHandler);
    sharedProcessor.addAndStartListener(expectDeleteHandler);
    sharedProcessor.distribute(addNotification, false);
    sharedProcessor.distribute(updateNotification, false);
    sharedProcessor.distribute(deleteNotification, false);
    // sleep 1s for notification distribution completing
    Thread.sleep(1000);
    assertTrue(expectAddHandler.isSatisfied());
    assertTrue(expectUpdateHandler.isSatisfied());
    assertTrue(expectDeleteHandler.isSatisfied());
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 65 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class GenericKubernetesApiForCoreApiTest method testReadTimeoutShouldThrowException.

@Test
public void testReadTimeoutShouldThrowException() {
    ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();
    apiClient.setHttpClient(apiClient.getHttpClient().newBuilder().readTimeout(1, // timeout everytime
    TimeUnit.MILLISECONDS).build());
    stubFor(get(urlEqualTo("/api/v1/namespaces/foo/pods/test")).willReturn(aResponse().withFixedDelay(99999).withStatus(200).withBody("")));
    podClient = new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
    try {
        KubernetesApiResponse<V1Pod> response = podClient.get("foo", "test");
    } catch (Throwable t) {
        assertTrue(t.getCause() instanceof SocketTimeoutException);
        return;
    }
    fail("no exception happened");
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) SocketTimeoutException(java.net.SocketTimeoutException) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) ClientBuilder(io.kubernetes.client.util.ClientBuilder) Test(org.junit.Test)

Aggregations

V1Pod (io.kubernetes.client.openapi.models.V1Pod)96 Test (org.junit.Test)55 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)38 V1PodList (io.kubernetes.client.openapi.models.V1PodList)31 Type (java.lang.reflect.Type)22 ApiException (io.kubernetes.client.openapi.ApiException)17 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)17 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)15 ApiClient (io.kubernetes.client.openapi.ApiClient)12 V1Status (io.kubernetes.client.openapi.models.V1Status)9 Watch (io.kubernetes.client.util.Watch)9 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)8 JSON (io.kubernetes.client.openapi.JSON)7 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)7 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)4 V1Patch (io.kubernetes.client.custom.V1Patch)4 V1PodBuilder (io.kubernetes.client.openapi.models.V1PodBuilder)4