Search in sources :

Example 1 with KubernetesReconcilerWatch

use of io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch in project java by kubernetes-client.

the class KubernetesControllerFactory method getDeleteFilters.

private Map<Class, DeleteFilterAdaptor> getDeleteFilters(KubernetesReconcilerWatches watches, Reconciler reconciler) {
    Map<Class, DeleteFilterAdaptor> filters = new HashMap<>();
    Set<Method> allAnnotatedMethods = new HashSet<>();
    Set<Method> adoptedMethods = new HashSet<>();
    for (KubernetesReconcilerWatch watch : watches.value()) {
        for (Method method : reconciler.getClass().getMethods()) {
            DeleteWatchEventFilter annotation = method.getAnnotation(DeleteWatchEventFilter.class);
            if (annotation == null) {
                continue;
            }
            if (watch.apiTypeClass().equals(annotation.apiTypeClass())) {
                if (filters.containsKey(watch.apiTypeClass())) {
                    log.warn("Duplicated watch DELETE event filter upon apiType {}", annotation.apiTypeClass());
                }
                filters.put(watch.apiTypeClass(), new DeleteFilterAdaptor(reconciler, method));
                adoptedMethods.add(method);
            }
            allAnnotatedMethods.add(method);
        }
    }
    allAnnotatedMethods.removeAll(adoptedMethods);
    if (allAnnotatedMethods.size() > 0) {
        log.warn("Dangling watch DELETE event filters {}", StringUtils.join(allAnnotatedMethods, ","));
    }
    return filters;
}
Also used : HashMap(java.util.HashMap) DeleteWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.DeleteWatchEventFilter) Method(java.lang.reflect.Method) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) HashSet(java.util.HashSet)

Example 2 with KubernetesReconcilerWatch

use of io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch 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 3 with KubernetesReconcilerWatch

use of io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch in project java by kubernetes-client.

the class KubernetesControllerFactory method getAddFilters.

private Map<Class, AddFilterAdaptor> getAddFilters(KubernetesReconcilerWatches watches, Reconciler reconciler) {
    Map<Class, AddFilterAdaptor> filters = new HashMap<>();
    Set<Method> allAnnotatedMethods = new HashSet<>();
    Set<Method> adoptedMethods = new HashSet<>();
    for (KubernetesReconcilerWatch watch : watches.value()) {
        for (Method method : reconciler.getClass().getMethods()) {
            AddWatchEventFilter annotation = method.getAnnotation(AddWatchEventFilter.class);
            if (annotation == null) {
                continue;
            }
            if (watch.apiTypeClass().equals(annotation.apiTypeClass())) {
                if (filters.containsKey(watch.apiTypeClass())) {
                    log.warn("Duplicated watch ADD event filter upon apiType {}", annotation.apiTypeClass());
                }
                filters.put(watch.apiTypeClass(), new AddFilterAdaptor(reconciler, method));
                adoptedMethods.add(method);
            }
            allAnnotatedMethods.add(method);
        }
    }
    allAnnotatedMethods.removeAll(adoptedMethods);
    if (allAnnotatedMethods.size() > 0) {
        log.warn("Dangling watch ADD event filters {}", StringUtils.join(allAnnotatedMethods, ","));
    }
    return filters;
}
Also used : AddWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.AddWatchEventFilter) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) HashSet(java.util.HashSet)

Example 4 with KubernetesReconcilerWatch

use of io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch in project java by kubernetes-client.

the class KubernetesControllerFactory method getUpdateFilters.

private Map<Class, UpdateFilterAdaptor> getUpdateFilters(KubernetesReconcilerWatches watches, Reconciler reconciler) {
    Map<Class, UpdateFilterAdaptor> filters = new HashMap<>();
    Set<Method> allAnnotatedMethods = new HashSet<>();
    Set<Method> adoptedMethods = new HashSet<>();
    for (KubernetesReconcilerWatch watch : watches.value()) {
        for (Method method : reconciler.getClass().getMethods()) {
            UpdateWatchEventFilter annotation = method.getAnnotation(UpdateWatchEventFilter.class);
            if (annotation == null) {
                continue;
            }
            if (watch.apiTypeClass().equals(annotation.apiTypeClass())) {
                if (filters.containsKey(watch.apiTypeClass())) {
                    log.warn("Duplicated watch UPDATE event filter upon apiType {}", annotation.apiTypeClass());
                }
                filters.put(watch.apiTypeClass(), new UpdateFilterAdaptor(reconciler, method));
                adoptedMethods.add(method);
            }
            allAnnotatedMethods.add(method);
        }
    }
    allAnnotatedMethods.removeAll(adoptedMethods);
    if (allAnnotatedMethods.size() > 0) {
        log.warn("Dangling watch UPDATE event filters {}", StringUtils.join(allAnnotatedMethods, ","));
    }
    return filters;
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) HashSet(java.util.HashSet) UpdateWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.UpdateWatchEventFilter)

Aggregations

KubernetesReconcilerWatch (io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch)4 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 AddWatchEventFilter (io.kubernetes.client.spring.extended.controller.annotation.AddWatchEventFilter)2 DeleteWatchEventFilter (io.kubernetes.client.spring.extended.controller.annotation.DeleteWatchEventFilter)2 UpdateWatchEventFilter (io.kubernetes.client.spring.extended.controller.annotation.UpdateWatchEventFilter)2 KubernetesObject (io.kubernetes.client.common.KubernetesObject)1 Controller (io.kubernetes.client.extended.controller.Controller)1 ControllerBuilder (io.kubernetes.client.extended.controller.builder.ControllerBuilder)1 DefaultControllerBuilder (io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder)1 Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)1 Request (io.kubernetes.client.extended.controller.reconciler.Request)1 DefaultRateLimitingQueue (io.kubernetes.client.extended.workqueue.DefaultRateLimitingQueue)1 RateLimitingQueue (io.kubernetes.client.extended.workqueue.RateLimitingQueue)1 WorkQueue (io.kubernetes.client.extended.workqueue.WorkQueue)1 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)1 KubernetesReconciler (io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler)1 KubernetesReconcilerReadyFunc (io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerReadyFunc)1 KubernetesReconcilerWatches (io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches)1