@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(); } });
@Override public void eventReceived(Watcher.Action action, Pod pod) { podWaitLog.info(getName(pod) + " status: " + getPodStatusDescription(pod) + getPodStatusMessagePostfix(action)); if (isAddOrModified(action) && KubernetesHelper.isPodRunning(pod) && KubernetesHelper.isPodReady(pod) && podHasEnvVars(pod, envVars)) { foundPod = pod; terminateLatch.countDown(); } }
public static String getPodStatusDescription(Pod pod) { return KubernetesHelper.getPodPhase(pod) + " " + getPodCondition(pod); }
private Pod getNewestPod(LabelSelector selector) { FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = KubernetesClientUtil.withSelector(kubernetes.pods(), selector, log); PodList list = pods.list(); if (list != null) { List<Pod> items = list.getItems(); return getNewestPod(items); } return null; }
@Override protected void applyEntities(KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception { deleteEntities(kubernetes, namespace, entities, s2iBuildNameSuffix, log); } }
private void watchLog(final LogWatch logWatcher, String podName, final String failureMessage, String ctrlCMessage, String containerName) { context.getNewPodLog().info("Tailing log of pod: " + podName + containerNameMessage(containerName)); context.getNewPodLog().info("Press Ctrl-C to " + ctrlCMessage); context.getNewPodLog().info(""); KubernetesClientUtil.printLogsAsync(logWatcher, failureMessage, this.logWatchTerminateLatch, log); }
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 static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, Logger log) { List<HasMetadata> list = new ArrayList<>(entities); // For OpenShift cluster, also delete s2i buildconfig OpenShiftClient openshiftClient = OpenshiftHelper.asOpenShiftClient(kubernetes); if (openshiftClient != null) { for (HasMetadata entity : list) { if ("ImageStream".equals(KubernetesHelper.getKind(entity))) { ImageName imageName = new ImageName(entity.getMetadata().getName()); String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix); log.info("Deleting resource BuildConfig " + namespace + "/" + buildName); openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete(); } } } // lets delete in reverse order Collections.reverse(list); for (HasMetadata entity : list) { log.info("Deleting resource " + KubernetesHelper.getKind(entity) + " " + namespace + "/" + KubernetesHelper.getName(entity)); kubernetes.resource(entity).inNamespace(namespace).cascading(true).delete(); } }
nextForwardedPod[0] = newPod; final Watch watch = KubernetesClientUtil.withSelector(kubernetes.pods(), podSelector, log).watch(new Watcher<Pod>() {
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);
private void onPod(Watcher.Action action, Pod pod, KubernetesClient kubernetes, String namespace, String ctrlCMessage, boolean followLog) { String name = KubernetesHelper.getName(pod); if (action.equals(Watcher.Action.DELETED)) { addedPods.remove(name); if (Objects.equals(watchingPodName, name)) { watchingPodName = null; addedPods.remove(name); } } else { if (action.equals(Watcher.Action.ADDED) || action.equals(Watcher.Action.MODIFIED)) { addedPods.put(name, pod); } } Pod watchPod = KubernetesResourceUtil.getNewestPod(addedPods.values()); newestPodName = KubernetesHelper.getName(watchPod); Logger statusLog = Objects.equals(name, newestPodName) ? context.getNewPodLog() : context.getOldPodLog(); if (!action.equals(Watcher.Action.MODIFIED) || watchingPodName == null || !watchingPodName.equals(name)) { statusLog.info("%s status: %s%s", name, getPodStatusDescription(pod), getPodStatusMessagePostfix(action)); } if (watchPod != null && KubernetesHelper.isPodRunning(watchPod)) { watchLogOfPodName(kubernetes, namespace, ctrlCMessage, followLog, watchPod, KubernetesHelper.getName(watchPod)); } }
private String waitForRunningPodWithEnvVar(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final Map<String, String> envVars) throws MojoExecutionException { 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]] ");
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());