Search in sources :

Example 26 with V1PodList

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

the class DefaultControllerBuilderTest method testBuildWatchEventNotificationShouldWork.

@Test
public void testBuildWatchEventNotificationShouldWork() throws InterruptedException {
    V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().name("test-pod1")).spec(new V1PodSpec().hostname("hostname1"))));
    stubFor(get(urlPathEqualTo("/api/v1/pods")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
    CoreV1Api api = new CoreV1Api(client);
    SharedIndexInformer<V1Pod> podInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        return api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Pod.class, V1PodList.class);
    List<Request> keyFuncReceivingRequests = new ArrayList<>();
    Function<V1Pod, Request> podKeyFunc = (V1Pod pod) -> {
        // twisting pod name key
        Request request = new Request(pod.getSpec().getHostname() + "/" + pod.getMetadata().getName());
        keyFuncReceivingRequests.add(request);
        return request;
    };
    List<Request> controllerReceivingRequests = new ArrayList<>();
    final Semaphore latch = new Semaphore(1);
    latch.acquire();
    final Controller testController = ControllerBuilder.defaultBuilder(informerFactory).withReconciler(new Reconciler() {

        @Override
        public Result reconcile(Request request) {
            controllerReceivingRequests.add(request);
            latch.release();
            return new Result(false);
        }
    }).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Pod.class, workQueue).withWorkQueueKeyFunc(podKeyFunc).build()).build();
    controllerThead.submit(testController::run);
    informerFactory.startAllRegisteredInformers();
    // Wait for the request to be processed.
    latch.acquire(1);
    Request expectedRequest = new Request("hostname1/test-pod1");
    assertEquals(1, keyFuncReceivingRequests.size());
    assertEquals(expectedRequest, keyFuncReceivingRequests.get(0));
    assertEquals(1, controllerReceivingRequests.size());
    assertEquals(expectedRequest, controllerReceivingRequests.get(0));
}
Also used : V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) JSON(io.kubernetes.client.openapi.JSON) Arrays(java.util.Arrays) Result(io.kubernetes.client.extended.controller.reconciler.Result) V1PodList(io.kubernetes.client.openapi.models.V1PodList) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Function(java.util.function.Function) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) WireMock(com.github.tomakehurst.wiremock.client.WireMock) ApiClient(io.kubernetes.client.openapi.ApiClient) ArrayList(java.util.ArrayList) Request(io.kubernetes.client.extended.controller.reconciler.Request) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) After(org.junit.After) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Controller(io.kubernetes.client.extended.controller.Controller) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Executors(java.util.concurrent.Executors) ClientBuilder(io.kubernetes.client.util.ClientBuilder) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) List(java.util.List) Rule(org.junit.Rule) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) Assert(org.junit.Assert) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Request(io.kubernetes.client.extended.controller.reconciler.Request) ArrayList(java.util.ArrayList) JSON(io.kubernetes.client.openapi.JSON) Semaphore(java.util.concurrent.Semaphore) Controller(io.kubernetes.client.extended.controller.Controller) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Result(io.kubernetes.client.extended.controller.reconciler.Result) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

Example 27 with V1PodList

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

the class KubectlGetTest method testGetDefaultNamespacePods.

@Test
public void testGetDefaultNamespacePods() throws KubectlException {
    V1PodList podList = new V1PodList().items(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1")), new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo2"))));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/default/pods")).willReturn(aResponse().withStatus(200).withBody(apiClient.getJSON().serialize(podList))));
    List<V1Pod> pods = Kubectl.get(V1Pod.class).apiClient(apiClient).skipDiscovery().namespace("default").execute();
    wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods")));
    assertEquals(2, pods.size());
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 28 with V1PodList

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

the class KubectlDrain method doDrain.

private V1Node doDrain() throws KubectlException, ApiException, IOException {
    CoreV1Api api = new CoreV1Api(apiClient);
    V1Node node = performCordon();
    V1PodList allPods = api.listPodForAllNamespaces(null, null, "spec.nodeName=" + node.getMetadata().getName(), null, null, null, null, null, null, null);
    validatePods(allPods.getItems());
    for (V1Pod pod : allPods.getItems()) {
        deletePod(api, pod.getMetadata().getName(), pod.getMetadata().getNamespace());
    }
    return node;
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Node(io.kubernetes.client.openapi.models.V1Node) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 29 with V1PodList

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

the class GenericKubernetesApiForCoreApiTest method listClusterPodReturningObject.

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

Example 30 with V1PodList

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

the class DefaultSharedIndexInformerWireMockTest method testAllNamespacedPodInformerTransformFailure.

@Test
public void testAllNamespacedPodInformerTransformFailure() 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)));
    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) -> {
        throw new ObjectTransformException("test transform failure");
    });
    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);
    // cannot find the pod due to transform failure
    assertFalse(foundExistingPod.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) ObjectTransformException(io.kubernetes.client.informer.exception.ObjectTransformException) 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)

Aggregations

V1PodList (io.kubernetes.client.openapi.models.V1PodList)42 V1Pod (io.kubernetes.client.openapi.models.V1Pod)33 Test (org.junit.Test)22 ApiException (io.kubernetes.client.openapi.ApiException)18 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)17 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)16 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)13 ApiClient (io.kubernetes.client.openapi.ApiClient)12 Watch (io.kubernetes.client.util.Watch)9 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)7 JSON (io.kubernetes.client.openapi.JSON)6 V1Status (io.kubernetes.client.openapi.models.V1Status)6 List (java.util.List)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)5 Configuration (io.kubernetes.client.openapi.Configuration)5 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)5 IOException (java.io.IOException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Collectors (java.util.stream.Collectors)5