use of io.fabric8.maven.watcher.api.Watcher in project fabric8-maven-plugin by fabric8io.
the class PodLogService method watchLogOfPodName.
private void watchLogOfPodName(KubernetesClient kubernetes, String namespace, String ctrlCMessage, boolean followLog, Pod pod, String name) {
if (watchingPodName == null || !watchingPodName.equals(name)) {
if (logWatcher != null) {
log.info("Closing log watcher for %s as now watching %s", watchingPodName, name);
closeLogWatcher();
}
PodResource<Pod, DoneablePod> podResource = kubernetes.pods().inNamespace(namespace).withName(name);
List<Container> containers = KubernetesHelper.getContainers(pod);
String containerName = null;
if (followLog) {
watchingPodName = name;
logWatchTerminateLatch = new CountDownLatch(1);
if (containers.size() < 2) {
logWatcher = podResource.watchLog();
} else {
containerName = getLogContainerName(containers);
logWatcher = podResource.inContainer(containerName).watchLog();
}
watchLog(logWatcher, name, "Failed to read log of pod " + name + ".", ctrlCMessage, containerName);
} else {
String logText;
if (containers.size() < 2) {
logText = podResource.getLog();
} else {
containerName = getLogContainerName(containers);
logText = podResource.inContainer(containerName).getLog();
}
if (logText != null) {
String[] lines = logText.split("\n");
log.info("Log of pod: %s%s", name, containerNameMessage(containerName));
log.info("");
for (String line : lines) {
log.info("[[s]]%s", line);
}
}
terminateLatch.countDown();
}
}
}
use of io.fabric8.maven.watcher.api.Watcher in project fabric8-maven-plugin by fabric8io.
the class PodLogService method waitAndLogPods.
private void waitAndLogPods(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final boolean watchAddedPodsOnly, final String ctrlCMessage, final boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
if (context.getPodName() != null) {
log.info("Watching pod with selector %s, and name %s waiting for a running pod...", selector, context.getPodName());
pods = pods.withField("metadata.name", context.getPodName());
} else {
log.info("Watching pods with selector %s waiting for a running pod...", selector);
}
Pod latestPod = null;
boolean runningPod = false;
PodList list = pods.list();
if (list != null) {
List<Pod> items = list.getItems();
if (items != null) {
for (Pod pod : items) {
PodStatusType status = getPodStatus(pod);
switch(status) {
case WAIT:
case OK:
if (latestPod == null || KubernetesResourceUtil.isNewerResource(pod, latestPod)) {
if (ignorePodsOlderThan != null) {
Date podCreateTime = KubernetesResourceUtil.getCreationTimestamp(pod);
if (podCreateTime != null && podCreateTime.compareTo(ignorePodsOlderThan) > 0) {
latestPod = pod;
}
} else {
latestPod = pod;
}
}
runningPod = true;
break;
case ERROR:
default:
continue;
}
}
}
}
// we may have missed the ADDED event so lets simulate one
if (latestPod != null) {
onPod(Watcher.Action.ADDED, latestPod, kubernetes, namespace, ctrlCMessage, followLog);
}
if (!watchAddedPodsOnly) {
// lets watch the current pods then watch for changes
if (!runningPod) {
log.warn("No pod is running yet. Are you sure you deployed your app via `fabric8:deploy`?");
log.warn("Or did you stop it via `fabric8:stop`? If so try running the `fabric8:start` goal");
}
}
podWatcher = pods.watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod pod) {
onPod(action, pod, kubernetes, namespace, ctrlCMessage, followLog);
}
@Override
public void onClose(KubernetesClientException e) {
// ignore
}
});
if (waitInCurrentThread) {
while (terminateLatch.getCount() > 0) {
try {
terminateLatch.await();
} catch (InterruptedException e) {
// ignore
}
}
}
}
use of io.fabric8.maven.watcher.api.Watcher in project fabric8-maven-plugin by fabric8io.
the class DockerImageWatcher method restartContainer.
protected void restartContainer(WatchService.ImageWatcher watcher, Set<HasMetadata> resources) throws MojoExecutionException {
ImageConfiguration imageConfig = watcher.getImageConfiguration();
String imageName = imageConfig.getName();
try {
ClusterAccess clusterAccess = new ClusterAccess(getContext().getNamespace());
KubernetesClient client = clusterAccess.createDefaultClient(log);
String namespace = clusterAccess.getNamespace();
String imagePrefix = getImagePrefix(imageName);
for (HasMetadata entity : resources) {
updateImageName(client, namespace, entity, imagePrefix, imageName);
}
} catch (KubernetesClientException e) {
KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
} catch (MojoExecutionException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
use of io.fabric8.maven.watcher.api.Watcher in project fabric8 by fabric8io.
the class WatchBuilds method main.
public static void main(String... args) {
String namespace = null;
if (args.length > 0) {
namespace = args[0];
}
String consoleLink = Links.getFabric8ConsoleLink();
OpenShiftClient client = new DefaultOpenShiftClient();
BuildListener buildListener = new BuildListener() {
@Override
public void onBuildFinished(BuildFinishedEvent event) {
System.out.println("Build: " + event.getUid() + " for config: " + event.getConfigName() + " finished. Status: " + event.getStatus() + " link: " + event.getBuildLink());
}
};
BuildWatcher watcher = new BuildWatcher(client, buildListener, namespace, consoleLink);
long pollTime = 3000;
watcher.schedule(pollTime);
watcher.join();
}
Aggregations