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();
private static Map<String, String> getData(KubernetesClient client, String name, String namespace, String[] profiles) { try { ConfigMap map = StringUtils.isEmpty(namespace) ? client.configMaps().withName(name).get() : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { return processAllEntries(map.getData(), profiles); } } catch (Exception e) { LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring"); } return new HashMap<>(); }
"[Assertion failed] - the object argument must be null"); Endpoints endpoints = client.endpoints().withName(serviceId).get(); List<EndpointSubset> subsets = null != endpoints ? endpoints.getSubsets() : new ArrayList<>(); List<ServiceInstance> instances = new ArrayList<>(); if (!subsets.isEmpty()) { final Service service = client.services().withName(serviceId).get();
public List<Server> getUpdatedListOfServers() { Endpoints endpoints = namespace != null ? client.endpoints().inNamespace(namespace).withName(serviceId).get() : client.endpoints().withName(serviceId).get();
/** * 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(); }
secret = client.secrets().withName(name).get();
@Override public List<ServiceInstance> getInstances(String serviceId) { Assert.notNull(serviceId, "[Assertion failed] - the object argument must be null"); return Optional.ofNullable(client.endpoints().withName(serviceId).get()).orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(serviceId, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
@Override public User currentUser() { return users().withName("~").get(); }
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; } }
@Override public ServiceInstance getLocalServiceInstance() { String serviceName = properties.getServiceName(); String podName = System.getenv(HOSTNAME); ServiceInstance defaultInstance = new DefaultServiceInstance(serviceName, "localhost", 8080, false); Endpoints endpoints = client.endpoints().withName(serviceName).get(); if (Utils.isNullOrEmpty(podName) || endpoints == null) { return defaultInstance; } try { return endpoints.getSubsets() .stream() .filter(s -> s.getAddresses().get(0).getTargetRef().getName().equals(podName)) .map(s -> (ServiceInstance) new KubernetesServiceInstance(serviceName, s.getAddresses().stream().findFirst().orElseThrow(IllegalStateException::new), s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false)) .findFirst().orElse(defaultInstance); } catch (Throwable t) { return defaultInstance; } }
public void start() { ConfigMap map = Strings.isNullOrEmpty(namespace) ? client.configMaps().withName(name).get() : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { currentData.set(asObjectMap(map.getData())); } watch = Strings.isNullOrEmpty(namespace) ? client.configMaps().withName(name).watch(watcher) : client.configMaps().inNamespace(namespace).withName(namespace).watch(watcher); started.set(true); }
public static void main(String[] args) throws InterruptedException { String namespace = "myproject"; String master = "CLUSTER_URL"; Config config = new ConfigBuilder().withMasterUrl(master).build(); OpenShiftClient client = new DefaultOpenShiftClient(config); try { ImageStreamTag istag = new ImageStreamTagBuilder().withNewMetadata().withName("bar1:1.0.12").endMetadata() .withNewTag().withNewFrom().withKind("DockerImage").withName("openshift/wildfly-81-centos7:latest").endFrom().endTag() .build(); log("Created istag", client.imageStreamTags().inNamespace(namespace).create(istag)); Thread.sleep(30000); }finally { log("ImageStreamTags are :"); log(client.imageStreamTags().inNamespace(namespace).withName("bar1:1.0.12").get().toString()); log("ImageStreamTags using list are :"); log(client.imageStreamTags().list().getItems().get(0).toString()); log("Deleted istag",client.imageStreamTags().withName("bar1:1.0.12").delete()); client.close(); } }