use of com.hubspot.mesos.SingularityPortMapping in project Singularity by HubSpot.
the class SingularityMesosTaskBuilderTest method testMesosContainer.
@Test
public void testMesosContainer() {
taskResources = new Resources(1, 1, 2);
final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build();
final SingularityContainerInfo containerInfo = new SingularityContainerInfo(SingularityContainerType.MESOS, Optional.of(Collections.singletonList(new SingularityVolume("/testing", Optional.of("/host"), SingularityDockerVolumeMode.RW, Optional.of(new SingularityVolumeSource(SingularityVolumeSourceType.DOCKER_VOLUME, Optional.of(new SingularityDockerVolume(Optional.of("rexray"), Optional.of("testvolume-%i"), Collections.singletonMap("iops", "1")))))))), Optional.absent(), Optional.of(new SingularityMesosInfo(Optional.of(new SingularityMesosImage(SingularityMesosImageType.DOCKER, Optional.absent(), Optional.of(new SingularityDockerImage("test:image")), true)))), Optional.of(Arrays.asList(new SingularityNetworkInfo(Optional.of("network-name"), Optional.of(Arrays.asList("blue", "purple")), Optional.of(Arrays.asList(new SingularityPortMapping(0, 8080, Optional.of("tcp")), new SingularityPortMapping(8888, 8081, Optional.of("udp"))))))));
final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1").setContainerInfo(Optional.of(containerInfo)).build();
final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask);
final SingularityMesosTaskHolder task = builder.buildTask(offerHolder, Collections.singletonList(MesosUtils.getPortRangeResource(31010, 31011)), taskRequest, taskResources, executorResources);
assertEquals(Type.MESOS, task.getMesosTask().getContainer().getType());
final Image image = task.getMesosTask().getContainer().getMesos().getImage();
assertEquals(Protos.Image.Type.DOCKER, image.getType());
assertEquals("test:image", image.getDocker().getName());
final Volume volume = task.getMesosTask().getContainer().getVolumesList().get(0);
assertEquals("/testing", volume.getContainerPath());
assertEquals("/host", volume.getHostPath());
assertEquals(Volume.Mode.RW, volume.getMode());
assertEquals(Volume.Source.Type.DOCKER_VOLUME, volume.getSource().getType());
final DockerVolume dockerVolume = volume.getSource().getDockerVolume();
assertEquals("rexray", dockerVolume.getDriver());
assertEquals("testvolume-1", dockerVolume.getName());
assertEquals("iops", dockerVolume.getDriverOptions().getParameterList().get(0).getKey());
final NetworkInfo networkInfo = task.getMesosTask().getContainer().getNetworkInfosList().get(0);
assertEquals("network-name", networkInfo.getName());
assertEquals(Arrays.asList("blue", "purple"), networkInfo.getGroupsList());
final List<Protos.NetworkInfo.PortMapping> portMappings = networkInfo.getPortMappingsList();
assertEquals(2, portMappings.size());
assertEquals(31010, portMappings.get(0).getHostPort());
assertEquals(8080, portMappings.get(0).getContainerPort());
assertEquals("tcp", portMappings.get(0).getProtocol());
assertEquals(8888, portMappings.get(1).getHostPort());
assertEquals(8081, portMappings.get(1).getContainerPort());
assertEquals("udp", portMappings.get(1).getProtocol());
}
use of com.hubspot.mesos.SingularityPortMapping in project Singularity by HubSpot.
the class SingularityMesosTaskBuilder method prepareNetworkInfos.
private void prepareNetworkInfos(ContainerInfo.Builder containerBuilder, final SingularityContainerInfo containerInfo, final Optional<long[]> ports) {
for (SingularityNetworkInfo netInfo : containerInfo.getNetworkInfos().or(Collections.emptyList())) {
final NetworkInfo.Builder netBuilder = NetworkInfo.newBuilder();
if (netInfo.getName().isPresent()) {
netBuilder.setName(netInfo.getName().get());
}
for (String group : netInfo.getGroups().or(Collections.emptyList())) {
netBuilder.addGroups(group);
}
for (SingularityPortMapping mapping : netInfo.getPortMappings().or(defaultPortMappingFor(ports))) {
final NetworkInfo.PortMapping.Builder portBuilder = NetworkInfo.PortMapping.newBuilder();
final int hostPort = mapping.getHostPort();
final int containerPort = mapping.getContainerPort();
final long[] offerPorts = ports.or(new long[0]);
portBuilder.setHostPort(hostPort < offerPorts.length ? (int) offerPorts[hostPort] : hostPort);
portBuilder.setContainerPort(containerPort < offerPorts.length ? (int) offerPorts[containerPort] : containerPort);
if (mapping.getProtocol().isPresent()) {
portBuilder.setProtocol(mapping.getProtocol().get());
}
netBuilder.addPortMappings(portBuilder.build());
}
containerBuilder.addNetworkInfos(netBuilder.build());
}
}
use of com.hubspot.mesos.SingularityPortMapping in project Singularity by HubSpot.
the class SingularityMesosTaskBuilder method defaultPortMappingFor.
private Supplier<List<SingularityPortMapping>> defaultPortMappingFor(Optional<long[]> ports) {
return new Supplier<List<SingularityPortMapping>>() {
@Override
public List<SingularityPortMapping> get() {
final long[] portArray = ports.or(new long[0]);
final List<SingularityPortMapping> mappings = new ArrayList<>(portArray.length);
for (long port : portArray) {
final int p = (int) port;
mappings.add(new SingularityPortMapping(p, p, Optional.of("tcp")));
mappings.add(new SingularityPortMapping(p, p, Optional.of("udp")));
}
return mappings;
}
};
}
Aggregations