use of org.eclipse.che.plugin.docker.client.json.PortBinding in project che by eclipse.
the class OpenShiftConnector method createContainerInfo.
/**
* Collects the relevant information from a Service, an ImageInfo, and a Pod into
* a docker ContainerInfo JSON object. The returned object is what would be returned
* by executing {@code docker inspect <container>}, with fields filled as available.
* @param svc
* @param imageInfo
* @param pod
* @param containerId
* @return
*/
private ContainerInfo createContainerInfo(Service svc, ImageInfo imageInfo, Pod pod, String containerId) {
// In Che on OpenShift, we only have one container per pod.
Container container = pod.getSpec().getContainers().get(0);
ContainerConfig imageContainerConfig = imageInfo.getContainerConfig();
// HostConfig
HostConfig hostConfig = new HostConfig();
hostConfig.setBinds(new String[0]);
hostConfig.setMemory(imageInfo.getConfig().getMemory());
// Env vars
List<String> imageEnv = Arrays.asList(imageContainerConfig.getEnv());
List<String> containerEnv = container.getEnv().stream().map(e -> String.format("%s=%s", e.getName(), e.getValue())).collect(Collectors.toList());
String[] env = Stream.concat(imageEnv.stream(), containerEnv.stream()).toArray(String[]::new);
// Exposed Ports
Map<String, List<PortBinding>> ports = getCheServicePorts(svc);
Map<String, Map<String, String>> exposedPorts = new HashMap<>();
for (String key : ports.keySet()) {
exposedPorts.put(key, Collections.emptyMap());
}
// Labels
Map<String, String> annotations = KubernetesLabelConverter.namesToLabels(svc.getMetadata().getAnnotations());
Map<String, String> containerLabels = imageInfo.getConfig().getLabels();
Map<String, String> labels = Stream.concat(annotations.entrySet().stream(), containerLabels.entrySet().stream()).filter(e -> e.getKey().startsWith(KubernetesLabelConverter.getCheServerLabelPrefix())).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
// ContainerConfig
ContainerConfig config = imageContainerConfig;
config.setHostname(svc.getMetadata().getName());
config.setEnv(env);
config.setExposedPorts(exposedPorts);
config.setLabels(labels);
config.setImage(container.getImage());
// NetworkSettings
NetworkSettings networkSettings = new NetworkSettings();
networkSettings.setIpAddress(svc.getSpec().getClusterIP());
networkSettings.setGateway(svc.getSpec().getClusterIP());
networkSettings.setPorts(ports);
// Make final ContainerInfo
ContainerInfo info = new ContainerInfo();
info.setId(containerId);
info.setConfig(config);
info.setNetworkSettings(networkSettings);
info.setHostConfig(hostConfig);
info.setImage(imageInfo.getConfig().getImage());
return info;
}
use of org.eclipse.che.plugin.docker.client.json.PortBinding in project che by eclipse.
the class OpenShiftConnector method getCheServicePorts.
private Map<String, List<PortBinding>> getCheServicePorts(Service service) {
Map<String, List<PortBinding>> networkSettingsPorts = new HashMap<>();
List<ServicePort> servicePorts = service.getSpec().getPorts();
LOG.info("Retrieving {} ports exposed by service {}", servicePorts.size(), service.getMetadata().getName());
for (ServicePort servicePort : servicePorts) {
String protocol = servicePort.getProtocol();
String targetPort = String.valueOf(servicePort.getTargetPort().getIntVal());
String nodePort = String.valueOf(servicePort.getNodePort());
String portName = servicePort.getName();
LOG.info("Port: {}{}{} ({})", targetPort, DOCKER_PROTOCOL_PORT_DELIMITER, protocol, portName);
networkSettingsPorts.put(targetPort + DOCKER_PROTOCOL_PORT_DELIMITER + protocol.toLowerCase(), Collections.singletonList(new PortBinding().withHostIp(CHE_DEFAULT_EXTERNAL_ADDRESS).withHostPort(nodePort)));
}
return networkSettingsPorts;
}
use of org.eclipse.che.plugin.docker.client.json.PortBinding in project che by eclipse.
the class LocalDockerServerEvaluationStrategyTest method setUp.
@BeforeMethod
public void setUp() {
serverConfs = new HashMap<>();
serverConfs.put("4301/tcp", new ServerConfImpl("sysServer1-tcp", "4301/tcp", "http", "/some/path1"));
serverConfs.put("4305/udp", new ServerConfImpl("devSysServer1-udp", "4305/udp", null, "some/path4"));
ports = new HashMap<>();
ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS).withHostPort("32100")));
ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS).withHostPort("32103")));
when(containerInfo.getNetworkSettings()).thenReturn(networkSettings);
when(networkSettings.getGateway()).thenReturn(CONTAINERINFO_GATEWAY);
when(networkSettings.getIpAddress()).thenReturn(CONTAINERINFO_IP_ADDRESS);
when(networkSettings.getPorts()).thenReturn(ports);
when(containerInfo.getConfig()).thenReturn(containerConfig);
when(containerConfig.getLabels()).thenReturn(Collections.emptyMap());
}
use of org.eclipse.che.plugin.docker.client.json.PortBinding in project che by eclipse.
the class ServerEvaluationStrategyTest method shouldAddRefUrlPathToServerFromLabels.
@Test
public void shouldAddRefUrlPathToServerFromLabels() throws Exception {
// given
Map<String, List<PortBinding>> ports = prepareStrategyAndContainerInfoMocks();
Map<String, String> labels = new HashMap<>();
when(containerConfig.getLabels()).thenReturn(labels);
ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS).withHostPort("32100")));
ports.put("9090/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS).withHostPort("32101")));
labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1");
labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/tcp"), "http");
labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "8080/tcp"), "/some/path");
labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "9090/udp"), "dhcp");
labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "9090/udp"), "some/path");
final HashMap<String, ServerImpl> expectedServers = new HashMap<>();
expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", DEFAULT_HOSTNAME + ":32100", "http://" + DEFAULT_HOSTNAME + ":32100/some/path", new ServerPropertiesImpl("/some/path", DEFAULT_HOSTNAME + ":32100", "http://" + DEFAULT_HOSTNAME + ":32100/some/path")));
expectedServers.put("9090/udp", new ServerImpl("Server-9090-udp", "dhcp", DEFAULT_HOSTNAME + ":32101", "dhcp://" + DEFAULT_HOSTNAME + ":32101/some/path", new ServerPropertiesImpl("some/path", DEFAULT_HOSTNAME + ":32101", "dhcp://" + DEFAULT_HOSTNAME + ":32101/some/path")));
// when
final Map<String, ServerImpl> servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs);
// then
assertEquals(servers, expectedServers);
}
use of org.eclipse.che.plugin.docker.client.json.PortBinding in project che by eclipse.
the class MachineProviderImpl method createContainer.
private String createContainer(String workspaceId, String machineName, boolean isDev, String image, String networkName, CheServiceImpl service) throws IOException {
long machineMemorySwap = memorySwapMultiplier == -1 ? -1 : (long) (service.getMemLimit() * memorySwapMultiplier);
addSystemWideContainerSettings(workspaceId, isDev, service);
EndpointConfig endpointConfig = new EndpointConfig().withAliases(machineName).withLinks(toArrayIfNotNull(service.getLinks()));
NetworkingConfig networkingConfig = new NetworkingConfig().withEndpointsConfig(singletonMap(networkName, endpointConfig));
HostConfig hostConfig = new HostConfig();
hostConfig.withMemorySwap(machineMemorySwap).withMemory(service.getMemLimit()).withNetworkMode(networkName).withLinks(toArrayIfNotNull(service.getLinks())).withPortBindings(service.getPorts().stream().collect(toMap(Function.identity(), value -> new PortBinding[0]))).withVolumesFrom(toArrayIfNotNull(service.getVolumesFrom()));
ContainerConfig config = new ContainerConfig();
config.withImage(image).withExposedPorts(service.getExpose().stream().distinct().collect(toMap(Function.identity(), value -> emptyMap()))).withHostConfig(hostConfig).withCmd(toArrayIfNotNull(service.getCommand())).withEntrypoint(toArrayIfNotNull(service.getEntrypoint())).withLabels(service.getLabels()).withNetworkingConfig(networkingConfig).withEnv(service.getEnvironment().entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).toArray(String[]::new));
List<String> bindMountVolumes = new ArrayList<>();
Map<String, Volume> nonBindMountVolumes = new HashMap<>();
for (String volume : service.getVolumes()) {
// If volume contains colon then it is bind volume, otherwise - non bind-mount volume.
if (volume.contains(":")) {
bindMountVolumes.add(volume);
} else {
nonBindMountVolumes.put(volume, new Volume());
}
}
hostConfig.setBinds(bindMountVolumes.toArray(new String[bindMountVolumes.size()]));
config.setVolumes(nonBindMountVolumes);
addStaticDockerConfiguration(config);
return docker.createContainer(CreateContainerParams.create(config).withContainerName(service.getContainerName())).getId();
}
Aggregations