Search in sources :

Example 16 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.

the class ResourceTest method testWatch.

@Test
void testWatch() throws InterruptedException {
    Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withResourceVersion("1").withNamespace("test").and().build();
    server.expect().get().withPath("/api/v1/namespaces/test/pods").andReturn(200, pod1).once();
    server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(201, pod1).once();
    server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "DELETED")).done().always();
    final CountDownLatch latch = new CountDownLatch(1);
    Watch watch = client.resource(pod1).watch(new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            latch.countDown();
        }

        @Override
        public void onClose(WatcherException cause) {
        }
    });
    Assert.assertTrue(latch.await(5000, MILLISECONDS));
    watch.close();
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Watch(io.fabric8.kubernetes.client.Watch) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test)

Example 17 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.

the class WatchTest method testTryWithResourcesCantConnectShouldCloseAndThenThrowException.

@Test
@DisplayName("TryWithResources, receives error when connecting, should NOT receive events and close before propagating the connect exception")
void testTryWithResourcesCantConnectShouldCloseAndThenThrowException() throws Exception {
    // Given
    final CountDownLatch closeLatch = new CountDownLatch(1);
    server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andReturn(410, outdatedEvent()).once();
    final Watcher<Pod> watcher = new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            fail();
        }

        @Override
        public void onClose(WatcherException cause) {
            fail("Close event should be invoked by try-with-resources successful completion, not by exception");
        }

        @Override
        public void onClose() {
            closeLatch.countDown();
        }
    };
    final Watchable<Watcher<Pod>> watchable = client.pods().withName("pod1").withResourceVersion("1");
    // When
    final KubernetesClientException result = Assertions.assertThrows(KubernetesClientException.class, () -> {
        try (Watch watch = watchable.watch(watcher)) {
            assertNull(watch);
            fail("Close with resources should call Watcher#onClose when closing watch");
        }
    });
    // Then
    assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
    assertEquals(410, result.getCode());
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatcherException(io.fabric8.kubernetes.client.WatcherException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 18 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.

the class WatchTest method testOnCloseEvent.

@Test
void testOnCloseEvent() throws InterruptedException {
    final CountDownLatch eventLatch = new CountDownLatch(2);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "MODIFIED")).waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "MODIFIED")).done().once();
    Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            eventLatch.countDown();
        }

        @Override
        public void onClose(WatcherException cause) {
            fail();
        }

        @Override
        public void onClose() {
            closeLatch.countDown();
        }
    });
    assertTrue(eventLatch.await(10, TimeUnit.SECONDS));
    watch.close();
    assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test)

Example 19 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.

the class WatchTest method testTryWithResourcesConnectsThenReceivesEventBookmark.

@Test
@DisplayName("TryWithResources, connects and receives event then receives GONE, should receive first event and then close")
void testTryWithResourcesConnectsThenReceivesEventBookmark() throws InterruptedException {
    // Given
    server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "BOOKMARK")).waitFor(EVENT_WAIT_PERIOD_MS).andEmit(outdatedEvent()).done().once();
    final CountDownLatch bookmarkLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final Watcher<Pod> watcher = new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            if (action != BOOKMARK) {
                fail();
            }
            bookmarkLatch.countDown();
        }

        @Override
        public void onClose(WatcherException cause) {
            assertTrue(cause.isHttpGone());
            closeLatch.countDown();
        }
    };
    // When
    try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new ListOptionsBuilder().withAllowWatchBookmarks(true).build(), watcher)) {
        // Then
        assertNotNull(watch);
        assertTrue(bookmarkLatch.await(10, TimeUnit.SECONDS));
        assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 20 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.

the class WatchTest method testHttpErrorReconnect.

/**
 * Will attempt a reconnect after 10ms..20ms...40ms....80ms.....160ms......320ms
 */
@Test
void testHttpErrorReconnect() throws InterruptedException {
    // Given
    client.getConfiguration().setWatchReconnectInterval(10);
    final String path = "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true";
    // accept watch and disconnect
    server.expect().withPath(path).andUpgradeToWebSocket().open().done().once();
    // refuse reconnect attempts 6 times
    server.expect().withPath(path).andReturn(HttpURLConnection.HTTP_NOT_FOUND, new StatusBuilder().withCode(HttpURLConnection.HTTP_NOT_FOUND).build()).times(6);
    // accept next reconnect and send outdated event to stop the watch
    server.expect().withPath(path).andUpgradeToWebSocket().open(outdatedEvent()).done().once();
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final Watcher<Pod> watcher = new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            fail();
        }

        @Override
        public void onClose(WatcherException cause) {
            assertTrue(cause.isHttpGone());
            closeLatch.countDown();
        }
    };
    // When
    try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(watcher)) {
        // Then
        assertNotNull(watch);
        assertTrue(closeLatch.await(3, TimeUnit.MINUTES));
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher) StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test)

Aggregations

WatcherException (io.fabric8.kubernetes.client.WatcherException)63 Watch (io.fabric8.kubernetes.client.Watch)48 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)42 Watcher (io.fabric8.kubernetes.client.Watcher)35 Pod (io.fabric8.kubernetes.api.model.Pod)34 CountDownLatch (java.util.concurrent.CountDownLatch)27 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)23 Test (org.junit.jupiter.api.Test)22 Map (java.util.Map)15 List (java.util.List)14 ArrayList (java.util.ArrayList)13 DisplayName (org.junit.jupiter.api.DisplayName)12 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)11 Future (io.vertx.core.Future)11 ReconciliationLogger (io.strimzi.operator.common.ReconciliationLogger)10 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)9 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 Service (io.fabric8.kubernetes.api.model.Service)8 Annotations (io.strimzi.operator.common.Annotations)8