use of org.eclipse.jkube.kit.build.api.model.ContainersListElement in project jkube by eclipse.
the class DockerAccessWithHcClient method getContainersForImage.
@Override
public List<Container> getContainersForImage(String image, boolean all) throws DockerAccessException {
String url;
String serverApiVersion = getServerApiVersion();
if (EnvUtil.greaterOrEqualsVersion(serverApiVersion, "1.23")) {
// For Docker >= 1.11 we can use a new filter when listing containers
url = urlBuilder.listContainers(all, "ancestor", image);
} else {
// For older versions (< Docker 1.11) we need to iterate over the containers.
url = urlBuilder.listContainers(all);
}
try {
String response = delegate.get(url, HTTP_OK);
JsonArray array = JsonFactory.newJsonArray(response);
List<Container> containers = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JsonObject element = array.get(i).getAsJsonObject();
if (image.equals(element.get("Image").getAsString())) {
containers.add(new ContainersListElement(element));
}
}
return containers;
} catch (IOException e) {
throw new DockerAccessException(e.getMessage());
}
}
Aggregations