Search in sources :

Example 6 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.

the class DefaultSharedIndexInformerTest method testHasSynced.

@Test
@DisplayName("PodInformer's hasSynced() method should return false when it's not able to resync")
void testHasSynced() {
    // Given
    String startResourceVersion = "1000", endResourceVersion = "1001";
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(startResourceVersion).endMetadata().withItems(Collections.emptyList()).build()).once();
    server.expect().withPath("/api/v1/namespaces/test/pods?resourceVersion=" + startResourceVersion + "&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(WATCH_EVENT_EMIT_TIME).andEmit(new WatchEvent(new PodBuilder().withNewMetadata().withNamespace("test").withName("pod1").withResourceVersion(endResourceVersion).endMetadata().build(), "ADDED")).waitFor(OUTDATED_WATCH_EVENT_EMIT_TIME).andEmit(outdatedEvent).done().once();
    // When
    SharedIndexInformer<Pod> podInformer = factory.sharedIndexInformerFor(Pod.class, 2000L);
    podInformer.addEventHandler(new ResourceEventHandler<Pod>() {

        @Override
        public void onAdd(Pod obj) {
        }

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

        @Override
        public void onDelete(Pod oldObj, boolean deletedFinalStateUnknown) {
        }
    });
    factory.startAllRegisteredInformers();
    await().atMost(1, TimeUnit.SECONDS).until(() -> !podInformer.hasSynced());
    // Then
    assertFalse(podInformer.hasSynced());
}
Also used : PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 7 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.

the class DefaultSharedIndexInformerTest method shouldDeleteIfMissingOnResync.

@Test
@DisplayName("Pod Informer should delete the entry from the index")
void shouldDeleteIfMissingOnResync() throws InterruptedException {
    // Given
    String startResourceVersion = "1000", midResourceVersion = "1001", mid2ResourceVersion = "1002";
    server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(startResourceVersion).endMetadata().withItems(Collections.emptyList()).build()).once();
    server.expect().withPath("/api/v1/pods?resourceVersion=" + startResourceVersion + "&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(WATCH_EVENT_EMIT_TIME).andEmit(new WatchEvent(new PodBuilder().withNewMetadata().withNamespace("test").withName("pod1").withResourceVersion(midResourceVersion).endMetadata().build(), "ADDED")).waitFor(OUTDATED_WATCH_EVENT_EMIT_TIME).andEmit(outdatedEvent).done().always();
    server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(mid2ResourceVersion).endMetadata().withItems(Collections.emptyList()).build()).times(2);
    // When
    SharedIndexInformer<Pod> podInformer = factory.sharedIndexInformerFor(Pod.class, 10000L);
    CountDownLatch delete = new CountDownLatch(1);
    podInformer.addEventHandler(new ResourceEventHandler<Pod>() {

        @Override
        public void onAdd(Pod obj) {
        }

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

        @Override
        public void onDelete(Pod oldObj, boolean deletedFinalStateUnknown) {
            if (deletedFinalStateUnknown) {
                delete.countDown();
            }
        }
    });
    factory.startAllRegisteredInformers();
    delete.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
    // Then
    assertEquals(0, delete.getCount());
    assertEquals(0, podInformer.getIndexer().list().size());
}
Also used : PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 8 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.

the class DefaultSharedIndexInformerTest method testPodSetCustomResourceInformerShouldWatchInAllNamespaces.

@Test
@DisplayName("PodSet Informer should watch in all namespaces")
void testPodSetCustomResourceInformerShouldWatchInAllNamespaces() throws InterruptedException {
    // Given
    setupMockServerExpectations(PodSet.class, null, this::getList, r -> new WatchEvent(getPodSet("podset1", r), "ADDED"), null, null);
    // When
    SharedIndexInformer<PodSet> podSetSharedIndexInformer = factory.sharedIndexInformerForCustomResource(PodSet.class, 60 * WATCH_EVENT_EMIT_TIME);
    CountDownLatch foundExistingPodSet = new CountDownLatch(1);
    podSetSharedIndexInformer.addEventHandler(new TestResourceHandler<>(foundExistingPodSet, "podset1"));
    factory.startAllRegisteredInformers();
    foundExistingPodSet.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
    // Namespace set in Client's Configuration from MockWebServer
    assertEquals("test", client.getConfiguration().getNamespace());
    assertEquals(0, foundExistingPodSet.getCount());
}
Also used : PodSet(io.fabric8.kubernetes.client.mock.crd.PodSet) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 9 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.

the class DefaultSharedIndexInformerTest method testWithNamespaceInformer.

@Test
@DisplayName("Should create Informer for Namespace resource")
void testWithNamespaceInformer() throws InterruptedException {
    // Given
    setupMockServerExpectations(Namespace.class, null, this::getList, r -> new WatchEvent(new NamespaceBuilder().withNewMetadata().withName("ns1").withResourceVersion(r).endMetadata().build(), "ADDED"), null, null);
    // When
    SharedIndexInformer<Namespace> namespaceSharedIndexInformer = factory.sharedIndexInformerFor(Namespace.class, RESYNC_PERIOD);
    CountDownLatch foundExistingNamespace = new CountDownLatch(1);
    namespaceSharedIndexInformer.addEventHandler(new TestResourceHandler<>(foundExistingNamespace, "ns1"));
    factory.startAllRegisteredInformers();
    foundExistingNamespace.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
    // Then
    assertEquals(0, foundExistingNamespace.getCount());
}
Also used : WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Namespace(io.fabric8.kubernetes.api.model.Namespace) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 10 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.

the class DefaultSharedIndexInformerTest method testWithClusterBindingInformer.

@Test
@DisplayName("Should create informer for ClusterRoleBinding resource")
void testWithClusterBindingInformer() throws InterruptedException {
    // Given
    setupMockServerExpectations(ClusterRoleBinding.class, null, this::getList, r -> new WatchEvent(new ClusterRoleBindingBuilder().withNewMetadata().withName("crb1").withResourceVersion(r).endMetadata().build(), "ADDED"), null, null);
    // Given
    SharedIndexInformer<ClusterRoleBinding> clusterRoleBindingSharedIndexInformer = factory.sharedIndexInformerFor(ClusterRoleBinding.class, RESYNC_PERIOD);
    CountDownLatch foundExistingClusterRoleBinding = new CountDownLatch(1);
    clusterRoleBindingSharedIndexInformer.addEventHandler(new TestResourceHandler<>(foundExistingClusterRoleBinding, "crb1"));
    factory.startAllRegisteredInformers();
    foundExistingClusterRoleBinding.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
    // Then
    assertEquals(0, foundExistingClusterRoleBinding.getCount());
}
Also used : ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)61 Test (org.junit.jupiter.api.Test)56 CountDownLatch (java.util.concurrent.CountDownLatch)43 Pod (io.fabric8.kubernetes.api.model.Pod)39 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)32 DisplayName (org.junit.jupiter.api.DisplayName)25 PodListBuilder (io.fabric8.kubernetes.api.model.PodListBuilder)20 WatcherException (io.fabric8.kubernetes.client.WatcherException)10 Watch (io.fabric8.kubernetes.client.Watch)9 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)7 Status (io.fabric8.kubernetes.api.model.Status)7 StatusBuilder (io.fabric8.kubernetes.api.model.StatusBuilder)7 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)5 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 CronTab (io.fabric8.kubernetes.client.mock.crd.CronTab)5 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)3 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3