use of io.kubernetes.client.extended.controller.reconciler.Request in project java by kubernetes-client.
the class KubernetesControllerFactory method buildController.
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<? extends KubernetesObject, Request> workQueueKeyFunc;
try {
workQueueKeyFunc = watch.workQueueKeyFunc().getConstructor(WorkQueue.class).newInstance(workQueue);
} catch (NoSuchMethodException e) {
workQueueKeyFunc = watch.workQueueKeyFunc().newInstance();
} catch (InvocationTargetException e) {
throw new BeanCreationException("Failed instantiating controller watch: " + e.getMessage());
}
final Function<? extends KubernetesObject, Request> finalWorkQueueKeyFunc = workQueueKeyFunc;
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(finalWorkQueueKeyFunc).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();
}
use of io.kubernetes.client.extended.controller.reconciler.Request in project java by kubernetes-client.
the class DefaultControllerTest method testControllerWontStartBeforeReady.
@Test(timeout = 90000)
public void testControllerWontStartBeforeReady() throws InterruptedException {
Request request1 = new Request("test1");
final Semaphore latch = new Semaphore(1);
when(mockReconciler.reconcile(request1)).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
latch.release();
return new Result(false);
}
});
latch.acquire();
AtomicBoolean ready = new AtomicBoolean(false);
DefaultController testController = new DefaultController("", mockReconciler, workQueue, () -> ready.get());
testController.setWorkerCount(1);
testController.setWorkerThreadPool(Executors.newScheduledThreadPool(1));
testController.setReadyCheckInternal(Duration.ofMillis(100));
controllerThead.submit(testController::run);
// emit an event when the controller hasn't been ready
workQueue.add(request1);
// As above wrt sleep
cooldown();
verify(mockReconciler, times(0)).reconcile(request1);
ready.set(true);
latch.acquire();
verify(mockReconciler, times(1)).reconcile(request1);
}
use of io.kubernetes.client.extended.controller.reconciler.Request in project java by kubernetes-client.
the class DefaultControllerBuilderTest method testBuildWatchEventNotificationShouldWork.
@Test
public void testBuildWatchEventNotificationShouldWork() throws InterruptedException {
V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().name("test-pod1")).spec(new V1PodSpec().hostname("hostname1"))));
stubFor(get(urlPathEqualTo("/api/v1/pods")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
CoreV1Api api = new CoreV1Api(client);
SharedIndexInformer<V1Pod> podInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
return api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
}, V1Pod.class, V1PodList.class);
List<Request> keyFuncReceivingRequests = new ArrayList<>();
Function<V1Pod, Request> podKeyFunc = (V1Pod pod) -> {
// twisting pod name key
Request request = new Request(pod.getSpec().getHostname() + "/" + pod.getMetadata().getName());
keyFuncReceivingRequests.add(request);
return request;
};
List<Request> controllerReceivingRequests = new ArrayList<>();
final Semaphore latch = new Semaphore(1);
latch.acquire();
final Controller testController = ControllerBuilder.defaultBuilder(informerFactory).withReconciler(new Reconciler() {
@Override
public Result reconcile(Request request) {
controllerReceivingRequests.add(request);
latch.release();
return new Result(false);
}
}).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Pod.class, workQueue).withWorkQueueKeyFunc(podKeyFunc).build()).build();
controllerThead.submit(testController::run);
informerFactory.startAllRegisteredInformers();
// Wait for the request to be processed.
latch.acquire(1);
Request expectedRequest = new Request("hostname1/test-pod1");
assertEquals(1, keyFuncReceivingRequests.size());
assertEquals(expectedRequest, keyFuncReceivingRequests.get(0));
assertEquals(1, controllerReceivingRequests.size());
assertEquals(expectedRequest, controllerReceivingRequests.get(0));
}
use of io.kubernetes.client.extended.controller.reconciler.Request 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();
}
Aggregations