Search in sources :

Example 1 with LogWatch

use of io.fabric8.kubernetes.client.dsl.LogWatch 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 LogWatch

use of io.fabric8.kubernetes.client.dsl.LogWatch in project camel by apache.

the class KubernetesBuildsProducer method doListBuildByLabels.

protected void doListBuildByLabels(Exchange exchange, String operation) throws Exception {
    BuildList buildList = null;
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_BUILDS_LABELS, Map.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    if (!ObjectHelper.isEmpty(namespaceName)) {
        NonNamespaceOperation<Build, BuildList, DoneableBuild, BuildResource<Build, DoneableBuild, String, LogWatch>> builds = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).builds().inNamespace(namespaceName);
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            builds.withLabel(entry.getKey(), entry.getValue());
        }
        buildList = builds.list();
    } else {
        MixedOperation<Build, BuildList, DoneableBuild, BuildResource<Build, DoneableBuild, String, LogWatch>> builds = getEndpoint().getKubernetesClient().adapt(OpenShiftClient.class).builds();
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            builds.withLabel(entry.getKey(), entry.getValue());
        }
        buildList = builds.list();
    }
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(buildList.getItems());
}
Also used : BuildList(io.fabric8.openshift.api.model.BuildList) DoneableBuild(io.fabric8.openshift.api.model.DoneableBuild) DoneableBuild(io.fabric8.openshift.api.model.DoneableBuild) Build(io.fabric8.openshift.api.model.Build) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Map(java.util.Map) BuildResource(io.fabric8.openshift.client.dsl.BuildResource)

Aggregations

Build (io.fabric8.openshift.api.model.Build)2 Watch (io.fabric8.kubernetes.client.Watch)1 LogWatch (io.fabric8.kubernetes.client.dsl.LogWatch)1 BuildList (io.fabric8.openshift.api.model.BuildList)1 DoneableBuild (io.fabric8.openshift.api.model.DoneableBuild)1 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)1 BuildResource (io.fabric8.openshift.client.dsl.BuildResource)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1