Search in sources :

Example 1 with V1ListMeta

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

the class KubernetesInformerCreatorTest method testInformerInjection.

@Test
public void testInformerInjection() throws InterruptedException {
    assertNotNull(podInformer);
    assertNotNull(configMapInformer);
    Semaphore getCount = new Semaphore(2);
    Semaphore watchCount = new Semaphore(2);
    Parameters getParams = new Parameters();
    Parameters watchParams = new Parameters();
    getParams.put("semaphore", getCount);
    watchParams.put("semaphore", watchCount);
    V1Pod foo1 = new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
    V1ConfigMap bar1 = new V1ConfigMap().kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1"));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/pods.*")).withPostServeAction("semaphore", getParams).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1PodList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(foo1))))));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/pods.*")).withPostServeAction("semaphore", watchParams).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody("{}")));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).withPostServeAction("semaphore", getParams).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1ConfigMapList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(bar1))))));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).withPostServeAction("semaphore", watchParams).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody("{}")));
    // These will be released for each web call above.
    getCount.acquire(2);
    watchCount.acquire(2);
    informerFactory.startAllRegisteredInformers();
    // Wait for the GETs to complete and the watches to start.
    getCount.acquire(2);
    watchCount.acquire(2);
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
    verify(getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")));
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/configmaps")).withQueryParam("watch", equalTo("false")));
    verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/configmaps")).withQueryParam("watch", equalTo("true")));
    assertEquals(1, new Lister<>(podInformer.getIndexer()).list().size());
    assertEquals(1, new Lister<>(configMapInformer.getIndexer()).list().size());
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) Parameters(com.github.tomakehurst.wiremock.extension.Parameters) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) JSON(io.kubernetes.client.openapi.JSON) Semaphore(java.util.concurrent.Semaphore) V1ConfigMapList(io.kubernetes.client.openapi.models.V1ConfigMapList) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with V1ListMeta

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

the class ReflectorRunnableTest method testReflectorRelistShouldHonorLastSyncResourceVersion.

@Test
public void testReflectorRelistShouldHonorLastSyncResourceVersion() {
    String expectedResourceVersion = "999";
    AtomicReference<String> requestedResourceVersion = new AtomicReference<>();
    ReflectorRunnable<V1Pod, V1PodList> reflectorRunnable = new ReflectorRunnable<>(V1Pod.class, new ListerWatcher<V1Pod, V1PodList>() {

        @Override
        public V1PodList list(CallGeneratorParams params) throws ApiException {
            requestedResourceVersion.set(params.resourceVersion);
            return new V1PodList().metadata(new V1ListMeta().resourceVersion(expectedResourceVersion));
        }

        @Override
        public Watchable<V1Pod> watch(CallGeneratorParams params) throws ApiException {
            throw new ApiException("HTTP GONE");
        }
    }, deltaFIFO);
    // first run
    try {
        Thread thread = new Thread(reflectorRunnable::run);
        thread.setDaemon(true);
        thread.start();
        Awaitility.await().atMost(Duration.ofSeconds(1)).pollInterval(Duration.ofMillis(100)).until(() -> expectedResourceVersion.equals(reflectorRunnable.getLastSyncResourceVersion()));
    } finally {
        reflectorRunnable.stop();
    }
    // second run
    try {
        Thread thread = new Thread(reflectorRunnable::run);
        thread.setDaemon(true);
        thread.start();
        Awaitility.await().atMost(Duration.ofSeconds(1)).pollInterval(Duration.ofMillis(100)).untilAtomic(requestedResourceVersion, new IsEqual<>(expectedResourceVersion));
    } finally {
        reflectorRunnable.stop();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) V1PodList(io.kubernetes.client.openapi.models.V1PodList) Watchable(io.kubernetes.client.util.Watchable) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 3 with V1ListMeta

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

the class ReflectorRunnableTest method testReflectorListShouldHandleExpiredResourceVersionFromWatchHandler.

@Test
public void testReflectorListShouldHandleExpiredResourceVersionFromWatchHandler() throws ApiException {
    String expectedResourceVersion = "100";
    when(listerWatcher.list(any())).thenReturn(new V1PodList().metadata(new V1ListMeta().resourceVersion(expectedResourceVersion)));
    V1Status v1Status = new V1Status();
    v1Status.setMessage("dummy-error-message");
    v1Status.setCode(410);
    when(listerWatcher.watch(any())).thenReturn(new MockWatch<>(new Watch.Response("Error", v1Status)));
    ReflectorRunnable<V1Pod, V1PodList> reflectorRunnable = new ReflectorRunnable<>(V1Pod.class, listerWatcher, deltaFIFO);
    try {
        Thread thread = new Thread(reflectorRunnable::run);
        thread.setDaemon(true);
        thread.start();
        Awaitility.await().atMost(Duration.ofSeconds(1)).pollInterval(Duration.ofMillis(100)).until(() -> expectedResourceVersion.equals(reflectorRunnable.getLastSyncResourceVersion()));
        assertTrue(reflectorRunnable.isLastSyncResourceVersionUnavailable());
    } finally {
        reflectorRunnable.stop();
    }
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Status(io.kubernetes.client.openapi.models.V1Status) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Example 4 with V1ListMeta

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

the class ReflectorRunnableTest method testReflectorRunOnce.

@Test
public void testReflectorRunOnce() throws ApiException {
    String mockResourceVersion = "1000";
    when(listerWatcher.list(any())).thenReturn(new V1PodList().metadata(new V1ListMeta().resourceVersion(mockResourceVersion)));
    when(listerWatcher.watch(any())).then((v) -> {
        // block forever
        Awaitility.await().forever();
        return null;
    });
    ReflectorRunnable<V1Pod, V1PodList> reflectorRunnable = new ReflectorRunnable<V1Pod, V1PodList>(V1Pod.class, listerWatcher, deltaFIFO);
    try {
        Thread thread = new Thread(reflectorRunnable::run);
        thread.setDaemon(true);
        thread.start();
        Awaitility.await().atMost(Duration.ofSeconds(1)).pollInterval(Duration.ofMillis(100)).until(() -> mockResourceVersion.equals(reflectorRunnable.getLastSyncResourceVersion()));
    } finally {
        reflectorRunnable.stop();
    }
    verify(deltaFIFO, times(1)).replace(any(), any());
    verify(deltaFIFO, never()).add(any());
    verify(listerWatcher, times(1)).list(any());
    verify(listerWatcher, times(1)).watch(any());
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Example 5 with V1ListMeta

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

the class GenericKubernetesApiForCoreApiTest method listNamespacedPodReturningObject.

@Test
public void listNamespacedPodReturningObject() {
    V1PodList podList = new V1PodList().kind("PodList").metadata(new V1ListMeta());
    stubFor(get(urlPathEqualTo("/api/v1/namespaces/default/pods")).willReturn(aResponse().withStatus(200).withBody(json.serialize(podList))));
    KubernetesApiResponse<V1PodList> podListResp = podClient.list("default");
    assertTrue(podListResp.isSuccess());
    assertEquals(podList, podListResp.getObject());
    assertNull(podListResp.getStatus());
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods")));
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Aggregations

V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)21 Test (org.junit.Test)20 V1PodList (io.kubernetes.client.openapi.models.V1PodList)17 V1Pod (io.kubernetes.client.openapi.models.V1Pod)15 ApiException (io.kubernetes.client.openapi.ApiException)8 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)8 Watch (io.kubernetes.client.util.Watch)7 JSON (io.kubernetes.client.openapi.JSON)6 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)6 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)5 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)5 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)4 V1Status (io.kubernetes.client.openapi.models.V1Status)4 V1JobList (io.kubernetes.client.openapi.models.V1JobList)3 ListOptions (io.kubernetes.client.util.generic.options.ListOptions)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Watchable (io.kubernetes.client.util.Watchable)2 Semaphore (java.util.concurrent.Semaphore)2 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 Parameters (com.github.tomakehurst.wiremock.extension.Parameters)1