Search in sources :

Example 1 with Reconciler

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

the class DefaultControllerTest method testControllerKeepsWorkingWhenReconcilerAbortsWithRuntimeException.

@Test(timeout = 90000)
public void testControllerKeepsWorkingWhenReconcilerAbortsWithRuntimeException() throws InterruptedException {
    AtomicBoolean aborts = new AtomicBoolean(true);
    AtomicBoolean resumed = new AtomicBoolean(false);
    List<Request> finishedRequests = new ArrayList<>();
    final Semaphore latch = new Semaphore(1);
    DefaultController testController = new DefaultController("", new Reconciler() {

        @Override
        public Result reconcile(Request request) {
            try {
                if (aborts.get()) {
                    throw new RuntimeException("Oops!!");
                }
                resumed.set(true);
                finishedRequests.add(request);
                return new Result(false);
            } finally {
                latch.release();
            }
        }
    }, workQueue);
    testController.setWorkerCount(1);
    testController.setWorkerThreadPool(Executors.newScheduledThreadPool(1));
    controllerThead.submit(testController::run);
    Request request1 = new Request("test1");
    workQueue.add(request1);
    latch.acquire();
    aborts.set(false);
    // emit another event, the previous one has been backoff'd
    Request request2 = new Request("test2");
    workQueue.add(request2);
    latch.acquire();
    testController.shutdown();
    assertTrue(resumed.get());
    assertTrue(finishedRequests.size() >= 1);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Request(io.kubernetes.client.extended.controller.reconciler.Request) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Result(io.kubernetes.client.extended.controller.reconciler.Result) Test(org.junit.Test)

Example 2 with Reconciler

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

the class DefaultControllerBuilderTest method testBuildWatchShouldWorkIfInformerPresent.

@Test
public void testBuildWatchShouldWorkIfInformerPresent() {
    CoreV1Api api = new CoreV1Api();
    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);
    ControllerBuilder.defaultBuilder(informerFactory).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Pod.class, workQueue).build()).withReconciler(new Reconciler() {

        @Override
        public Result reconcile(Request request) {
            return new Result(false);
        }
    }).build();
}
Also used : Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Request(io.kubernetes.client.extended.controller.reconciler.Request) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Result(io.kubernetes.client.extended.controller.reconciler.Result) Test(org.junit.Test)

Example 3 with Reconciler

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

the class DefaultControllerBuilderTest method testControllerBuilderCustomizationShouldWork.

@Test
public void testControllerBuilderCustomizationShouldWork() {
    String testName = "test-controller";
    int testWorkerCount = 1024;
    ExecutorService threadPool = Executors.newCachedThreadPool();
    ControllerBuilder.defaultBuilder(informerFactory).withName(testName).withWorkerCount(testWorkerCount).withWorkQueue(null).withReconciler(new Reconciler() {

        @Override
        public Result reconcile(Request request) {
            return new Result(false);
        }
    }).build();
}
Also used : Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) ExecutorService(java.util.concurrent.ExecutorService) Request(io.kubernetes.client.extended.controller.reconciler.Request) Result(io.kubernetes.client.extended.controller.reconciler.Result) Test(org.junit.Test)

Example 4 with Reconciler

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

the class KubernetesControllerFactory method buildController.

private Controller buildController(SharedInformerFactory sharedInformerFactory, Reconciler r) throws BeansException {
    KubernetesReconciler kubernetesReconciler = r.getClass().getAnnotation(KubernetesReconciler.class);
    String reconcilerName = kubernetesReconciler.value();
    KubernetesReconcilerWatches watches = kubernetesReconciler.watches();
    DefaultControllerBuilder builder = ControllerBuilder.defaultBuilder(sharedInformerFactory);
    RateLimitingQueue<Request> workQueue = new DefaultRateLimitingQueue<>();
    builder = builder.withWorkQueue(workQueue);
    Map<Class, AddFilterAdaptor> addFilters = getAddFilters(watches, r);
    Map<Class, UpdateFilterAdaptor> updateFilters = getUpdateFilters(watches, r);
    Map<Class, DeleteFilterAdaptor> deleteFilters = getDeleteFilters(watches, r);
    List<ReadyFuncAdaptor> readyFuncs = getReadyFuncs(r);
    for (KubernetesReconcilerWatch watch : watches.value()) {
        try {
            Function<? extends KubernetesObject, Request> workQueueKeyFunc;
            try {
                workQueueKeyFunc = watch.workQueueKeyFunc().getConstructor(WorkQueue.class).newInstance(workQueue);
            } catch (NoSuchMethodException e) {
                workQueueKeyFunc = watch.workQueueKeyFunc().newInstance();
            } catch (InvocationTargetException e) {
                throw new BeanCreationException("Failed instantiating controller watch: " + e.getMessage());
            }
            final Function<? extends KubernetesObject, Request> finalWorkQueueKeyFunc = workQueueKeyFunc;
            builder = builder.watch((q) -> {
                return ControllerBuilder.controllerWatchBuilder(watch.apiTypeClass(), q).withOnAddFilter(addFilters.get(watch.apiTypeClass())).withOnUpdateFilter(updateFilters.get(watch.apiTypeClass())).withOnDeleteFilter(deleteFilters.get(watch.apiTypeClass())).withWorkQueueKeyFunc(finalWorkQueueKeyFunc).withResyncPeriod(Duration.ofMillis(watch.resyncPeriodMillis())).build();
            });
            for (Supplier<Boolean> readyFunc : readyFuncs) {
                builder = builder.withReadyFunc(readyFunc);
            }
        } catch (IllegalAccessException | InstantiationException e) {
            throw new BeanCreationException("Failed instantiating controller: " + e.getMessage());
        }
    }
    builder = builder.withWorkerCount(kubernetesReconciler.workerCount());
    return builder.withReconciler(r).withName(reconcilerName).build();
}
Also used : WorkQueue(io.kubernetes.client.extended.workqueue.WorkQueue) AddWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.AddWatchEventFilter) LoggerFactory(org.slf4j.LoggerFactory) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) HashMap(java.util.HashMap) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) DefaultControllerBuilder(io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) ArrayList(java.util.ArrayList) Request(io.kubernetes.client.extended.controller.reconciler.Request) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) KubernetesObject(io.kubernetes.client.common.KubernetesObject) Duration(java.time.Duration) Map(java.util.Map) DeleteWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.DeleteWatchEventFilter) DefaultRateLimitingQueue(io.kubernetes.client.extended.workqueue.DefaultRateLimitingQueue) Method(java.lang.reflect.Method) RateLimitingQueue(io.kubernetes.client.extended.workqueue.RateLimitingQueue) KubernetesReconcilerReadyFunc(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerReadyFunc) KubernetesReconcilerWatches(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches) Logger(org.slf4j.Logger) Controller(io.kubernetes.client.extended.controller.Controller) ControllerBuilder(io.kubernetes.client.extended.controller.builder.ControllerBuilder) Predicate(java.util.function.Predicate) Set(java.util.Set) BeansException(org.springframework.beans.BeansException) KubernetesReconciler(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) UpdateWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.UpdateWatchEventFilter) DefaultControllerBuilder(io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Request(io.kubernetes.client.extended.controller.reconciler.Request) DefaultRateLimitingQueue(io.kubernetes.client.extended.workqueue.DefaultRateLimitingQueue) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) InvocationTargetException(java.lang.reflect.InvocationTargetException) KubernetesReconciler(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler) KubernetesReconcilerWatches(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches)

Example 5 with Reconciler

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

Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)6 Request (io.kubernetes.client.extended.controller.reconciler.Request)6 Result (io.kubernetes.client.extended.controller.reconciler.Result)5 Controller (io.kubernetes.client.extended.controller.Controller)3 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)3 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)3 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ControllerBuilder (io.kubernetes.client.extended.controller.builder.ControllerBuilder)2 SharedIndexInformer (io.kubernetes.client.informer.SharedIndexInformer)2 ApiClient (io.kubernetes.client.openapi.ApiClient)2 Duration (java.time.Duration)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Semaphore (java.util.concurrent.Semaphore)2 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)1 KubernetesObject (io.kubernetes.client.common.KubernetesObject)1 ControllerManager (io.kubernetes.client.extended.controller.ControllerManager)1