Search in sources :

Example 71 with V1Pod

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

the class DefaultSharedIndexInformerWireMockTest method testNamespacedPodInformerNormalBehavior.

@Test
public void testNamespacedPodInformerNormalBehavior() throws InterruptedException {
    CoreV1Api coreV1Api = new CoreV1Api(client);
    String startRV = "1000";
    String endRV = "1001";
    V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
    stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
    Watch.Response<V1Pod> watchResponse = new Watch.Response<>(EventType.ADDED.name(), new V1Pod().metadata(new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV)));
    stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(watchResponse))));
    SharedInformerFactory factory = new SharedInformerFactory();
    SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        try {
            return coreV1Api.listNamespacedPodCall(namespace, null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }, V1Pod.class, V1PodList.class);
    AtomicBoolean foundExistingPod = new AtomicBoolean(false);
    podInformer.addEventHandler(new ResourceEventHandler<V1Pod>() {

        @Override
        public void onAdd(V1Pod obj) {
            if (podName.equals(obj.getMetadata().getName()) && namespace.equals(obj.getMetadata().getNamespace())) {
                foundExistingPod.set(true);
            }
        }

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

        @Override
        public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
        }
    });
    factory.startAllRegisteredInformers();
    Thread.sleep(1000);
    assertEquals(true, foundExistingPod.get());
    assertEquals(endRV, podInformer.lastSyncResourceVersion());
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")));
    verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")));
    factory.stopAllRegisteredInformers();
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) JSON(io.kubernetes.client.openapi.JSON) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) V1PodList(io.kubernetes.client.openapi.models.V1PodList) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 72 with V1Pod

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

the class DefaultSharedIndexInformerWireMockTest method testInformerReListingOnListForbidden.

@Test
public void testInformerReListingOnListForbidden() throws InterruptedException {
    CoreV1Api coreV1Api = new CoreV1Api(client);
    stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(403).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(new V1Status().apiVersion("v1").kind("Status").code(403).reason("RBAC forbidden")))));
    SharedInformerFactory factory = new SharedInformerFactory();
    SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        try {
            return coreV1Api.listNamespacedPodCall(namespace, null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }, V1Pod.class, V1PodList.class);
    factory.startAllRegisteredInformers();
    // Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
    Thread.sleep(3000);
    verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")));
    factory.stopAllRegisteredInformers();
}
Also used : V1Status(io.kubernetes.client.openapi.models.V1Status) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) V1Pod(io.kubernetes.client.openapi.models.V1Pod) JSON(io.kubernetes.client.openapi.JSON) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 73 with V1Pod

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

the class CacheTest method data.

@Parameterized.Parameters
public static Collection data() {
    V1Pod normalPod = new V1Pod();
    V1ObjectMeta normalPodMeta = new V1ObjectMeta();
    normalPodMeta.setName("foo");
    normalPodMeta.setNamespace("default");
    normalPod.setMetadata(normalPodMeta);
    V1Pod missingNamespacePod = new V1Pod();
    V1ObjectMeta missingNamespacePodMeta = new V1ObjectMeta();
    missingNamespacePodMeta.setName("foo");
    missingNamespacePodMeta.setNamespace(null);
    missingNamespacePod.setMetadata(missingNamespacePodMeta);
    V1Pod missingNamePod = new V1Pod();
    V1ObjectMeta missingNamePodMeta = new V1ObjectMeta();
    missingNamePodMeta.setName(null);
    missingNamePodMeta.setNamespace("default");
    missingNamePod.setMetadata(missingNamePodMeta);
    return Arrays.asList(new Object[][] { { normalPod, "io.kubernetes.client.openapi.models.V1Pod" }, { missingNamespacePod, "io.kubernetes.client.openapi.models.V1Pod" }, { missingNamePod, "io.kubernetes.client.openapi.models.V1Pod" }, { null, "null" } });
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod)

Example 74 with V1Pod

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

the class CacheTest method testMultiIndexFuncCacheStore.

@Test
public void testMultiIndexFuncCacheStore() {
    String testIndexFuncName = "test-idx-func";
    Cache<V1Pod> podCache = new Cache<>();
    podCache.addIndexFunc(testIndexFuncName, (V1Pod pod) -> {
        return Arrays.asList(pod.getSpec().getNodeName());
    });
    V1Pod testPod = new V1Pod().metadata(new V1ObjectMeta().namespace("ns").name("n")).spec(new V1PodSpec().nodeName("node1"));
    podCache.add(testPod);
    List<V1Pod> namespaceIndexedPods = podCache.byIndex(Caches.NAMESPACE_INDEX, "ns");
    assertEquals(1, namespaceIndexedPods.size());
    List<V1Pod> nodeNameIndexedPods = podCache.byIndex(testIndexFuncName, "node1");
    assertEquals(1, nodeNameIndexedPods.size());
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) Test(org.junit.Test)

Example 75 with V1Pod

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

the class YamlTest method testDateTimeRoundTrip.

@Test
public void testDateTimeRoundTrip() {
    // This test verifies that its fixed.
    try {
        String data = Resources.toString(CREATED_TIMESTAMP_FILE, UTF_8);
        V1Pod pod = Yaml.loadAs(data, V1Pod.class);
        String output = Yaml.dump(pod);
        V1Pod pod2 = Yaml.loadAs(output, V1Pod.class);
        assertEquals(pod, pod2);
    } catch (Exception ex) {
        assertNull("Unexpected exception: " + ex.toString(), ex);
    }
}
Also used : V1Pod(io.kubernetes.client.openapi.models.V1Pod) IOException(java.io.IOException) 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