Search in sources :

Example 6 with V1ListMeta

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

the class GenericKubernetesApiTest method listNamespacedJobReturningObject.

@Test
public void listNamespacedJobReturningObject() {
    V1JobList jobList = new V1JobList().kind("JobList").metadata(new V1ListMeta());
    stubFor(get(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")).willReturn(aResponse().withStatus(200).withBody(json.serialize(jobList))));
    KubernetesApiResponse<V1JobList> jobListResp = jobClient.list("default");
    assertTrue(jobListResp.isSuccess());
    assertEquals(jobList, jobListResp.getObject());
    assertNull(jobListResp.getStatus());
    verify(1, getRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")));
}
Also used : V1JobList(io.kubernetes.client.openapi.models.V1JobList) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Example 7 with V1ListMeta

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

the class GenericKubernetesApiTest method watchNamespacedJobReturningObject.

@Test
public void watchNamespacedJobReturningObject() throws ApiException {
    V1JobList jobList = new V1JobList().kind("JobList").metadata(new V1ListMeta());
    stubFor(get(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")).willReturn(aResponse().withStatus(200).withBody(json.serialize(jobList))));
    Watchable<V1Job> jobListWatch = jobClient.watch("default", new ListOptions());
    verify(1, getRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")).withQueryParam("watch", equalTo("true")));
}
Also used : V1Job(io.kubernetes.client.openapi.models.V1Job) V1JobList(io.kubernetes.client.openapi.models.V1JobList) ListOptions(io.kubernetes.client.util.generic.options.ListOptions) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Example 8 with V1ListMeta

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

the class DefaultSharedIndexInformerWireMockTest method testAllNamespacedPodInformerNormalBehavior.

@Test
public void testAllNamespacedPodInformerNormalBehavior() 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/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).labels(Collections.singletonMap("foo", "bar")).annotations(Collections.singletonMap("foo", "bar"))));
    stubFor(get(urlPathEqualTo("/api/v1/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.listPodForAllNamespacesCall(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);
    podInformer.setTransform((obj) -> {
        // deepcopy
        String json = new JSON().serialize(obj);
        V1Pod pod = new JSON().deserialize(json, V1Pod.class);
        // remove pod annotations
        pod.getMetadata().setAnnotations(null);
        return pod;
    });
    AtomicBoolean foundExistingPod = new AtomicBoolean(false);
    AtomicBoolean transformed = new AtomicBoolean(false);
    AtomicBoolean setTransformAfterStarted = 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);
            }
            V1ObjectMeta metadata = obj.getMetadata();
            // check if the object was transformed
            if (metadata.getLabels().get("foo").equals("bar") && metadata.getAnnotations() == null) {
                transformed.set(true);
            }
        }

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

        @Override
        public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
        }
    });
    factory.startAllRegisteredInformers();
    Thread.sleep(1000);
    // can not set transform func if the informer has started
    try {
        podInformer.setTransform((obj) -> new V1Pod());
        setTransformAfterStarted.set(true);
    } catch (IllegalStateException e) {
    }
    assertTrue(foundExistingPod.get());
    assertTrue(transformed.get());
    assertFalse(setTransformAfterStarted.get());
    assertEquals(endRV, podInformer.lastSyncResourceVersion());
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
    verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/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 9 with V1ListMeta

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

the class DefaultSharedIndexInformerWireMockTest method testInformerReListWatchOnWatchConflict.

@Test
public void testInformerReListWatchOnWatchConflict() throws InterruptedException {
    CoreV1Api coreV1Api = new CoreV1Api(client);
    String startRV = "1000";
    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.ERROR.name(), new V1Status().apiVersion("v1").kind("Status").code(409));
    stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")).withQueryParam("resourceVersion", equalTo(startRV)).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);
    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")));
    verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")));
    factory.stopAllRegisteredInformers();
}
Also used : V1Status(io.kubernetes.client.openapi.models.V1Status) 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) 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 10 with V1ListMeta

use of io.kubernetes.client.openapi.models.V1ListMeta 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)

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