Search in sources :

Example 1 with PodWatcher

use of io.fabric8.kubernetes.assertions.support.PodWatcher in project fabric8-maven-plugin by fabric8io.

the class DebugMojo method waitForRunningPodWithEnvVar.

private String waitForRunningPodWithEnvVar(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final Map<String, String> envVars) throws MojoExecutionException {
    // wait for the newest pod to be ready with the given env var
    FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
    log.info("Waiting for debug pod with selector " + selector + " and environment variables " + envVars);
    podWaitLog = createExternalProcessLogger("[[Y]][W][[Y]] ");
    PodList list = pods.list();
    if (list != null) {
        Pod latestPod = KubernetesResourceUtil.getNewestPod(list.getItems());
        if (latestPod != null && podHasEnvVars(latestPod, envVars)) {
            return getName(latestPod);
        }
    }
    podWatcher = pods.watch(new Watcher<Pod>() {

        @Override
        public void eventReceived(Watcher.Action action, Pod pod) {
            podWaitLog.info(getName(pod) + " status: " + getPodStatusDescription(pod) + getPodStatusMessagePostfix(action));
            if (isAddOrModified(action) && isPodRunning(pod) && isPodReady(pod) && podHasEnvVars(pod, envVars)) {
                foundPod = pod;
                terminateLatch.countDown();
            }
        }

        @Override
        public void onClose(KubernetesClientException e) {
        // ignore
        }
    });
    // now lets wait forever?
    while (terminateLatch.getCount() > 0) {
        try {
            terminateLatch.await();
        } catch (InterruptedException e) {
        // ignore
        }
        if (foundPod != null) {
            return getName(foundPod);
        }
    }
    throw new MojoExecutionException("Could not find a running pod with environment variables " + envVars);
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with PodWatcher

use of io.fabric8.kubernetes.assertions.support.PodWatcher in project fabric8-maven-plugin by fabric8io.

the class PodLogService method tailAppPodsLogs.

public void tailAppPodsLogs(final KubernetesClient kubernetes, final String namespace, final Set<HasMetadata> entities, boolean watchAddedPodsOnly, String onExitOperation, boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
    LabelSelector selector = KubernetesResourceUtil.getPodLabelSelector(entities);
    if (selector != null) {
        String ctrlCMessage = "stop tailing the log";
        if (Strings.isNotBlank(onExitOperation)) {
            final String onExitOperationLower = onExitOperation.toLowerCase().trim();
            if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
                ctrlCMessage = "undeploy the app";
            } else if (onExitOperationLower.equals(OPERATION_STOP)) {
                ctrlCMessage = "scale down the app and stop tailing the log";
            } else {
                log.warn("Unknown on-exit command: `%s`", onExitOperationLower);
            }
            resizeApp(kubernetes, namespace, entities, 1, log);
            Runtime.getRuntime().addShutdownHook(new Thread("pod log service shutdown hook") {

                @Override
                public void run() {
                    if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
                        log.info("Undeploying the app:");
                        deleteEntities(kubernetes, namespace, entities, context.getS2iBuildNameSuffix(), log);
                    } else if (onExitOperationLower.equals(OPERATION_STOP)) {
                        log.info("Stopping the app:");
                        resizeApp(kubernetes, namespace, entities, 0, log);
                    }
                    if (podWatcher != null) {
                        podWatcher.close();
                    }
                    closeLogWatcher();
                }
            });
        }
        waitAndLogPods(kubernetes, namespace, selector, watchAddedPodsOnly, ctrlCMessage, followLog, ignorePodsOlderThan, waitInCurrentThread);
    } else {
        log.warn("No selector in deployment so cannot watch pods!");
    }
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector)

Example 3 with PodWatcher

use of io.fabric8.kubernetes.assertions.support.PodWatcher 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 4 with PodWatcher

use of io.fabric8.kubernetes.assertions.support.PodWatcher in project fabric8 by fabric8io.

the class PodSelectionAssert method isPodReadyForPeriod.

/**
 * Asserts that a pod is ready for this deployment all become ready within the given time and that each one keeps being ready for the given time
 */
public PodSelectionAssert isPodReadyForPeriod(long notReadyTimeoutMS, long readyPeriodMS) {
    if (replicas.intValue() <= 0) {
        LOG.warn("Not that the pod selection for: " + description + " has no replicas defined so we cannot assert there is a pod ready");
        return this;
    }
    try (PodWatcher podWatcher = new PodWatcher(this, notReadyTimeoutMS, readyPeriodMS);
        Watch watch = client.pods().withLabels(matchLabels).watch(podWatcher)) {
        podWatcher.loadCurrentPods();
        podWatcher.waitForPodReady();
    }
    return this;
}
Also used : Watch(io.fabric8.kubernetes.client.Watch) PodWatcher(io.fabric8.kubernetes.assertions.support.PodWatcher)

Aggregations

Watch (io.fabric8.kubernetes.client.Watch)3 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodList (io.fabric8.kubernetes.api.model.PodList)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 Watcher (io.fabric8.kubernetes.client.Watcher)2 PodStatusType (io.fabric8.kubernetes.api.PodStatusType)1 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)1 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1 PodWatcher (io.fabric8.kubernetes.assertions.support.PodWatcher)1 LogWatch (io.fabric8.kubernetes.client.dsl.LogWatch)1 Date (java.util.Date)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1