use of io.fabric8.maven.watcher.api.Watcher in project fabric8-maven-plugin by fabric8io.
the class WatcherManager method watch.
public static void watch(List<ImageConfiguration> ret, Set<HasMetadata> resources, WatcherContext watcherCtx) throws Exception {
PluginServiceFactory<WatcherContext> pluginFactory = watcherCtx.isUseProjectClasspath() ? new PluginServiceFactory<>(watcherCtx, ClassUtil.createProjectClassLoader(watcherCtx.getProject(), watcherCtx.getLogger())) : new PluginServiceFactory<>(watcherCtx);
boolean isOpenshift = KubernetesHelper.isOpenShift(watcherCtx.getKubernetesClient());
PlatformMode mode = isOpenshift ? PlatformMode.openshift : PlatformMode.kubernetes;
List<Watcher> watchers = pluginFactory.createServiceObjects("META-INF/fabric8/watcher-default", "META-INF/fabric8/fabric8-watcher-default", "META-INF/fabric8/watcher", "META-INF/fabric8-watcher");
ProcessorConfig config = watcherCtx.getConfig();
Logger log = watcherCtx.getLogger();
List<Watcher> usableWatchers = config.prepareProcessors(watchers, "watcher");
log.verbose("Watchers:");
Watcher chosen = null;
for (Watcher watcher : usableWatchers) {
if (watcher.isApplicable(ret, resources, mode)) {
if (chosen == null) {
log.verbose(" - %s [selected]", watcher.getName());
chosen = watcher;
} else {
log.verbose(" - %s", watcher.getName());
}
} else {
log.verbose(" - %s [not applicable]", watcher.getName());
}
}
if (chosen == null) {
throw new IllegalStateException("No watchers can be used for the current project");
}
log.info("Running watcher %s", chosen.getName());
chosen.watch(ret, resources, mode);
}
use of io.fabric8.maven.watcher.api.Watcher in project strimzi by strimzi.
the class StatefulSetOperator method restartPod.
private Future<Void> restartPod(String namespace, String name, Predicate<String> isReady, String podName) {
Future<Void> result = Future.future();
log.info("Roll {}/{}: Rolling pod {}", namespace, name, podName);
Future<Void> deleted = Future.future();
Future<CompositeFuture> deleteFinished = Future.future();
Watcher<Pod> watcher = new RollingUpdateWatcher(deleted);
Watch watch = podOperations.watch(namespace, podName, watcher);
// Delete the pod
log.debug("Roll {}/{}: Waiting for pod {} to be deleted", namespace, name, podName);
Future podReconcileFuture = podOperations.reconcile(namespace, podName, null);
CompositeFuture.join(podReconcileFuture, deleted).setHandler(deleteResult -> {
watch.close();
if (deleteResult.succeeded()) {
log.debug("Roll {}/{}: Pod {} was deleted", namespace, name, podName);
}
deleteFinished.handle(deleteResult);
});
deleteFinished.compose(ix -> {
log.debug("Roll {}/{}: Waiting for new pod {} to get ready", namespace, name, podName);
Future<Void> readyFuture = Future.future();
vertx.setPeriodic(1_000, timerId -> {
p(isReady, podName).setHandler(x -> {
if (x.succeeded()) {
if (x.result()) {
vertx.cancelTimer(timerId);
readyFuture.complete();
}
// else not ready
} else {
vertx.cancelTimer(timerId);
readyFuture.fail(x.cause());
}
});
});
return readyFuture;
}).setHandler(result);
return result;
}
use of io.fabric8.maven.watcher.api.Watcher in project fabric8 by fabric8io.
the class WatchBuildsExample method main.
public static void main(String... args) throws Exception {
OpenShiftClient client = new DefaultOpenShiftClient();
client.builds().watch(new Watcher<Build>() {
@Override
public void eventReceived(Action action, Build build) {
System.out.println(action + ": " + build);
}
@Override
public void onClose(KubernetesClientException e) {
System.out.println("Closed: " + e);
}
});
client.close();
}
use of io.fabric8.maven.watcher.api.Watcher in project fabric8 by fabric8io.
the class KubernetesConfigAdminBridge method watchConfigMapList.
private void watchConfigMapList() {
if (configWatch) {
KubernetesClient client = kubernetesClient.get();
if (client != null) {
FilterWatchListDeletable<ConfigMap, ConfigMapList, Boolean, Watch, Watcher<ConfigMap>> configMapsSelector = client.configMaps().withLabel(pidLabel);
for (String key : filters.keySet()) {
configMapsSelector.withLabelIn(key, filters.get(key).toArray(new String[filters.get(key).size()]));
}
watch = configMapsSelector.watch(this);
} else {
throw new RuntimeException("KubernetesClient not set");
}
}
}
use of io.fabric8.maven.watcher.api.Watcher in project strimzi by strimzi.
the class ClusterControllerTest method startStop.
/**
* Does the CC start and then stop a verticle per namespace?
* @param context
* @param namespaces
*/
private void startStop(TestContext context, String namespaces) {
AtomicInteger numWatchers = new AtomicInteger(0);
KubernetesClient client = mock(KubernetesClient.class);
MixedOperation mockCms = mock(MixedOperation.class);
when(client.configMaps()).thenReturn(mockCms);
List<String> namespaceList = asList(namespaces.split(" *,+ *"));
for (String namespace : namespaceList) {
MixedOperation mockNamespacedCms = mock(MixedOperation.class);
when(mockNamespacedCms.watch(any())).thenAnswer(invo -> {
numWatchers.incrementAndGet();
Watch mockWatch = mock(Watch.class);
doAnswer(invo2 -> {
((Watcher) invo.getArgument(0)).onClose(null);
return null;
}).when(mockWatch).close();
return mockWatch;
});
when(mockNamespacedCms.withLabels(any())).thenReturn(mockNamespacedCms);
when(mockCms.inNamespace(namespace)).thenReturn(mockNamespacedCms);
}
Async async = context.async();
Map<String, String> env = new HashMap<>();
env.put(ClusterControllerConfig.STRIMZI_NAMESPACE, namespaces);
env.put(ClusterControllerConfig.STRIMZI_CONFIGMAP_LABELS, STRIMZI_IO_KIND_CLUSTER);
env.put(ClusterControllerConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS, "120000");
Main.run(vertx, client, true, env).setHandler(ar -> {
context.assertNull(ar.cause(), "Expected all verticles to start OK");
async.complete();
});
async.await();
context.assertEquals(namespaceList.size(), vertx.deploymentIDs().size(), "A verticle per namespace");
List<Async> asyncs = new ArrayList<>();
for (String deploymentId : vertx.deploymentIDs()) {
Async async2 = context.async();
asyncs.add(async2);
vertx.undeploy(deploymentId, ar -> {
context.assertNull(ar.cause(), "Didn't expect error when undeploying verticle " + deploymentId);
async2.complete();
});
}
for (Async async2 : asyncs) {
async2.await();
}
if (numWatchers.get() > namespaceList.size()) {
context.fail("Looks like there were more watchers than namespaces");
}
}
Aggregations