Search in sources :

Example 1 with SingularityDockerVolume

use of com.hubspot.mesos.SingularityDockerVolume 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());
}
Also used : SingularityRequestBuilder(com.hubspot.singularity.SingularityRequestBuilder) SingularityNetworkInfo(com.hubspot.mesos.SingularityNetworkInfo) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) NetworkInfo(org.apache.mesos.v1.Protos.NetworkInfo) SingularityNetworkInfo(com.hubspot.mesos.SingularityNetworkInfo) SingularityVolume(com.hubspot.mesos.SingularityVolume) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) SingularityDockerImage(com.hubspot.mesos.SingularityDockerImage) SingularityMesosImage(com.hubspot.mesos.SingularityMesosImage) SingularityDockerImage(com.hubspot.mesos.SingularityDockerImage) Image(org.apache.mesos.v1.Protos.Image) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) SingularityPortMapping(com.hubspot.mesos.SingularityPortMapping) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) Volume(org.apache.mesos.v1.Protos.Volume) SingularityVolume(com.hubspot.mesos.SingularityVolume) DockerVolume(org.apache.mesos.v1.Protos.Volume.Source.DockerVolume) Protos(org.apache.mesos.v1.Protos) Resources(com.hubspot.mesos.Resources) SingularityMesosInfo(com.hubspot.mesos.SingularityMesosInfo) PortMapping(org.apache.mesos.v1.Protos.ContainerInfo.DockerInfo.PortMapping) SingularityPortMapping(com.hubspot.mesos.SingularityPortMapping) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityMesosImage(com.hubspot.mesos.SingularityMesosImage) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) SingularityVolumeSource(com.hubspot.mesos.SingularityVolumeSource) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) DockerVolume(org.apache.mesos.v1.Protos.Volume.Source.DockerVolume) Test(org.junit.Test)

Example 2 with SingularityDockerVolume

use of com.hubspot.mesos.SingularityDockerVolume in project Singularity by HubSpot.

the class SingularityMesosTaskBuilder method prepareContainerInfo.

private void prepareContainerInfo(final SingularityOfferHolder offerHolder, final SingularityTaskId taskId, final TaskInfo.Builder bldr, final SingularityContainerInfo containerInfo, final Optional<long[]> ports) {
    ContainerInfo.Builder containerBuilder = ContainerInfo.newBuilder();
    containerBuilder.setType(ContainerInfo.Type.valueOf(containerInfo.getType().toString()));
    final Optional<SingularityDockerInfo> dockerInfo = containerInfo.getDocker();
    if (dockerInfo.isPresent()) {
        final DockerInfo.Builder dockerInfoBuilder = DockerInfo.newBuilder();
        dockerInfoBuilder.setImage(dockerInfo.get().getImage());
        if (dockerInfo.get().getNetwork().isPresent()) {
            dockerInfoBuilder.setNetwork(DockerInfo.Network.valueOf(dockerInfo.get().getNetwork().get().toString()));
        }
        final List<SingularityDockerPortMapping> portMappings = dockerInfo.get().getPortMappings();
        final boolean isBridged = SingularityDockerNetworkType.BRIDGE.equals(dockerInfo.get().getNetwork().orNull());
        if ((dockerInfo.get().hasAllLiteralHostPortMappings() || ports.isPresent()) && !portMappings.isEmpty()) {
            for (SingularityDockerPortMapping singularityDockerPortMapping : portMappings) {
                final Optional<DockerInfo.PortMapping> maybePortMapping = buildPortMapping(singularityDockerPortMapping, ports);
                if (maybePortMapping.isPresent()) {
                    dockerInfoBuilder.addPortMappings(maybePortMapping.get());
                }
            }
        } else if (configuration.getNetworkConfiguration().isDefaultPortMapping() && isBridged && portMappings.isEmpty() && ports.isPresent()) {
            for (long longPort : ports.get()) {
                int port = Ints.checkedCast(longPort);
                dockerInfoBuilder.addPortMappings(DockerInfo.PortMapping.newBuilder().setHostPort(port).setContainerPort(port).build());
            }
        }
        if (!dockerInfo.get().getDockerParameters().isEmpty()) {
            List<Parameter> parameters = new ArrayList<>();
            for (SingularityDockerParameter parameter : dockerInfo.get().getDockerParameters()) {
                parameters.add(Parameter.newBuilder().setKey(parameter.getKey()).setValue(parameter.getValue()).build());
            }
            dockerInfoBuilder.addAllParameters(parameters);
        }
        dockerInfoBuilder.setPrivileged(dockerInfo.get().isPrivileged());
        dockerInfoBuilder.setForcePullImage(dockerInfo.get().isForcePullImage());
        containerBuilder.setDocker(dockerInfoBuilder);
    }
    for (SingularityVolume volumeInfo : containerInfo.getVolumes().or(Collections.<SingularityVolume>emptyList())) {
        final Volume.Builder volumeBuilder = Volume.newBuilder();
        volumeBuilder.setContainerPath(fillInTaskIdValues(volumeInfo.getContainerPath(), offerHolder, taskId));
        if (volumeInfo.getHostPath().isPresent()) {
            volumeBuilder.setHostPath(fillInTaskIdValues(volumeInfo.getHostPath().get(), offerHolder, taskId));
        }
        if (volumeInfo.getMode().isPresent()) {
            volumeBuilder.setMode(Volume.Mode.valueOf(volumeInfo.getMode().get().toString()));
        } else {
            volumeBuilder.setMode(Volume.Mode.RO);
        }
        if (volumeInfo.getSource().isPresent()) {
            final Volume.Source.Builder sourceBuilder = Volume.Source.newBuilder();
            final SingularityVolumeSource source = volumeInfo.getSource().get();
            sourceBuilder.setType(Volume.Source.Type.valueOf(source.getType().toString()));
            if (source.getDockerVolume().isPresent()) {
                final Volume.Source.DockerVolume.Builder dockerVolumeBuilder = Volume.Source.DockerVolume.newBuilder();
                final SingularityDockerVolume dockerVolume = source.getDockerVolume().get();
                if (dockerVolume.getDriver().isPresent()) {
                    dockerVolumeBuilder.setDriver(dockerVolume.getDriver().get());
                }
                if (dockerVolume.getName().isPresent()) {
                    dockerVolumeBuilder.setName(dockerVolume.getName().get().replace("%i", Integer.toString(taskId.getInstanceNo())));
                }
                if (!dockerVolume.getDriverOptions().isEmpty()) {
                    final Parameters.Builder parameters = Parameters.newBuilder();
                    for (Entry<String, String> option : dockerVolume.getDriverOptions().entrySet()) {
                        parameters.addParameter(Parameter.newBuilder().setKey(option.getKey()).setValue(option.getValue()).build());
                    }
                    dockerVolumeBuilder.setDriverOptions(parameters.build());
                }
                sourceBuilder.setDockerVolume(dockerVolumeBuilder.build());
            }
            volumeBuilder.setSource(sourceBuilder.build());
        }
        containerBuilder.addVolumes(volumeBuilder);
    }
    prepareMesosInfo(containerBuilder, containerInfo);
    prepareNetworkInfos(containerBuilder, containerInfo, ports);
    bldr.setContainer(containerBuilder);
}
Also used : SingularityVolume(com.hubspot.mesos.SingularityVolume) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo) DockerInfo(org.apache.mesos.v1.Protos.ContainerInfo.DockerInfo) SingularityVolumeSource(com.hubspot.mesos.SingularityVolumeSource) SingularityPortMapping(com.hubspot.mesos.SingularityPortMapping) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) Parameters(org.apache.mesos.v1.Protos.Parameters) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) SingularityDockerParameter(com.hubspot.mesos.SingularityDockerParameter) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) Volume(org.apache.mesos.v1.Protos.Volume) SingularityVolume(com.hubspot.mesos.SingularityVolume) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) ContainerInfo(org.apache.mesos.v1.Protos.ContainerInfo) Parameter(org.apache.mesos.v1.Protos.Parameter) SingularityDockerParameter(com.hubspot.mesos.SingularityDockerParameter) SingularityVolumeSource(com.hubspot.mesos.SingularityVolumeSource) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume)

Aggregations

SingularityContainerInfo (com.hubspot.mesos.SingularityContainerInfo)2 SingularityDockerPortMapping (com.hubspot.mesos.SingularityDockerPortMapping)2 SingularityDockerVolume (com.hubspot.mesos.SingularityDockerVolume)2 SingularityPortMapping (com.hubspot.mesos.SingularityPortMapping)2 SingularityVolume (com.hubspot.mesos.SingularityVolume)2 SingularityVolumeSource (com.hubspot.mesos.SingularityVolumeSource)2 Volume (org.apache.mesos.v1.Protos.Volume)2 ByteString (com.google.protobuf.ByteString)1 Resources (com.hubspot.mesos.Resources)1 SingularityDockerImage (com.hubspot.mesos.SingularityDockerImage)1 SingularityDockerInfo (com.hubspot.mesos.SingularityDockerInfo)1 SingularityDockerParameter (com.hubspot.mesos.SingularityDockerParameter)1 SingularityMesosImage (com.hubspot.mesos.SingularityMesosImage)1 SingularityMesosInfo (com.hubspot.mesos.SingularityMesosInfo)1 SingularityNetworkInfo (com.hubspot.mesos.SingularityNetworkInfo)1 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)1 SingularityDeployBuilder (com.hubspot.singularity.SingularityDeployBuilder)1 SingularityRequest (com.hubspot.singularity.SingularityRequest)1 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)1 SingularityTaskRequest (com.hubspot.singularity.SingularityTaskRequest)1