Search in sources :

Example 1 with Container

use of org.eclipse.jkube.kit.build.api.model.Container in project jkube by eclipse.

the class RunService method createAndStartContainer.

/**
 * Create and start a container with the given image configuration.
 * @param imageConfig image configuration holding the run information and the image name
 * @param portMapping container port mapping
 * @param gavLabel label to tag the started container with
 * @param baseDir base directory
 * @param properties properties to fill in with dynamically assigned ports
 * @param defaultContainerNamePattern pattern to use for naming containers. Can be null in which case a default pattern is used
 * @param buildTimestamp date which should be used as the timestamp when calculating container names
 * @return the container id
 *
 * @throws DockerAccessException if access to the docker backend fails
 */
public String createAndStartContainer(ImageConfiguration imageConfig, PortMapping portMapping, GavLabel gavLabel, Properties properties, File baseDir, String defaultContainerNamePattern, Date buildTimestamp) throws DockerAccessException {
    RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
    String imageName = imageConfig.getName();
    Collection<Container> existingContainers = queryService.getContainersForImage(imageName, true);
    String containerName = ContainerNamingUtil.formatContainerName(imageConfig, defaultContainerNamePattern, buildTimestamp, existingContainers);
    ContainerCreateConfig config = createContainerConfig(imageName, runConfig, portMapping, gavLabel, properties, baseDir);
    String id = docker.createContainer(config, containerName);
    startContainer(imageConfig, id, gavLabel);
    if (portMapping.needsPropertiesUpdate()) {
        updateMappedPortsAndAddresses(id, portMapping);
    }
    return id;
}
Also used : Container(org.eclipse.jkube.kit.build.api.model.Container) ContainerCreateConfig(org.eclipse.jkube.kit.build.service.docker.access.ContainerCreateConfig) RunImageConfiguration(org.eclipse.jkube.kit.config.image.RunImageConfiguration)

Example 2 with Container

use of org.eclipse.jkube.kit.build.api.model.Container in project jkube by eclipse.

the class WaitService method prepareWaitCheckers.

private List<WaitChecker> prepareWaitCheckers(ImageConfiguration imageConfig, Properties projectProperties, String containerId) throws IOException {
    WaitConfiguration wait = getWaitConfiguration(imageConfig);
    if (wait == null) {
        return Collections.emptyList();
    }
    List<WaitChecker> checkers = new ArrayList<>();
    if (wait.getUrl() != null) {
        checkers.add(getUrlWaitChecker(imageConfig.getDescription(), projectProperties, wait));
    }
    if (wait.getLog() != null) {
        log.debug("LogWaitChecker: Waiting on %s", wait.getLog());
        checkers.add(new LogWaitChecker(wait.getLog(), dockerAccess, containerId, log));
    }
    if (wait.getTcp() != null) {
        try {
            Container container = queryService.getMandatoryContainer(containerId);
            checkers.add(getTcpWaitChecker(container, imageConfig.getDescription(), projectProperties, wait.getTcp()));
        } catch (DockerAccessException e) {
            throw new IOException("Unable to access container " + containerId, e);
        }
    }
    if (wait.getHealthy() == Boolean.TRUE) {
        checkers.add(new HealthCheckChecker(dockerAccess, containerId, imageConfig.getDescription(), log));
    }
    if (wait.getExit() != null) {
        checkers.add(new ExitCodeChecker(wait.getExit(), queryService, containerId));
    }
    return checkers;
}
Also used : WaitConfiguration(org.eclipse.jkube.kit.config.image.WaitConfiguration) HealthCheckChecker(org.eclipse.jkube.kit.build.service.docker.wait.HealthCheckChecker) Container(org.eclipse.jkube.kit.build.api.model.Container) LogWaitChecker(org.eclipse.jkube.kit.build.service.docker.wait.LogWaitChecker) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) ArrayList(java.util.ArrayList) ExitCodeChecker(org.eclipse.jkube.kit.build.service.docker.wait.ExitCodeChecker) IOException(java.io.IOException) WaitChecker(org.eclipse.jkube.kit.build.service.docker.wait.WaitChecker) LogWaitChecker(org.eclipse.jkube.kit.build.service.docker.wait.LogWaitChecker)

Example 3 with Container

use of org.eclipse.jkube.kit.build.api.model.Container in project jkube by eclipse.

the class ContainerNamingUtil method keepOnlyLastIndexedContainer.

// Filter out any older indexed containernames, keeping only the last one (i.e. with the highest index)
private static Collection<Container> keepOnlyLastIndexedContainer(Collection<Container> existingContainers, final String partiallyApplied) {
    Collection<Container> result = new ArrayList<>(existingContainers);
    // No index place holder, so nothing to filters
    if (!partiallyApplied.contains(INDEX_PLACEHOLDER)) {
        return result;
    }
    Map<String, Container> containerMap = existingContainers.stream().collect(Collectors.toMap(Container::getName, Function.identity()));
    Container last = null;
    for (long i = 1; i < Long.MAX_VALUE; i++) {
        final String withIndexApplied = partiallyApplied.replaceAll(INDEX_PLACEHOLDER, String.valueOf(i));
        Container mapped = containerMap.get(withIndexApplied);
        if (mapped != null) {
            result.remove(mapped);
            last = mapped;
        } else {
            // Readd last one removed (if any)
            if (last != null) {
                result.add(last);
            }
            return result;
        }
    }
    throw new IllegalStateException("Internal error: Cannot find a free container index slot in " + existingContainers);
}
Also used : Container(org.eclipse.jkube.kit.build.api.model.Container) ArrayList(java.util.ArrayList)

Example 4 with Container

use of org.eclipse.jkube.kit.build.api.model.Container 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)

Example 5 with Container

use of org.eclipse.jkube.kit.build.api.model.Container in project jkube by eclipse.

the class StartContainerExecutor method exposeContainerProps.

private void exposeContainerProps(String containerId) throws DockerAccessException {
    String propKey = getExposedPropertyKeyPart();
    if (StringUtils.isNotEmpty(exposeContainerProps) && StringUtils.isNotEmpty(propKey)) {
        Container container = hub.getQueryService().getMandatoryContainer(containerId);
        String prefix = addDot(exposeContainerProps) + addDot(propKey);
        projectProperties.put(prefix + "id", containerId);
        String ip = container.getIPAddress();
        if (StringUtils.isNotEmpty(ip)) {
            projectProperties.put(prefix + "ip", ip);
        }
        Map<String, String> nets = container.getCustomNetworkIpAddresses();
        if (nets != null) {
            for (Map.Entry<String, String> entry : nets.entrySet()) {
                projectProperties.put(prefix + addDot("net") + addDot(entry.getKey()) + "ip", entry.getValue());
            }
        }
    }
}
Also used : Container(org.eclipse.jkube.kit.build.api.model.Container) Map(java.util.Map)

Aggregations

Container (org.eclipse.jkube.kit.build.api.model.Container)6 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 DockerAccessException (org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Map (java.util.Map)1 ContainersListElement (org.eclipse.jkube.kit.build.api.model.ContainersListElement)1 ContainerCreateConfig (org.eclipse.jkube.kit.build.service.docker.access.ContainerCreateConfig)1 ExitCodeChecker (org.eclipse.jkube.kit.build.service.docker.wait.ExitCodeChecker)1 HealthCheckChecker (org.eclipse.jkube.kit.build.service.docker.wait.HealthCheckChecker)1 LogWaitChecker (org.eclipse.jkube.kit.build.service.docker.wait.LogWaitChecker)1 WaitChecker (org.eclipse.jkube.kit.build.service.docker.wait.WaitChecker)1 RunImageConfiguration (org.eclipse.jkube.kit.config.image.RunImageConfiguration)1 WaitConfiguration (org.eclipse.jkube.kit.config.image.WaitConfiguration)1