Search in sources :

Example 1 with BuildWatcher

use of io.fabric8.kubernetes.api.builds.BuildWatcher 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);
        }
    }
}
Also used : LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Build(io.fabric8.openshift.api.model.Build) Watch(io.fabric8.kubernetes.client.Watch) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with BuildWatcher

use of io.fabric8.kubernetes.api.builds.BuildWatcher 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();
}
Also used : BuildListener(io.fabric8.kubernetes.api.builds.BuildListener) BuildWatcher(io.fabric8.kubernetes.api.builds.BuildWatcher) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildFinishedEvent(io.fabric8.kubernetes.api.builds.BuildFinishedEvent) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient)

Aggregations

BuildFinishedEvent (io.fabric8.kubernetes.api.builds.BuildFinishedEvent)1 BuildListener (io.fabric8.kubernetes.api.builds.BuildListener)1 BuildWatcher (io.fabric8.kubernetes.api.builds.BuildWatcher)1 Watch (io.fabric8.kubernetes.client.Watch)1 LogWatch (io.fabric8.kubernetes.client.dsl.LogWatch)1 Build (io.fabric8.openshift.api.model.Build)1 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)1 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1