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;
}
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;
}
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);
}
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());
}
}
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());
}
}
}
}
Aggregations