Search in sources :

Example 1 with PodStatusType

use of io.fabric8.kubernetes.api.PodStatusType 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)

Aggregations

PodStatusType (io.fabric8.kubernetes.api.PodStatusType)1 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 Watch (io.fabric8.kubernetes.client.Watch)1 Watcher (io.fabric8.kubernetes.client.Watcher)1 LogWatch (io.fabric8.kubernetes.client.dsl.LogWatch)1 Date (java.util.Date)1