Search in sources :

Example 1 with ContainersListElement

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());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) Container(org.eclipse.jkube.kit.build.api.model.Container) ContainersListElement(org.eclipse.jkube.kit.build.api.model.ContainersListElement) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Container (org.eclipse.jkube.kit.build.api.model.Container)1 ContainersListElement (org.eclipse.jkube.kit.build.api.model.ContainersListElement)1 DockerAccessException (org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException)1