use of io.fabric8.kubernetes.api.model.PodList in project fabric8-maven-plugin by fabric8io.
the class PodLogService method waitAndLogPods.
private void waitAndLogPods(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final boolean watchAddedPodsOnly, final String ctrlCMessage, final boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
if (context.getPodName() != null) {
log.info("Watching pod with selector %s, and name %s waiting for a running pod...", selector, context.getPodName());
pods = pods.withField("metadata.name", context.getPodName());
} else {
log.info("Watching pods with selector %s waiting for a running pod...", selector);
}
Pod latestPod = null;
boolean runningPod = false;
PodList list = pods.list();
if (list != null) {
List<Pod> items = list.getItems();
if (items != null) {
for (Pod pod : items) {
PodStatusType status = getPodStatus(pod);
switch(status) {
case WAIT:
case OK:
if (latestPod == null || KubernetesResourceUtil.isNewerResource(pod, latestPod)) {
if (ignorePodsOlderThan != null) {
Date podCreateTime = KubernetesResourceUtil.getCreationTimestamp(pod);
if (podCreateTime != null && podCreateTime.compareTo(ignorePodsOlderThan) > 0) {
latestPod = pod;
}
} else {
latestPod = pod;
}
}
runningPod = true;
break;
case ERROR:
default:
continue;
}
}
}
}
// we may have missed the ADDED event so lets simulate one
if (latestPod != null) {
onPod(Watcher.Action.ADDED, latestPod, kubernetes, namespace, ctrlCMessage, followLog);
}
if (!watchAddedPodsOnly) {
// lets watch the current pods then watch for changes
if (!runningPod) {
log.warn("No pod is running yet. Are you sure you deployed your app via `fabric8:deploy`?");
log.warn("Or did you stop it via `fabric8:stop`? If so try running the `fabric8:start` goal");
}
}
podWatcher = pods.watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod pod) {
onPod(action, pod, kubernetes, namespace, ctrlCMessage, followLog);
}
@Override
public void onClose(KubernetesClientException e) {
// ignore
}
});
if (waitInCurrentThread) {
while (terminateLatch.getCount() > 0) {
try {
terminateLatch.await();
} catch (InterruptedException e) {
// ignore
}
}
}
}
use of io.fabric8.kubernetes.api.model.PodList in project fabric8 by fabric8io.
the class Util method cleanupAllResources.
public static void cleanupAllResources(KubernetesClient client, Session session, List<Throwable> errors) throws MultiException {
String sessionNamespace = session.getNamespace();
session.getLogger().info("Removing all resources in namespace " + sessionNamespace);
/**
* Lets use a loop to ensure we really do delete all the matching resources
*/
for (int i = 0; i < 10; i++) {
OpenShiftClient openShiftClient = new Controller(client).getOpenShiftClientOrNull();
if (openShiftClient != null) {
try {
openShiftClient.deploymentConfigs().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
openShiftClient.routes().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
}
try {
client.extensions().deployments().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.extensions().replicaSets().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.replicationControllers().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.pods().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.extensions().ingresses().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.services().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.securityContextConstraints().withName(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
// lets see if there are any matching podList left
List<Pod> filteredPods = notNullList(client.pods().inNamespace(sessionNamespace).list().getItems());
if (filteredPods.isEmpty()) {
return;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
use of io.fabric8.kubernetes.api.model.PodList in project syndesis by syndesisio.
the class ActivityTrackingController method pollPods.
private void pollPods() {
try {
// clear the marks
for (PodLogMonitor handler : podHandlers.values()) {
handler.markInOpenshift.set(false);
}
PodList podList = listPods();
for (Pod pod : podList.getItems()) {
// We are only looking for running containers.
if (!"Running".equals(pod.getStatus().getPhase())) {
continue;
}
String name = pod.getMetadata().getName();
PodLogMonitor handler = podHandlers.get(name);
if (handler == null) {
// create a new handler.
try {
handler = new PodLogMonitor(this, pod);
handler.start();
LOG.info("Created handler for pod: {}", handler.podName);
podHandlers.put(name, handler);
} catch (IOException e) {
LOG.error("Unexpected Error", e);
}
} else {
// mark existing handlers as being used.
handler.markInOpenshift.set(true);
}
}
// Remove items from the map which are no longer in openshift
Iterator<Map.Entry<String, PodLogMonitor>> iterator = podHandlers.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, PodLogMonitor> next = iterator.next();
if (!next.getValue().markInOpenshift.get()) {
LOG.info("Pod not tracked by openshift anymore: {}", next.getValue().podName);
next.getValue().keepTrying.set(false);
iterator.remove();
}
}
@SuppressWarnings("unchecked") Map<String, Object> // NOPMD
pods = dbGet(HashMap.class, "/activity/pods");
if (pods != null) {
pods.keySet().removeAll(podHandlers.keySet());
for (String o : pods.keySet()) {
jsonDB.delete("/activity/pods/" + o);
LOG.info("Pod state removed from db: {}", o);
}
}
} catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException | IOException e) {
LOG.error("Unexpected Error occurred.", e);
}
}
use of io.fabric8.kubernetes.api.model.PodList in project syndesis by syndesisio.
the class ActivityTrackingControllerTest method testLogsController.
@Test
public void testLogsController() throws IOException {
final String expectedDBState = resource("logs-controller-db.json").trim();
final String podLogs = resource("test-pod-x23x.txt");
try (ActivityTrackingController controller = new ActivityTrackingController(jsondb, dbi, null) {
@Override
protected PodList listPods() {
return new PodListBuilder().addNewItem().withNewMetadata().withName("test-pod-x23x").addToLabels(OpenShiftService.COMPONENT_LABEL, "integration").addToLabels(OpenShiftService.DEPLOYMENT_VERSION_LABEL, "3").addToLabels(OpenShiftService.INTEGRATION_ID_LABEL, "my-integration").endMetadata().withNewStatus().withPhase("Running").endStatus().endItem().build();
}
@Override
protected boolean isPodRunning(String name) {
return true;
}
@Override
protected void watchLog(String podName, Consumer<InputStream> handler, String sinceTime) throws IOException {
executor.execute(() -> {
handler.accept(new ByteArrayInputStream(podLogs.getBytes(StandardCharsets.UTF_8)));
});
}
}) {
controller.setStartupDelay("0 seconds");
controller.setRetention("1000000000 days");
controller.open();
// Eventually all the log data should make it into the jsondb
given().await().atMost(20, SECONDS).pollInterval(1, SECONDS).untilAsserted(() -> {
String db = jsondb.getAsString("/", new GetOptions().prettyPrint(true));
assertThat(db).isEqualTo(expectedDBState);
});
}
}
use of io.fabric8.kubernetes.api.model.PodList in project zalenium by zalando.
the class KubernetesContainerClient method getRunningContainers.
@Override
public int getRunningContainers(String image) {
PodList list = client.pods().withLabels(createdByZaleniumMap).list();
logger.debug("Pods in the list " + list.getItems().size());
int count = 0;
for (Pod pod : list.getItems()) {
String phase = pod.getStatus().getPhase();
if ("Running".equalsIgnoreCase(phase) || "Pending".equalsIgnoreCase(phase)) {
count++;
}
}
return count;
}
Aggregations