Search in sources :

Example 1 with ControllerManager

use of io.kubernetes.client.extended.controller.ControllerManager in project java by kubernetes-client.

the class ControllerExample method main.

public static void main(String[] args) throws IOException {
    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);
    // instantiating an informer-factory, and there should be only one informer-factory
    // globally.
    SharedInformerFactory informerFactory = new SharedInformerFactory();
    // registering node-informer into the informer-factory.
    SharedIndexInformer<V1Node> nodeInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        return coreV1Api.listNodeCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Node.class, V1NodeList.class);
    informerFactory.startAllRegisteredInformers();
    EventBroadcaster eventBroadcaster = new LegacyEventBroadcaster(coreV1Api);
    // nodeReconciler prints node information on events
    NodePrintingReconciler nodeReconciler = new NodePrintingReconciler(nodeInformer, eventBroadcaster.newRecorder(new V1EventSource().host("localhost").component("node-printer")));
    // Use builder library to construct a default controller.
    Controller controller = ControllerBuilder.defaultBuilder(informerFactory).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Node.class, workQueue).withWorkQueueKeyFunc((V1Node node) -> new Request(node.getMetadata().getName())).withOnAddFilter((V1Node createdNode) -> createdNode.getMetadata().getName().startsWith(// optional, set onAdd filter
    "docker-")).withOnUpdateFilter((V1Node oldNode, V1Node newNode) -> newNode.getMetadata().getName().startsWith(// optional, set onUpdate filter
    "docker-")).withOnDeleteFilter((V1Node deletedNode, Boolean stateUnknown) -> deletedNode.getMetadata().getName().startsWith(// optional, set onDelete filter
    "docker-")).build()).withReconciler(// required, set the actual reconciler
    nodeReconciler).withName(// optional, set name for controller
    "node-printing-controller").withWorkerCount(// optional, set worker thread count
    4).withReadyFunc(// optional, only starts controller when the
    nodeInformer::hasSynced).build();
    // Use builder library to manage one or multiple controllers.
    ControllerManager controllerManager = ControllerBuilder.controllerManagerBuilder(informerFactory).addController(controller).build();
    LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(new EndpointsLock("kube-system", "leader-election", "foo"), Duration.ofMillis(10000), Duration.ofMillis(8000), Duration.ofMillis(5000))), controllerManager);
    leaderElectingController.run();
}
Also used : EventBroadcaster(io.kubernetes.client.extended.event.legacy.EventBroadcaster) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) V1NodeList(io.kubernetes.client.openapi.models.V1NodeList) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) Result(io.kubernetes.client.extended.controller.reconciler.Result) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) V1Node(io.kubernetes.client.openapi.models.V1Node) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) ApiClient(io.kubernetes.client.openapi.ApiClient) Request(io.kubernetes.client.extended.controller.reconciler.Request) Lister(io.kubernetes.client.informer.cache.Lister) ControllerManager(io.kubernetes.client.extended.controller.ControllerManager) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) Duration(java.time.Duration) EventRecorder(io.kubernetes.client.extended.event.legacy.EventRecorder) EventBroadcaster(io.kubernetes.client.extended.event.legacy.EventBroadcaster) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) EventType(io.kubernetes.client.extended.event.EventType) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) Controller(io.kubernetes.client.extended.controller.Controller) ControllerBuilder(io.kubernetes.client.extended.controller.builder.ControllerBuilder) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) OkHttpClient(okhttp3.OkHttpClient) V1Node(io.kubernetes.client.openapi.models.V1Node) Request(io.kubernetes.client.extended.controller.reconciler.Request) ControllerManager(io.kubernetes.client.extended.controller.ControllerManager) ApiClient(io.kubernetes.client.openapi.ApiClient) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) Controller(io.kubernetes.client.extended.controller.Controller) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Aggregations

Controller (io.kubernetes.client.extended.controller.Controller)1 ControllerManager (io.kubernetes.client.extended.controller.ControllerManager)1 LeaderElectingController (io.kubernetes.client.extended.controller.LeaderElectingController)1 ControllerBuilder (io.kubernetes.client.extended.controller.builder.ControllerBuilder)1 Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)1 Request (io.kubernetes.client.extended.controller.reconciler.Request)1 Result (io.kubernetes.client.extended.controller.reconciler.Result)1 EventType (io.kubernetes.client.extended.event.EventType)1 EventBroadcaster (io.kubernetes.client.extended.event.legacy.EventBroadcaster)1 EventRecorder (io.kubernetes.client.extended.event.legacy.EventRecorder)1 LegacyEventBroadcaster (io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster)1 LeaderElectionConfig (io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)1 LeaderElector (io.kubernetes.client.extended.leaderelection.LeaderElector)1 EndpointsLock (io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock)1 SharedIndexInformer (io.kubernetes.client.informer.SharedIndexInformer)1 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)1 Lister (io.kubernetes.client.informer.cache.Lister)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1EventSource (io.kubernetes.client.openapi.models.V1EventSource)1