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); } } } }
public Watch watch(final String podName, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished, Boolean cleanUpOnFinish) { Callable<Void> onCompletion = cleanUpOnFinish ? new Callable<Void>() { @Override public Void call() throws Exception { cleanUp(); return null; } } : null; Watch watch = client.pods().withName(podName).watch(new PodWatcher(alive, started, finished, onCompletion)); synchronized (closeables) { closeables.add(watch); } return watch; }
/** * Watch the pod identified by the given {@code namespace} and {@code name} using the given {@code watcher}. * @param namespace The namespace * @param name The name * @param watcher The watcher * @return The watch */ public Watch watch(String namespace, String name, Watcher<Pod> watcher) { return operation().inNamespace(namespace).withName(name).watch(watcher); } }
/** * A Simple utility function to watch over pod until it gets ready * * @param podName Name of the pod * @param nAwaitTimeout Time in seconds upto which pod must be watched * @param log Logger object * @throws InterruptedException */ private void waitUntilPodIsReady(String podName, int nAwaitTimeout, final Logger log) throws InterruptedException { final CountDownLatch readyLatch = new CountDownLatch(1); try (Watch watch = client.pods().withName(podName).watch(new Watcher<Pod>() { @Override public void eventReceived(Action action, Pod aPod) { if(KubernetesHelper.isPodReady(aPod)) { readyLatch.countDown(); } } @Override public void onClose(KubernetesClientException e) { // Ignore } })) { readyLatch.await(nAwaitTimeout, TimeUnit.SECONDS); } catch (KubernetesClientException | InterruptedException e) { log.error("Could not watch pod", e); } }