Search in sources :

Example 1 with PodEvent

use of com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodEvent in project titus-control-plane by Netflix.

the class DefaultFabric8IOConnector method kubeInformerEvents.

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

            @Override
            public void onAdd(Pod pod) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                try {
                    String taskId = pod.getMetadata().getName();
                    Pod old = pods.get(taskId);
                    pods = pods.plus(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(Pod oldPod, Pod newPod) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                try {
                    String taskId = newPod.getMetadata().getName();
                    pods = pods.plus(taskId, 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(Pod pod, boolean deletedFinalStateUnknown) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                try {
                    String taskId = pod.getMetadata().getName();
                    pods = pods.minus(taskId);
                    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());
                }
            }
        };
        this.getPodInformer().addEventHandler(handler);
    // A listener cannot be removed from shared informer.
    // sink.onCancel(() -> ???);
    });
}
Also used : ResourceEventHandler(io.fabric8.kubernetes.client.informers.ResourceEventHandler) PodDeletedEvent(com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodDeletedEvent) Pod(io.fabric8.kubernetes.api.model.Pod) Stopwatch(com.google.common.base.Stopwatch) PodEvent(com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodEvent) PodUpdatedEvent(com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodUpdatedEvent)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 PodDeletedEvent (com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodDeletedEvent)1 PodEvent (com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodEvent)1 PodUpdatedEvent (com.netflix.titus.runtime.connector.kubernetes.fabric8io.model.PodUpdatedEvent)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 ResourceEventHandler (io.fabric8.kubernetes.client.informers.ResourceEventHandler)1