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); } } } }
secret = client.secrets().withName(name).get(); secret = client.secrets().inNamespace(namespace).withName(name).get(); client.secrets().withLabels(config.getLabels()).list().getItems() .forEach(s -> putAll(s, result)); client.secrets().inNamespace(namespace) .withLabels(config.getLabels()).list().getItems() .forEach(s -> putAll(s, result));
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<>(); }
private ConfigMap getConfigMap() { return kubernetesClient.configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .withName(leaderProperties.getConfigMapName()) .get(); }
private void updateConfigMap(ConfigMap oldConfigMap, ConfigMap newConfigMap) { kubernetesClient.configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .withName(leaderProperties.getConfigMapName()) .lockResourceVersion(oldConfigMap.getMetadata().getResourceVersion()) .replace(newConfigMap); } }
public void start() { if (watch == null) { synchronized (lock) { if (watch == null) { LOGGER.debug("Starting leader record watcher"); watch = kubernetesClient .configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .withName(leaderProperties.getConfigMapName()) .watch(this); } } } }
private void createConfigMap(Map<String, String> data) { LOGGER.debug("Creating new config map with data: {}", data); ConfigMap newConfigMap = new ConfigMapBuilder().withNewMetadata() .withName(leaderProperties.getConfigMapName()) .addToLabels(PROVIDER_KEY, PROVIDER) .addToLabels(KIND_KEY, KIND) .endMetadata() .addToData(data) .build(); kubernetesClient.configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .create(newConfigMap); }
/** * 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(); }
ExecWatch exec = client.pods().withName(containerId).writingOutput(baos).writingError(baos).usingListener(new ExecListener() { latch.countDown(); }).exec(command); logger.error(String.format("%s Failed to execute command %s", containerId, Arrays.toString(command)), e); } finally { exec.close();
public List<String> getServices(Predicate<Service> filter) { return kubernetesClientServicesFunction.apply(client).list() .getItems() .stream() .filter(filter) .map(s -> s.getMetadata().getName()) .collect(Collectors.toList()); }
private void deleteSeleniumPods() { logger.info("About to clean up any left over docker-selenium pods created by Zalenium"); client.pods().withLabels(createdByZaleniumMap).delete(); }
@Bean public KubernetesClientServicesFunction servicesFunction(KubernetesDiscoveryProperties properties) { if (properties.getServiceLabels().isEmpty()) { return KubernetesClient::services; } return (client) -> client.services().withLabels(properties.getServiceLabels()); }
@Override public void onClose(int code, String reason) { // Dirty hack to workaround the fact that ExecWatch doesn't automatically close any resources boolean isClosed = closedResource.getAndSet(true); boolean hasErrors = stderr.size() > 0; if (!isClosed && hasErrors) { logger.error(String.format("%s Copy files command failed with:\n\tcommand: %s\n\t stderr:\n%s", containerId, Arrays.toString(command), stderr.toString())); this.execWatch.close(); } }
public List<Server> getUpdatedListOfServers() { Endpoints endpoints = namespace != null ? client.endpoints().inNamespace(namespace).withName(serviceId).get() : client.endpoints().withName(serviceId).get();
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; } }
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); } }
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(); } }