use of io.fabric8.kubernetes.client.Watcher in project docker-maven-plugin by fabric8io.
the class WatchService method defaultContainerRestartTask.
private Task<ImageWatcher> defaultContainerRestartTask() {
return new Task<ImageWatcher>() {
@Override
public void execute(ImageWatcher watcher) throws Exception {
// Stop old one
ImageConfiguration imageConfig = watcher.getImageConfiguration();
PortMapping mappedPorts = runService.createPortMapping(imageConfig.getRunConfiguration(), watcher.getWatchContext().getMojoParameters().getProject().getProperties());
String id = watcher.getContainerId();
String optionalPreStop = getPreStopCommand(imageConfig);
if (optionalPreStop != null) {
runService.execInContainer(id, optionalPreStop, watcher.getImageConfiguration());
}
runService.stopPreviouslyStartedContainer(id, false, false);
// Start new one
watcher.setContainerId(runService.createAndStartContainer(imageConfig, mappedPorts, watcher.getWatchContext().getPomLabel(), watcher.getWatchContext().getMojoParameters().getProject().getProperties(), watcher.getWatchContext().getMojoParameters().getProject().getBasedir()));
}
};
}
use of io.fabric8.kubernetes.client.Watcher in project docker-maven-plugin by fabric8io.
the class WatchService method createBuildWatchTask.
private Runnable createBuildWatchTask(final ImageWatcher watcher, final MojoParameters mojoParameters, final boolean doRestart, final BuildService.BuildContext buildContext) throws MojoExecutionException {
final ImageConfiguration imageConfig = watcher.getImageConfiguration();
final AssemblyFiles files = archiveService.getAssemblyFiles(imageConfig, mojoParameters);
if (files.isEmpty()) {
log.error("No assembly files for %s. Are you sure you invoked together with the `package` goal?", imageConfig.getDescription());
throw new MojoExecutionException("No files to watch found for " + imageConfig);
}
return new Runnable() {
@Override
public void run() {
List<AssemblyFiles.Entry> entries = files.getUpdatedEntriesAndRefresh();
if (entries != null && entries.size() > 0) {
try {
log.info("%s: Assembly changed. Rebuild ...", imageConfig.getDescription());
if (watcher.getWatchContext().getImageCustomizer() != null) {
log.info("%s: Customizing the image ...", imageConfig.getDescription());
watcher.getWatchContext().getImageCustomizer().execute(imageConfig);
}
buildService.buildImage(imageConfig, null, buildContext);
String name = imageConfig.getName();
watcher.setImageId(queryService.getImageId(name));
if (doRestart) {
restartContainer(watcher);
}
callPostGoal(watcher);
} catch (Exception e) {
log.error("%s: Error when rebuilding - %s", imageConfig.getDescription(), e);
}
}
}
};
}
use of io.fabric8.kubernetes.client.Watcher in project fabric8-maven-plugin by fabric8io.
the class KubernetesClientUtil method withSelector.
public static FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> withSelector(NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods, LabelSelector selector, Logger log) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> answer = pods;
Map<String, String> matchLabels = selector.getMatchLabels();
if (matchLabels != null && !matchLabels.isEmpty()) {
answer = answer.withLabels(matchLabels);
}
List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
if (matchExpressions != null) {
for (LabelSelectorRequirement expression : matchExpressions) {
String key = expression.getKey();
List<String> values = expression.getValues();
if (Strings.isNullOrBlank(key)) {
log.warn("Ignoring empty key in selector expression %s", expression);
continue;
}
if (values == null || values.isEmpty()) {
log.warn("Ignoring empty values in selector expression %s", expression);
continue;
}
String[] valuesArray = values.toArray(new String[values.size()]);
String operator = expression.getOperator();
switch(operator) {
case "In":
answer = answer.withLabelIn(key, valuesArray);
break;
case "NotIn":
answer = answer.withLabelNotIn(key, valuesArray);
break;
default:
log.warn("Ignoring unknown operator %s in selector expression %s", operator, expression);
}
}
}
return answer;
}
use of io.fabric8.kubernetes.client.Watcher 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.Watcher 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);
}
}
}
Aggregations