Search in sources :

Example 41 with V1Pod

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

the class CacheTest method testCacheStore.

@Test
public void testCacheStore() {
    if (this.obj == null) {
        // skip null object storing test b/c it should be checked before invoking cache
        return;
    }
    cache.replace(Arrays.asList(this.obj), "0");
    cache.delete(this.obj);
    V1Pod pod = ((V1Pod) this.obj);
    List indexedObjectList = cache.byIndex(mockIndexName, this.index);
    assertEquals(0, indexedObjectList.size());
    assertEquals(null, pod.getMetadata().getClusterName());
    cache.add(this.obj);
    // replace cached object w/ null value
    String newClusterName = "test_cluster";
    pod.getMetadata().setClusterName(newClusterName);
    cache.update(this.obj);
    assertEquals(1, cache.list().size());
    assertEquals(newClusterName, pod.getMetadata().getClusterName());
}
Also used : V1Pod(io.kubernetes.client.openapi.models.V1Pod) List(java.util.List) Test(org.junit.Test)

Example 42 with V1Pod

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

the class ControllerTest method testControllerProcessDeltas.

@Test
public void testControllerProcessDeltas() {
    AtomicInteger receivingDeltasCount = new AtomicInteger(0);
    V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
    V1Pod foo2 = new V1Pod().metadata(new V1ObjectMeta().name("foo2").namespace("default"));
    V1Pod foo3 = new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default").resourceVersion("rva"));
    V1Pod foo3Updated = new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default").resourceVersion("rvb"));
    V1PodList podList = new V1PodList().metadata(new V1ListMeta()).items(Arrays.asList(foo1, foo2, foo3));
    DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, new Cache());
    ListerWatcher<V1Pod, V1PodList> listerWatcher = new MockRunOnceListerWatcher<V1Pod, V1PodList>(podList, new Watch.Response<V1Pod>(EventType.MODIFIED.name(), foo3Updated));
    Controller<V1Pod, V1PodList> controller = new Controller<>(V1Pod.class, deltaFIFO, listerWatcher, (deltas) -> {
        receivingDeltasCount.addAndGet(deltas.size());
    });
    Thread controllerThread = new Thread(controller::run);
    controllerThread.setDaemon(true);
    controllerThread.start();
    try {
        Awaitility.await().pollInterval(Duration.ofSeconds(1)).timeout(Duration.ofSeconds(5)).untilAtomic(receivingDeltasCount, IsEqual.equalTo(4));
        assertEquals(4, receivingDeltasCount.get());
    } catch (Throwable t) {
        throw new RuntimeException(t);
    } finally {
        controller.stop();
    }
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) V1PodList(io.kubernetes.client.openapi.models.V1PodList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 43 with V1Pod

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

the class ListerTest method testListerBasic.

@Test
public void testListerBasic() {
    Cache<V1Pod> podCache = new Cache<>();
    Lister<V1Pod> namespacedPodLister = new Lister<>(podCache, "default");
    List<V1Pod> emptyPodList = namespacedPodLister.list();
    assertEquals(0, emptyPodList.size());
    podCache.replace(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default")), new V1Pod().metadata(new V1ObjectMeta().name("foo2").namespace("default")), new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default"))), "0");
    List<V1Pod> namespacedPodList = namespacedPodLister.list();
    assertEquals(3, namespacedPodList.size());
    Lister<V1Pod> allNamespacedPodLister = new Lister<>(podCache);
    List<V1Pod> allPodList = allNamespacedPodLister.list();
    assertEquals(3, allPodList.size());
    namespacedPodList = allNamespacedPodLister.namespace("default").list();
    assertEquals(3, namespacedPodList.size());
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 44 with V1Pod

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

the class PodLogsTest method testNotFound.

@Test
public void testNotFound() throws ApiException, IOException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace)).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).willReturn(aResponse().withStatus(404).withHeader("Content-Type", "text/plain").withBody("Not Found")));
    PodLogs logs = new PodLogs(client);
    boolean thrown = false;
    try {
        logs.streamNamespacedPodLog(pod);
    } catch (ApiException ex) {
        assertEquals(404, ex.getCode());
        thrown = true;
    }
    assertEquals(thrown, true);
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).withQueryParam("container", equalTo(container)).withQueryParam("follow", equalTo("true")).withQueryParam("pretty", equalTo("false")).withQueryParam("previous", equalTo("false")).withQueryParam("timestamps", equalTo("false")));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 45 with V1Pod

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

the class PodLogsTest method testStream.

@Test
public void testStream() throws ApiException, IOException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace)).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
    String content = "this is some\n content for \n various logs \n done";
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody(content)));
    PodLogs logs = new PodLogs(client);
    InputStream is = logs.streamNamespacedPodLog(pod);
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).withQueryParam("container", equalTo(container)).withQueryParam("follow", equalTo("true")).withQueryParam("pretty", equalTo("false")).withQueryParam("previous", equalTo("false")).withQueryParam("timestamps", equalTo("false")));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Streams.copy(is, bos);
    assertEquals(content, bos.toString());
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) InputStream(java.io.InputStream) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) Test(org.junit.Test)

Aggregations

V1Pod (io.kubernetes.client.openapi.models.V1Pod)99 Test (org.junit.Test)55 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)38 V1PodList (io.kubernetes.client.openapi.models.V1PodList)33 Type (java.lang.reflect.Type)22 ApiException (io.kubernetes.client.openapi.ApiException)20 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)17 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)15 ApiClient (io.kubernetes.client.openapi.ApiClient)12 Watch (io.kubernetes.client.util.Watch)10 V1Status (io.kubernetes.client.openapi.models.V1Status)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 V1Container (io.kubernetes.client.openapi.models.V1Container)4