use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.
the class PortForwardService method getNewestPod.
private Pod getNewestPod(LabelSelector selector) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = KubernetesClientUtil.withSelector(kubernetes.pods(), selector, log);
PodList list = pods.list();
if (list != null) {
List<Pod> items = list.getItems();
return getNewestPod(items);
}
return null;
}
use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method waitForOpenShiftBuildToComplete.
private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws MojoExecutionException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch logTerminateLatch = new CountDownLatch(1);
final String buildName = KubernetesHelper.getName(build);
final AtomicReference<Build> buildHolder = new AtomicReference<>();
// Don't query for logs directly, Watch over the build pod:
waitUntilPodIsReady(buildName + "-build", 20, log);
log.info("Waiting for build " + buildName + " to complete...");
try (LogWatch logWatch = client.pods().withName(buildName + "-build").watchLog()) {
KubernetesClientUtil.printLogsAsync(logWatch, "Failed to tail build log", logTerminateLatch, log);
Watcher<Build> buildWatcher = getBuildWatcher(latch, buildName, buildHolder);
try (Watch watcher = client.builds().withName(buildName).watch(buildWatcher)) {
// Check if the build is already finished to avoid waiting indefinitely
Build lastBuild = client.builds().withName(buildName).get();
String lastStatus = KubernetesResourceUtil.getBuildStatusPhase(lastBuild);
if (Builds.isFinished(lastStatus)) {
log.debug("Build %s is already finished", buildName);
buildHolder.set(lastBuild);
latch.countDown();
}
waitUntilBuildFinished(latch);
logTerminateLatch.countDown();
build = buildHolder.get();
String status = KubernetesResourceUtil.getBuildStatusPhase(build);
if (Builds.isFailed(status) || Builds.isCancelled(status)) {
throw new MojoExecutionException("OpenShift Build " + buildName + ": " + KubernetesResourceUtil.getBuildStatusReason(build));
}
log.info("Build " + buildName + " " + status);
}
}
}
use of io.fabric8.kubernetes.client.Watch 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.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.
the class WatchMojo method customizeConfig.
@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
try {
Fabric8ServiceHub serviceHub = getFabric8ServiceHub();
GeneratorContext ctx = new GeneratorContext.Builder().config(extractGeneratorConfig()).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:watch").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(useProjectClasspath).artifactResolver(serviceHub.getArtifactResolverService()).build();
return GeneratorManager.generate(configs, ctx, false);
} catch (MojoExecutionException e) {
throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
}
}
use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.
the class SpringBootWatcher method watch.
@Override
public void watch(List<ImageConfiguration> configs, Set<HasMetadata> resources, PlatformMode mode) throws Exception {
KubernetesClient kubernetes = getContext().getKubernetesClient();
PodLogService.PodLogServiceContext logContext = new PodLogService.PodLogServiceContext.Builder().log(log).newPodLog(getContext().getNewPodLogger()).oldPodLog(getContext().getOldPodLogger()).build();
new PodLogService(logContext).tailAppPodsLogs(kubernetes, getContext().getNamespace(), resources, false, null, true, null, false);
String url = getServiceExposeUrl(kubernetes, resources);
if (url == null) {
url = getPortForwardUrl(resources);
}
if (url != null) {
runRemoteSpringApplication(url);
} else {
throw new IllegalStateException("Unable to open a channel to the remote pod.");
}
}
Aggregations