Search in sources :

Example 21 with Watcher

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();
        }
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with Watcher

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
            }
        }
    }
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) Watcher(io.fabric8.kubernetes.client.Watcher) Date(java.util.Date) Watch(io.fabric8.kubernetes.client.Watch) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) PodStatusType(io.fabric8.kubernetes.api.PodStatusType) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 23 with Watcher

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);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 24 with Watcher

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();
}
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

Watch (io.fabric8.kubernetes.client.Watch)11 Watcher (io.fabric8.kubernetes.client.Watcher)9 Pod (io.fabric8.kubernetes.api.model.Pod)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)8 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)7 PodList (io.fabric8.kubernetes.api.model.PodList)6 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 ArrayList (java.util.ArrayList)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)4 WatchImageConfiguration (io.fabric8.maven.docker.config.WatchImageConfiguration)4 IOException (java.io.IOException)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 LogWatch (io.fabric8.kubernetes.client.dsl.LogWatch)3 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)3 HashMap (java.util.HashMap)3 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 DoneableStatefulSet (io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet)2 StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)2