public LogWatch watchPodLog(String name, String container, OutputStream outputStream) { return client.pods().withName(name).inContainer(container).watchLog(outputStream); }
/*** * Returns log of conrainer in pod * @param podName name of pod * @param containerName name of container in pod * @return log */ public String getLog(String podName, String containerName) { return client.pods().inNamespace(globalNamespace).withName(podName).inContainer(containerName).getLog(); }
/** * Log the last lines of containers logs */ private void logLastLines(List<ContainerStatus> containers, String podId, String namespace, KubernetesSlave slave, Map<String, Integer> errors, KubernetesClient client) { for (ContainerStatus containerStatus : containers) { String containerName = containerStatus.getName(); PrettyLoggable<String, LogWatch> tailingLines = client.pods().inNamespace(namespace) .withName(podId).inContainer(containerStatus.getName()).tailingLines(30); String log = tailingLines.getLog(); if (!StringUtils.isBlank(log)) { String msg = errors != null ? String.format(" exited with error %s", errors.get(containerName)) : ""; LOGGER.log(Level.SEVERE, "Error in provisioning; agent={0}, template={1}. Container {2}{3}. Logs: {4}", new Object[]{slave, slave.getTemplate(), containerName, msg, tailingLines.getLog()}); } } }
public ExecWatch exec(String podName, String containerName, final AtomicBoolean alive, final CountDownLatch started, final CountDownLatch finished, final PrintStream out, final String... statements) { ExecWatch watch = client.pods().withName(podName) .inContainer(containerName) .redirectingInput() .writingOutput(out)
public String runOnPod(Pod pod, String container, String... command) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); log.info("Running command on pod {}: {}", pod.getMetadata().getName(), command); CompletableFuture<String> data = new CompletableFuture<>(); try (ExecWatch execWatch = client.pods().inNamespace(pod.getMetadata().getNamespace()) .withName(pod.getMetadata().getName()).inContainer(container) .readingInput(null) .writingOutput(baos) .usingListener(new ExecListener() { @Override public void onOpen(Response response) { log.info("Reading data..."); } @Override public void onFailure(Throwable throwable, Response response) { data.completeExceptionally(throwable); } @Override public void onClose(int i, String s) { data.complete(baos.toString()); } }).exec(command)) { return data.get(1, TimeUnit.MINUTES); } catch (Exception e) { log.warn("Exception running command {} on pod: {}", command, e.getMessage()); return ""; } }
printStream.println(msg); Execable<String, ExecWatch> execable = client.pods().inNamespace(namespace).withName(podName).inContainer(containerName) .redirectingInput().writingOutput(stream).writingError(stream).writingErrorChannel(error) .usingListener(new ExecListener() {
public HashMap<String, String> getLogsOfTerminatedPods(String namespace) { HashMap<String, String> terminatedPodsLogs = new HashMap<>(); try { client.pods().inNamespace(namespace).list().getItems().forEach(pod -> { pod.getStatus().getContainerStatuses().forEach(containerStatus -> { log.info("pod:'{}' : restart count '{}'", pod.getMetadata().getName(), containerStatus.getRestartCount()); if (containerStatus.getRestartCount() > 0) { terminatedPodsLogs.put( pod.getMetadata().getName(), client.pods().inNamespace(namespace) .withName(pod.getMetadata().getName()) .inContainer(containerStatus.getName()) .terminated().getLog()); } }); }); } catch (Exception allExceptions) { log.warn("Searching in terminated pods failed! No logs of terminated pods will be stored."); allExceptions.printStackTrace(); } return terminatedPodsLogs; }
} else { containerName = getLogContainerName(containers); logWatcher = podResource.inContainer(containerName).watchLog(); } else { containerName = getLogContainerName(containers); logText = podResource.inContainer(containerName).getLog();
.inNamespace(nodeContext.getNamespace()) .withName(podName) .inContainer(containerName);
for (Container container : containers) { File logFile = LogHelpers.getLogFileName(session.getBaseDir(), name, container, restartCount); String log = kubernetesClient.pods().inNamespace(session.getNamespace()).withName(name).inContainer(container.getName()).getLog(); IOHelpers.writeFully(logFile, log);