Search in sources :

Example 1 with Lister

use of io.kubernetes.client.informer.cache.Lister in project java by kubernetes-client.

the class InformerExample method main.

public static void main(String[] args) throws Exception {
    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);
    SharedInformerFactory factory = new SharedInformerFactory(apiClient);
    // Node informer
    SharedIndexInformer<V1Node> nodeInformer = factory.sharedIndexInformerFor(// the informer-factory.
    (CallGeneratorParams params) -> {
        return coreV1Api.listNodeCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Node.class, V1NodeList.class);
    nodeInformer.addEventHandler(new ResourceEventHandler<V1Node>() {

        @Override
        public void onAdd(V1Node node) {
            System.out.printf("%s node added!\n", node.getMetadata().getName());
        }

        @Override
        public void onUpdate(V1Node oldNode, V1Node newNode) {
            System.out.printf("%s => %s node updated!\n", oldNode.getMetadata().getName(), newNode.getMetadata().getName());
        }

        @Override
        public void onDelete(V1Node node, boolean deletedFinalStateUnknown) {
            System.out.printf("%s node deleted!\n", node.getMetadata().getName());
        }
    });
    factory.startAllRegisteredInformers();
    V1Node nodeToCreate = new V1Node();
    V1ObjectMeta metadata = new V1ObjectMeta();
    metadata.setName("noxu");
    nodeToCreate.setMetadata(metadata);
    V1Node createdNode = coreV1Api.createNode(nodeToCreate, null, null, null, null);
    Thread.sleep(3000);
    Lister<V1Node> nodeLister = new Lister<V1Node>(nodeInformer.getIndexer());
    V1Node node = nodeLister.get("noxu");
    System.out.printf("noxu created! %s\n", node.getMetadata().getCreationTimestamp());
    factory.stopAllRegisteredInformers();
    Thread.sleep(3000);
    System.out.println("informer stopped..");
}
Also used : OkHttpClient(okhttp3.OkHttpClient) V1Node(io.kubernetes.client.openapi.models.V1Node) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Lister(io.kubernetes.client.informer.cache.Lister) ApiClient(io.kubernetes.client.openapi.ApiClient) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 2 with Lister

use of io.kubernetes.client.informer.cache.Lister in project java by kubernetes-client.

the class KubernetesInformerFactoryProcessor method lister.

@SuppressWarnings({ "unchecked", "rawtypes" })
private <T extends KubernetesObject> Lister<T> lister(Class<T> type) {
    SharedIndexInformer sharedInformer = this.beanFactory.getBean(getInformerBeanName(type), SharedIndexInformer.class);
    Lister<T> lister = new Lister<>(sharedInformer.getIndexer());
    return lister;
}
Also used : Lister(io.kubernetes.client.informer.cache.Lister) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer)

Aggregations

Lister (io.kubernetes.client.informer.cache.Lister)2 SharedIndexInformer (io.kubernetes.client.informer.SharedIndexInformer)1 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1Node (io.kubernetes.client.openapi.models.V1Node)1 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)1 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)1 OkHttpClient (okhttp3.OkHttpClient)1