Search in sources :

Example 1 with Lister

use of io.fabric8.kubernetes.client.informers.cache.Lister in project kubernetes-client by fabric8io.

the class SharedInformerExample method main.

public static void main(String[] args) throws InterruptedException {
    try (final KubernetesClient client = new KubernetesClientBuilder().build()) {
        SharedInformerFactory sharedInformerFactory = client.informers();
        SharedIndexInformer<Pod> podInformer = sharedInformerFactory.sharedIndexInformerFor(Pod.class, 30 * 1000L);
        logger.info("Informer factory initialized.");
        podInformer.addEventHandler(new ResourceEventHandler<Pod>() {

            @Override
            public void onAdd(Pod pod) {
                logger.info("{} pod added", pod.getMetadata().getName());
            }

            @Override
            public void onUpdate(Pod oldPod, Pod newPod) {
                logger.info("{} pod updated", oldPod.getMetadata().getName());
            }

            @Override
            public void onDelete(Pod pod, boolean deletedFinalStateUnknown) {
                logger.info("{} pod deleted", pod.getMetadata().getName());
            }
        });
        logger.info("Starting all registered informers");
        sharedInformerFactory.startAllRegisteredInformers();
        Pod testPod = new PodBuilder().withNewMetadata().withName(POD_NAME).withLabels(Collections.singletonMap("app", POD_NAME)).endMetadata().withNewSpec().addNewContainer().withName("myapp-container").withImage("busybox:1.28").withCommand("sh", "-c", "echo The app is running!; sleep 10").endContainer().addNewInitContainer().withName("init-myservice").withImage("busybox:1.28").withCommand("sh", "-c", "echo inititalizing...; sleep 5").endInitContainer().endSpec().build();
        client.pods().inNamespace("default").create(testPod);
        logger.info("Pod created");
        Thread.sleep(3000L);
        Lister<Pod> podLister = new Lister<>(podInformer.getIndexer(), "default");
        Pod myPod = podLister.get(POD_NAME);
        logger.info("PodLister has {}", podLister.list().size());
        if (myPod != null) {
            logger.info("***** myapp-pod created {}", myPod.getMetadata().getCreationTimestamp());
        }
        // Wait for some time now
        TimeUnit.MINUTES.sleep(3);
        sharedInformerFactory.stopAllRegisteredInformers();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) SharedInformerFactory(io.fabric8.kubernetes.client.informers.SharedInformerFactory) Pod(io.fabric8.kubernetes.api.model.Pod) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) Lister(io.fabric8.kubernetes.client.informers.cache.Lister) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)1 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)1 SharedInformerFactory (io.fabric8.kubernetes.client.informers.SharedInformerFactory)1 Lister (io.fabric8.kubernetes.client.informers.cache.Lister)1