io.kubernetes.client.informer.SharedInformerFactory Java Examples

The following examples show how to use io.kubernetes.client.informer.SharedInformerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultKubeApiFacade.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private SharedIndexInformer<V1Pod> createPodInformer(SharedInformerFactory sharedInformerFactory) {
    return sharedInformerFactory.sharedIndexInformerFor(
            (CallGeneratorParams params) -> coreV1Api.listNamespacedPodCall(
                    KUBERNETES_NAMESPACE,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    params.resourceVersion,
                    params.timeoutSeconds,
                    params.watch,
                    null
            ),
            V1Pod.class,
            V1PodList.class,
            configuration.getKubeApiServerIntegratorRefreshIntervalMs()
    );
}
 
Example #2
Source File: DefaultKubeApiFacade.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private SharedIndexInformer<V1Node> createNodeInformer(SharedInformerFactory sharedInformerFactory) {
    return sharedInformerFactory.sharedIndexInformerFor(
            (CallGeneratorParams params) -> coreV1Api.listNodeCall(
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    params.resourceVersion,
                    params.timeoutSeconds,
                    params.watch,
                    null
            ),
            V1Node.class,
            V1NodeList.class,
            configuration.getKubeApiServerIntegratorRefreshIntervalMs()
    );
}
 
Example #3
Source File: KubernetesReconcilerProcessor.java    From java with Apache License 2.0 5 votes vote down vote up
private Controller buildController(SharedInformerFactory sharedInformerFactory, Reconciler r)
    throws BeansException {
  KubernetesReconciler kubernetesReconciler =
      r.getClass().getAnnotation(KubernetesReconciler.class);
  String reconcilerName = kubernetesReconciler.value();

  KubernetesReconcilerWatches watches = kubernetesReconciler.watches();
  DefaultControllerBuilder builder = ControllerBuilder.defaultBuilder(sharedInformerFactory);
  RateLimitingQueue<Request> workQueue = new DefaultRateLimitingQueue<>();
  builder = builder.withWorkQueue(workQueue);
  Map<Class, AddFilterAdaptor> addFilters = getAddFilters(watches, r);
  Map<Class, UpdateFilterAdaptor> updateFilters = getUpdateFilters(watches, r);
  Map<Class, DeleteFilterAdaptor> deleteFilters = getDeleteFilters(watches, r);
  List<ReadyFuncAdaptor> readyFuncs = getReadyFuncs(r);
  for (KubernetesReconcilerWatch watch : watches.value()) {
    try {
      Function<?, Request> workQueueKeyFunc = watch.workQueueKeyFunc().newInstance();
      builder =
          builder.watch(
              (q) -> {
                return ControllerBuilder.controllerWatchBuilder(watch.apiTypeClass(), q)
                    .withOnAddFilter(addFilters.get(watch.apiTypeClass()))
                    .withOnUpdateFilter(updateFilters.get(watch.apiTypeClass()))
                    .withOnDeleteFilter(deleteFilters.get(watch.apiTypeClass()))
                    .withWorkQueueKeyFunc(workQueueKeyFunc)
                    .withResyncPeriod(Duration.ofMillis(watch.resyncPeriodMillis()))
                    .build();
              });
      for (Supplier<Boolean> readyFunc : readyFuncs) {
        builder = builder.withReadyFunc(readyFunc);
      }
    } catch (IllegalAccessException | InstantiationException e) {
      throw new BeanCreationException("Failed instantiating controller: " + e.getMessage());
    }
  }

  builder = builder.withWorkerCount(kubernetesReconciler.workerCount());

  return builder.withReconciler(r).withName(reconcilerName).build();
}
 
Example #4
Source File: DefaultKubeApiFacade.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private SharedIndexInformer<V1OpportunisticResource> createOpportunisticResourceInformer(SharedInformerFactory sharedInformerFactory) {
    return sharedInformerFactory.sharedIndexInformerFor(
            this::listOpportunisticResourcesCall,
            V1OpportunisticResource.class,
            V1OpportunisticResourceList.class,
            configuration.getKubeOpportunisticRefreshIntervalMs()
    );
}
 
Example #5
Source File: ControllerManagerTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerStartShutdown() {
  DummyController dummy1 = new DummyController();
  DummyController dummy2 = new DummyController();

  ControllerManager cm = new ControllerManager(new SharedInformerFactory(), dummy1, dummy2);

  cm.run();
  assertTrue(dummy1.started);
  assertTrue(dummy2.started);

  cm.shutdown();
  assertTrue(dummy1.stopped);
  assertTrue(dummy2.stopped);
}
 
Example #6
Source File: SpringControllerExample.java    From java with Apache License 2.0 5 votes vote down vote up
@Bean
public CommandLineRunner commandLineRunner(
    SharedInformerFactory sharedInformerFactory,
    @Qualifier("node-printing-controller") Controller nodePrintingController) {
  return args -> {
    System.out.println("starting informers..");
    sharedInformerFactory.startAllRegisteredInformers();

    System.out.println("running controller..");
    nodePrintingController.run();
  };
}
 
Example #7
Source File: KubernetesReconcilerProcessor.java    From java with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
    throws BeansException {
  SharedInformerFactory sharedInformerFactory = beanFactory.getBean(SharedInformerFactory.class);
  String[] names = beanFactory.getBeanNamesForType(Reconciler.class);
  for (String name : names) {
    Reconciler reconciler = (Reconciler) beanFactory.getBean(name);
    KubernetesReconciler kubernetesReconciler =
        reconciler.getClass().getAnnotation(KubernetesReconciler.class);
    String reconcilerName = kubernetesReconciler.value();
    Controller controller = buildController(sharedInformerFactory, reconciler);
    beanFactory.registerSingleton(reconcilerName, controller);
  }
}
 
Example #8
Source File: SpringControllerExample.java    From java with Apache License 2.0 4 votes vote down vote up
@Bean("sharedInformerFactory")
public SharedInformerFactory sharedInformerFactory() {
  return new MySharedInformerFactory();
}
 
Example #9
Source File: DefaultSharedIndexInformerTest.java    From java with Apache License 2.0 4 votes vote down vote up
@Test
public void testInformerReListWatchOnWatchConflict() throws InterruptedException {

  CoreV1Api coreV1Api = new CoreV1Api(client);

  String startRV = "1000";
  V1PodList podList =
      new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());

  stubFor(
      get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
          .withQueryParam("watch", equalTo("false"))
          .willReturn(
              aResponse()
                  .withStatus(200)
                  .withHeader("Content-Type", "application/json")
                  .withBody(new JSON().serialize(podList))));

  Watch.Response<V1Pod> watchResponse =
      new Watch.Response<>(
          EventType.ERROR.name(), new V1Status().apiVersion("v1").kind("Status").code(409));
  stubFor(
      get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
          .withQueryParam("watch", equalTo("true"))
          .withQueryParam("resourceVersion", equalTo(startRV))
          .willReturn(
              aResponse()
                  .withStatus(200)
                  .withHeader("Content-Type", "application/json")
                  .withBody(new JSON().serialize(watchResponse))));

  SharedInformerFactory factory = new SharedInformerFactory();
  SharedIndexInformer<V1Pod> podInformer =
      factory.sharedIndexInformerFor(
          (CallGeneratorParams params) -> {
            try {
              return coreV1Api.listNamespacedPodCall(
                  namespace,
                  null,
                  null,
                  null,
                  null,
                  null,
                  null,
                  params.resourceVersion,
                  params.timeoutSeconds,
                  params.watch,
                  null);
            } catch (ApiException e) {
              throw new RuntimeException(e);
            }
          },
          V1Pod.class,
          V1PodList.class);

  factory.startAllRegisteredInformers();

  // Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
  Thread.sleep(3000);

  verify(
      moreThan(1),
      getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
          .withQueryParam("watch", equalTo("false")));
  verify(
      moreThan(1),
      getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
          .withQueryParam("watch", equalTo("true")));
  factory.stopAllRegisteredInformers();
}
 
Example #10
Source File: DefaultSharedIndexInformerTest.java    From java with Apache License 2.0 4 votes vote down vote up
@Test
public void testInformerReListingOnListForbidden() throws InterruptedException {

  CoreV1Api coreV1Api = new CoreV1Api(client);

  stubFor(
      get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
          .withQueryParam("watch", equalTo("false"))
          .willReturn(
              aResponse()
                  .withStatus(403)
                  .withHeader("Content-Type", "application/json")
                  .withBody(
                      new JSON()
                          .serialize(
                              new V1Status()
                                  .apiVersion("v1")
                                  .kind("Status")
                                  .code(403)
                                  .reason("RBAC forbidden")))));

  SharedInformerFactory factory = new SharedInformerFactory();
  SharedIndexInformer<V1Pod> podInformer =
      factory.sharedIndexInformerFor(
          (CallGeneratorParams params) -> {
            try {
              return coreV1Api.listNamespacedPodCall(
                  namespace,
                  null,
                  null,
                  null,
                  null,
                  null,
                  null,
                  params.resourceVersion,
                  params.timeoutSeconds,
                  params.watch,
                  null);
            } catch (ApiException e) {
              throw new RuntimeException(e);
            }
          },
          V1Pod.class,
          V1PodList.class);

  factory.startAllRegisteredInformers();

  // Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
  Thread.sleep(3000);

  verify(
      moreThan(1),
      getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
          .withQueryParam("watch", equalTo("false")));
  factory.stopAllRegisteredInformers();
}
 
Example #11
Source File: ControllerManagerBuilder.java    From java with Apache License 2.0 4 votes vote down vote up
/** Instantiates a new Controller mananger builder. */
ControllerManagerBuilder(SharedInformerFactory factory) {
  this.informerFactory = factory;
  this.controllerList = new ArrayList<>();
}
 
Example #12
Source File: KubeApiClients.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public static SharedInformerFactory createSharedInformerFactory(String threadNamePrefix, ApiClient apiClient, TitusRuntime titusRuntime) {
    ExecutorService threadPool = ExecutorsExt.instrumentedCachedThreadPool(titusRuntime.getRegistry(), threadNamePrefix);
    return new SharedInformerFactory(apiClient, threadPool);
}
 
Example #13
Source File: ControllerExample.java    From java with Apache License 2.0 4 votes vote down vote up
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,
                  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())) // optional, default to
                        .withOnAddFilter(
                            (V1Node createdNode) ->
                                createdNode
                                    .getMetadata()
                                    .getName()
                                    .startsWith("docker-")) // optional, set onAdd filter
                        .withOnUpdateFilter(
                            (V1Node oldNode, V1Node newNode) ->
                                newNode
                                    .getMetadata()
                                    .getName()
                                    .startsWith("docker-")) // optional, set onUpdate filter
                        .withOnDeleteFilter(
                            (V1Node deletedNode, Boolean stateUnknown) ->
                                deletedNode
                                    .getMetadata()
                                    .getName()
                                    .startsWith("docker-")) // optional, set onDelete filter
                        .build())
            .withReconciler(nodeReconciler) // required, set the actual reconciler
            .withName("node-printing-controller") // optional, set name for controller
            .withWorkerCount(4) // optional, set worker thread count
            .withReadyFunc(
                nodeInformer
                    ::hasSynced) // optional, only starts controller when the cache has synced up
            .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();
  }
 
Example #14
Source File: KubernetesInformerCreatorTest.java    From java with Apache License 2.0 4 votes vote down vote up
@Bean
public SharedInformerFactory sharedInformerFactory() {
  return new TestSharedInformerFactory();
}
 
Example #15
Source File: ControllerManager.java    From java with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new Controller manager.
 *
 * @param controllers the controllers to be managed.
 */
public ControllerManager(SharedInformerFactory factory, Controller... controllers) {
  this.controllers = controllers;
  this.informerFactory = factory;
}
 
Example #16
Source File: DefaultControllerBuilder.java    From java with Apache License 2.0 2 votes vote down vote up
/**
 * Starts building controller by given informer factory.
 *
 * @param informerFactory the informer factory
 * @return the controller builder
 */
DefaultControllerBuilder(SharedInformerFactory informerFactory) {
  this();
  this.informerFactory = informerFactory;
}
 
Example #17
Source File: ControllerBuilder.java    From java with Apache License 2.0 2 votes vote down vote up
/**
 * Default builder is for building default controller.
 *
 * @param factory the informer factory, note that there supposed to be one informer factory
 *     globally in your application.
 * @return the default controller builder
 */
public static DefaultControllerBuilder defaultBuilder(SharedInformerFactory factory) {
  return new DefaultControllerBuilder(factory);
}
 
Example #18
Source File: ControllerBuilder.java    From java with Apache License 2.0 2 votes vote down vote up
/**
 * Controller manager builder is for building controller-manager .
 *
 * @param factory the informer factory
 * @return the controller mananger builder
 */
public static ControllerManagerBuilder controllerManagerBuilder(SharedInformerFactory factory) {
  return new ControllerManagerBuilder(factory);
}