Search in sources :

Example 6 with Result

use of io.kubernetes.client.extended.controller.reconciler.Result in project java by kubernetes-client.

the class DefaultControllerTest method testControllerWontStartBeforeReady.

@Test(timeout = 90000)
public void testControllerWontStartBeforeReady() throws InterruptedException {
    Request request1 = new Request("test1");
    final Semaphore latch = new Semaphore(1);
    when(mockReconciler.reconcile(request1)).thenAnswer(new Answer() {

        public Object answer(InvocationOnMock invocation) {
            latch.release();
            return new Result(false);
        }
    });
    latch.acquire();
    AtomicBoolean ready = new AtomicBoolean(false);
    DefaultController testController = new DefaultController("", mockReconciler, workQueue, () -> ready.get());
    testController.setWorkerCount(1);
    testController.setWorkerThreadPool(Executors.newScheduledThreadPool(1));
    testController.setReadyCheckInternal(Duration.ofMillis(100));
    controllerThead.submit(testController::run);
    // emit an event when the controller hasn't been ready
    workQueue.add(request1);
    // As above wrt sleep
    cooldown();
    verify(mockReconciler, times(0)).reconcile(request1);
    ready.set(true);
    latch.acquire();
    verify(mockReconciler, times(1)).reconcile(request1);
}
Also used : Answer(org.mockito.stubbing.Answer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Request(io.kubernetes.client.extended.controller.reconciler.Request) Semaphore(java.util.concurrent.Semaphore) Result(io.kubernetes.client.extended.controller.reconciler.Result) Test(org.junit.Test)

Example 7 with Result

use of io.kubernetes.client.extended.controller.reconciler.Result 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)

Aggregations

Request (io.kubernetes.client.extended.controller.reconciler.Request)7 Result (io.kubernetes.client.extended.controller.reconciler.Result)7 Test (org.junit.Test)6 Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)4 Semaphore (java.util.concurrent.Semaphore)4 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)2 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)1 Controller (io.kubernetes.client.extended.controller.Controller)1 SharedIndexInformer (io.kubernetes.client.informer.SharedIndexInformer)1 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 JSON (io.kubernetes.client.openapi.JSON)1 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)1