private void deleteSeleniumPods() { logger.info("About to clean up any left over docker-selenium pods created by Zalenium"); client.pods().withLabels(createdByZaleniumMap).delete(); }
public boolean isTerminated(ContainerCreationStatus container) { Pod pod = client.pods().withName(container.getContainerName()).get(); if (pod == null) { logger.info("Container {} has no pod - terminal.", container); return true; } else { List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses(); Optional<ContainerStateTerminated> terminated = containerStatuses.stream() .flatMap(status -> Optional.ofNullable(status.getState()).map(Stream::of).orElse(Stream.empty())) .flatMap(state -> Optional.ofNullable(state.getTerminated()).map(Stream::of).orElse(Stream.empty())) .findFirst(); terminated.ifPresent(state -> logger.info("Container {} is {} - terminal.", container, state)); return terminated.isPresent(); } }
private boolean isPodReady(String name) { return kubernetesClient.pods() .withName(name) .isReady(); }
@Override public void stopContainer(String containerId) { client.pods().withName(containerId).delete(); }
public boolean isReady(ContainerCreationStatus container) { Pod pod = client.pods().withName(container.getContainerName()).get(); if (pod == null) { return false; } else { return pod.getStatus().getConditions().stream() .filter(condition -> condition.getType().equals("Ready")) .map(condition -> condition.getStatus().equals("True")) .findFirst() .orElse(false); } }
@Override public String getContainerIp(String containerName) { Pod pod = client.pods().withName(containerName).get(); if (pod != null) { String podIP = pod.getStatus().getPodIP(); logger.debug(String.format("Pod %s, IP -> %s", containerName, podIP)); return podIP; } else { return null; } }
public void start() { if (watch == null) { synchronized (lock) { if (watch == null) { LOGGER.debug("Starting pod readiness watcher for '{}'", podName); PodResource<Pod, DoneablePod> podResource = kubernetesClient .pods() .withName(podName); previousState = podResource.isReady(); watch = podResource.watch(this); } } } }
ExecWatch exec = client.pods().withName(containerId).writingOutput(baos).writingError(baos).usingListener(new ExecListener() {
private synchronized Pod internalGetPod() { try { if (isServiceAccountFound() && isHostNameEnvVarPresent()) { return client.pods().withName(hostName).get(); } else { return null; } } catch (Throwable t) { LOG.warn("Failed to get pod with name:[" + hostName + "]. You should look into this if things aren't working as you expect. Are you missing serviceaccount permissions?", t); return null; } }
zaleniumPod = client.pods().withName(hostname).get();
PodList list = client.pods().withLabels(createdByZaleniumMap).list();
/** * Copy some files by executing a tar command to the stdout and return the InputStream that contains the tar * contents. * <p> * Unfortunately due to the fact that any error handling happens on another thread, if the tar command fails the * InputStream will simply be empty and it will close. It won't propagate an Exception to the reader of the * InputStream. */ @Override public InputStream copyFiles(String containerId, String folderName) { ByteArrayOutputStream stderr = new ByteArrayOutputStream(); String[] command = new String[] { "tar", "-C", folderName, "-c", "." }; CopyFilesExecListener listener = new CopyFilesExecListener(stderr, command, containerId); ExecWatch exec = client.pods().withName(containerId).redirectingOutput().writingError(stderr).usingListener(listener).exec(command); // FIXME: This is a bit dodgy, but we need the listener to be able to close the ExecWatch in failure conditions, // because it doesn't cleanup properly and deadlocks. // Needs bugs fixed inside kubernetes-client. listener.setExecWatch(exec); // When zalenium is under high load sometimes the stdout isn't connected by the time we try to read from it. // Let's wait until it is connected before proceeding. listener.waitForInputStreamToConnect(); return exec.getOutput(); }
@Override public MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods() { return delegate.pods(); }
private synchronized Pod internalGetPod() { try { if (isServiceAccountFound() && isHostNameEnvVarPresent()) { return client.pods().withName(hostName).get(); } else { return null; } } catch (Throwable t) { LOGGER.warn("Failed to get pod with name:[" + hostName + "]. You should look into this if things aren't working as you expect. Are you missing serviceaccount permissions?", t); return null; } }
public static DoneablePod createDoneablePodDefaultImpl(PodConfiguration config) { DoneablePod doneablePod = config.getClient().pods() .createNew() .withNewMetadata()
public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage: podName [master] [namespace]"); return; } String podName = args[0]; String namespace = "default"; String master = "https://localhost:8443/"; if (args.length > 1) { master = args[1]; } if (args.length > 2) { namespace = args[2]; } System.out.println("Log of pod " + podName + " in " + namespace + " is:"); System.out.println("----------------------------------------------------------------"); Config config = new ConfigBuilder().withMasterUrl(master).build(); try (KubernetesClient client = new DefaultKubernetesClient(config); LogWatch watch = client.pods().inNamespace(namespace).withName(podName).tailingLines(10).watchLog(System.out)) { Thread.sleep(5 * 1000); } catch (Exception e) { logger.error(e.getMessage(), e); } } }
public static void main(String[] args) throws InterruptedException { String master = "https://localhost:8443/"; if (args.length == 1) { master = args[0]; } Config config = new ConfigBuilder().withMasterUrl(master) .withTrustCerts(true) .withUsername("admin") .withPassword("admin") .withNamespace("default") .build(); try (final KubernetesClient client = new AutoAdaptableKubernetesClient(config)) { log("Received pods", client.pods().list()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); Throwable[] suppressed = e.getSuppressed(); if (suppressed != null) { for (Throwable t : suppressed) { logger.error(t.getMessage(), t); } } } }
@Override public void eventReceived(Action action, Pod aPod) { log(aPod.getMetadata().getName(), aPod.getStatus().getPhase()); if(aPod.getStatus().getPhase().equals("Succeeded")) { log("Logs -> ", client.pods().inNamespace(namespace).withName(aPod.getMetadata().getName()).getLog()); watchLatch.countDown(); } }
public static void main(String[] args) throws InterruptedException { if (args.length < 1) { System.out.println("Usage: podName [master] [namespace]"); return; } String podName = args[0]; String namespace = "default"; String master = "https://localhost:8443/"; if (args.length > 1) { master = args[1]; } if (args.length > 2) { namespace = args[2]; } Config config = new ConfigBuilder().withMasterUrl(master).build(); try (final KubernetesClient client = new DefaultKubernetesClient(config); ExecWatch watch = client.pods().inNamespace(namespace).withName(podName) .readingInput(System.in) .writingOutput(System.out) .writingError(System.err) .withTTY() .usingListener(new SimpleListener()) .exec()){ Thread.sleep(10 * 1000); } }
public static void main(String[] args) { String master = "https://localhost:8443/"; if (args.length == 1) { master = args[0]; } Config config = new ConfigBuilder().withMasterUrl(master).build(); KubernetesClient client = new DefaultKubernetesClient(config); try { log("Create namespace:", client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName("thisisatest").endMetadata().build())); log("Deleted namespace:", client.namespaces().withName("test").delete()); log("Deleted testPod:", client.pods().inNamespace("thisisatest").withName("testpod").delete()); log("Deleted pod by label:", client.pods().withLabel("this", "works").delete()); } catch (KubernetesClientException e) { logger.error(e.getMessage(), e); } finally { client.namespaces().withName("thisisatest").delete(); client.close(); } }