Search in sources :

Example 1 with ResourceEventHandler

use of io.kubernetes.client.informer.ResourceEventHandler in project micronaut-kubernetes by micronaut-projects.

the class ResourceEventHandlerBeanListener method onCreated.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public ResourceEventHandler<ApiType> onCreated(BeanCreatedEvent<ResourceEventHandler<ApiType>> event) {
    BeanDefinition<ResourceEventHandler<ApiType>> beanDefinition = event.getBeanDefinition();
    if (beanDefinition.hasAnnotation(Informer.class)) {
        AnnotationValue<Informer> annotationValue = beanDefinition.getAnnotation(Informer.class);
        if (annotationValue != null) {
            ResourceEventHandler resourceEventHandler = event.getBean();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Found @Informer annotation on {}", resourceEventHandler);
            }
            final Class<? extends KubernetesObject> apiType = InformerAnnotationUtils.resolveApiType(annotationValue);
            final Class<? extends KubernetesListObject> apiListType = InformerAnnotationUtils.resolveApiListType(annotationValue);
            final String resourcePlural = resourcePluralResolver.resolveInformerResourcePlural(annotationValue);
            final String apiGroup = apiGroupResolver.resolveInformerApiGroup(annotationValue);
            final Set<String> namespaces = namespaceResolver.resolveInformerNamespaces(annotationValue);
            final String labelSelector = labelSelectorResolver.resolveInformerLabels(annotationValue);
            final Long resyncCheckPeriod = annotationValue.get("resyncCheckPeriod", Long.class).orElse(0L);
            List<SharedIndexInformer<? extends KubernetesObject>> informers = sharedIndexInformerFactory.sharedIndexInformersFor(apiType, apiListType, resourcePlural, apiGroup, new ArrayList<>(namespaces), labelSelector, resyncCheckPeriod, true);
            informers.forEach(i -> i.addEventHandler(resourceEventHandler));
        } else {
            if (LOG.isErrorEnabled()) {
                LOG.error("Failed to create informer for the class [{}] that implements ResourceEventHandler. " + "The io.micronaut.kubernetes.informer.@Informer annotation is missing.", "vajco");
            }
        }
    }
    return event.getBean();
}
Also used : KubernetesObject(io.kubernetes.client.common.KubernetesObject) ResourceEventHandler(io.kubernetes.client.informer.ResourceEventHandler) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer)

Example 2 with ResourceEventHandler

use of io.kubernetes.client.informer.ResourceEventHandler in project titus-control-plane by Netflix.

the class DefaultDirectKubeApiServerIntegrator method kubeInformerEvents.

private Flux<PodEvent> kubeInformerEvents() {
    return Flux.create(sink -> {
        ResourceEventHandler<V1Pod> handler = new ResourceEventHandler<V1Pod>() {

            @Override
            public void onAdd(V1Pod pod) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                try {
                    if (!KubeUtil.isOwnedByKubeScheduler(pod)) {
                        return;
                    }
                    String taskId = pod.getSpec().getContainers().get(0).getName();
                    V1Pod old = pods.get(taskId);
                    pods.put(taskId, pod);
                    PodEvent podEvent;
                    if (old != null) {
                        podEvent = PodEvent.onUpdate(old, pod, findNode(pod));
                        metrics.onUpdate(pod);
                    } else {
                        podEvent = PodEvent.onAdd(pod);
                        metrics.onAdd(pod);
                    }
                    sink.next(podEvent);
                    logger.info("Pod Added: pod={}, sequenceNumber={}", formatPodEssentials(pod), podEvent.getSequenceNumber());
                    logger.debug("complete pod data: {}", pod);
                } finally {
                    logger.info("Pod informer onAdd: pod={}, elapsedMs={}", pod.getMetadata().getName(), stopwatch.elapsed().toMillis());
                }
            }

            @Override
            public void onUpdate(V1Pod oldPod, V1Pod newPod) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                try {
                    if (!KubeUtil.isOwnedByKubeScheduler(newPod)) {
                        return;
                    }
                    metrics.onUpdate(newPod);
                    pods.put(newPod.getSpec().getContainers().get(0).getName(), newPod);
                    PodUpdatedEvent podEvent = PodEvent.onUpdate(oldPod, newPod, findNode(newPod));
                    sink.next(podEvent);
                    logger.info("Pod Updated: old={}, new={}, sequenceNumber={}", formatPodEssentials(oldPod), formatPodEssentials(newPod), podEvent.getSequenceNumber());
                    logger.debug("Complete pod data: old={}, new={}", oldPod, newPod);
                } finally {
                    logger.info("Pod informer onUpdate: pod={}, elapsedMs={}", newPod.getMetadata().getName(), stopwatch.elapsed().toMillis());
                }
            }

            @Override
            public void onDelete(V1Pod pod, boolean deletedFinalStateUnknown) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                try {
                    if (!KubeUtil.isOwnedByKubeScheduler(pod)) {
                        return;
                    }
                    metrics.onDelete(pod);
                    pods.remove(pod.getSpec().getContainers().get(0).getName());
                    PodDeletedEvent podEvent = PodEvent.onDelete(pod, deletedFinalStateUnknown, findNode(pod));
                    sink.next(podEvent);
                    logger.info("Pod Deleted: {}, deletedFinalStateUnknown={}, sequenceNumber={}", formatPodEssentials(pod), deletedFinalStateUnknown, podEvent.getSequenceNumber());
                    logger.debug("complete pod data: {}", pod);
                } finally {
                    logger.info("Pod informer onDelete: pod={}, elapsedMs={}", pod.getMetadata().getName(), stopwatch.elapsed().toMillis());
                }
            }
        };
        kubeApiFacade.getPodInformer().addEventHandler(handler);
    // A listener cannot be removed from shared informer.
    // sink.onCancel(() -> ???);
    });
}
Also used : ResourceEventHandler(io.kubernetes.client.informer.ResourceEventHandler) PodDeletedEvent(com.netflix.titus.master.kubernetes.client.model.PodDeletedEvent) Stopwatch(com.google.common.base.Stopwatch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) PodEvent(com.netflix.titus.master.kubernetes.client.model.PodEvent) PodUpdatedEvent(com.netflix.titus.master.kubernetes.client.model.PodUpdatedEvent)

Aggregations

ResourceEventHandler (io.kubernetes.client.informer.ResourceEventHandler)2 Stopwatch (com.google.common.base.Stopwatch)1 PodDeletedEvent (com.netflix.titus.master.kubernetes.client.model.PodDeletedEvent)1 PodEvent (com.netflix.titus.master.kubernetes.client.model.PodEvent)1 PodUpdatedEvent (com.netflix.titus.master.kubernetes.client.model.PodUpdatedEvent)1 KubernetesObject (io.kubernetes.client.common.KubernetesObject)1 SharedIndexInformer (io.kubernetes.client.informer.SharedIndexInformer)1 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1