Search in sources :

Example 1 with SingularityDockerPortMapping

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

the class SingularityValidator method checkDocker.

private void checkDocker(SingularityDeploy deploy) {
    if (deploy.getResources().isPresent() && deploy.getContainerInfo().get().getDocker().isPresent()) {
        final SingularityDockerInfo dockerInfo = deploy.getContainerInfo().get().getDocker().get();
        final int numPorts = deploy.getResources().get().getNumPorts();
        checkBadRequest(dockerInfo.getImage() != null, "docker image may not be null");
        for (SingularityDockerPortMapping portMapping : dockerInfo.getPortMappings()) {
            if (portMapping.getContainerPortType() == SingularityPortMappingType.FROM_OFFER) {
                checkBadRequest(portMapping.getContainerPort() >= 0 && portMapping.getContainerPort() < numPorts, "Index of port resource for containerPort must be between 0 and %d (inclusive)", numPorts - 1);
            }
            if (portMapping.getHostPortType() == SingularityPortMappingType.FROM_OFFER) {
                checkBadRequest(portMapping.getHostPort() >= 0 && portMapping.getHostPort() < numPorts, "Index of port resource for hostPort must be between 0 and %d (inclusive)", numPorts - 1);
            }
        }
    }
}
Also used : SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo)

Example 2 with SingularityDockerPortMapping

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

the class SingularitySchedulerTest method dockerDeployWithPorts.

private SingularityDeployBuilder dockerDeployWithPorts() {
    final SingularityDockerPortMapping literalMapping = new SingularityDockerPortMapping(Optional.<SingularityPortMappingType>absent(), 80, Optional.of(SingularityPortMappingType.LITERAL), 8080, Optional.<String>absent());
    final SingularityDockerPortMapping offerMapping = new SingularityDockerPortMapping(Optional.<SingularityPortMappingType>absent(), 81, Optional.of(SingularityPortMappingType.FROM_OFFER), 0, Optional.of("udp"));
    final SingularityContainerInfo containerInfo = new SingularityContainerInfo(SingularityContainerType.DOCKER, Optional.<List<SingularityVolume>>absent(), Optional.of(new SingularityDockerInfo("docker-image", true, SingularityDockerNetworkType.BRIDGE, Optional.of(Arrays.asList(literalMapping, offerMapping)), Optional.of(false), Optional.of(ImmutableMap.of("env", "var=value")), Optional.absent())));
    final SingularityDeployBuilder deployBuilder = new SingularityDeployBuilder(requestId, "test-docker-ports-deploy");
    deployBuilder.setContainerInfo(Optional.of(containerInfo));
    return deployBuilder;
}
Also used : SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) SingularityVolume(com.hubspot.mesos.SingularityVolume) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo)

Example 3 with SingularityDockerPortMapping

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

the class SingularityMesosTaskBuilderTest method testDockerTask.

@Test
public void testDockerTask() {
    taskResources = new Resources(1, 1, 1, 0);
    final Protos.Resource portsResource = Protos.Resource.newBuilder().setName("ports").setType(Protos.Value.Type.RANGES).setRanges(Protos.Value.Ranges.newBuilder().addRange(Protos.Value.Range.newBuilder().setBegin(31000).setEnd(31000).build()).build()).build();
    final SingularityDockerPortMapping literalMapping = new SingularityDockerPortMapping(Optional.<SingularityPortMappingType>absent(), 80, Optional.of(SingularityPortMappingType.LITERAL), 8080, Optional.<String>absent());
    final SingularityDockerPortMapping offerMapping = new SingularityDockerPortMapping(Optional.<SingularityPortMappingType>absent(), 81, Optional.of(SingularityPortMappingType.FROM_OFFER), 0, Optional.of("udp"));
    final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build();
    final SingularityContainerInfo containerInfo = new SingularityContainerInfo(SingularityContainerType.DOCKER, Optional.of(Arrays.asList(new SingularityVolume("/container", Optional.of("/host"), SingularityDockerVolumeMode.RW), new SingularityVolume("/container/${TASK_REQUEST_ID}/${TASK_DEPLOY_ID}", Optional.of("/host/${TASK_ID}"), SingularityDockerVolumeMode.RO))), Optional.of(new SingularityDockerInfo("docker-image", true, SingularityDockerNetworkType.BRIDGE, Optional.of(Arrays.asList(literalMapping, offerMapping)), Optional.of(false), Optional.<Map<String, String>>of(ImmutableMap.of("env", "var=value")), Optional.absent())));
    final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1").setContainerInfo(Optional.of(containerInfo)).setCommand(Optional.of("/bin/echo")).setArguments(Optional.of(Collections.singletonList("wat"))).build();
    final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask);
    final SingularityMesosTaskHolder task = builder.buildTask(offerHolder, Collections.singletonList(portsResource), taskRequest, taskResources, executorResources);
    assertEquals("/bin/echo", task.getMesosTask().getCommand().getValue());
    assertEquals(1, task.getMesosTask().getCommand().getArgumentsCount());
    assertEquals("wat", task.getMesosTask().getCommand().getArguments(0));
    assertFalse(task.getMesosTask().getCommand().getShell());
    assertEquals(Type.DOCKER, task.getMesosTask().getContainer().getType());
    assertEquals("docker-image", task.getMesosTask().getContainer().getDocker().getImage());
    assertTrue(task.getMesosTask().getContainer().getDocker().getPrivileged());
    assertEquals("/container", task.getMesosTask().getContainer().getVolumes(0).getContainerPath());
    assertEquals("/host", task.getMesosTask().getContainer().getVolumes(0).getHostPath());
    assertEquals(Mode.RW, task.getMesosTask().getContainer().getVolumes(0).getMode());
    Parameter envParameter = Parameter.newBuilder().setKey("env").setValue("var=value").build();
    assertTrue(task.getMesosTask().getContainer().getDocker().getParametersList().contains(envParameter));
    assertEquals(String.format("/container/%s/%s", task.getTask().getTaskRequest().getDeploy().getRequestId(), task.getTask().getTaskRequest().getDeploy().getId()), task.getMesosTask().getContainer().getVolumes(1).getContainerPath());
    assertEquals(String.format("/host/%s", task.getMesosTask().getTaskId().getValue()), task.getMesosTask().getContainer().getVolumes(1).getHostPath());
    assertEquals(Mode.RO, task.getMesosTask().getContainer().getVolumes(1).getMode());
    assertEquals(80, task.getMesosTask().getContainer().getDocker().getPortMappings(0).getContainerPort());
    assertEquals(8080, task.getMesosTask().getContainer().getDocker().getPortMappings(0).getHostPort());
    assertEquals("tcp", task.getMesosTask().getContainer().getDocker().getPortMappings(0).getProtocol());
    assertTrue(MesosUtils.getAllPorts(task.getMesosTask().getResourcesList()).contains(8080L));
    assertEquals(81, task.getMesosTask().getContainer().getDocker().getPortMappings(1).getContainerPort());
    assertEquals(31000, task.getMesosTask().getContainer().getDocker().getPortMappings(1).getHostPort());
    assertEquals("udp", task.getMesosTask().getContainer().getDocker().getPortMappings(1).getProtocol());
    assertEquals(Protos.ContainerInfo.DockerInfo.Network.BRIDGE, task.getMesosTask().getContainer().getDocker().getNetwork());
}
Also used : SingularityRequestBuilder(com.hubspot.singularity.SingularityRequestBuilder) SingularityVolume(com.hubspot.mesos.SingularityVolume) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) Protos(org.apache.mesos.v1.Protos) Parameter(org.apache.mesos.v1.Protos.Parameter) Resources(com.hubspot.mesos.Resources) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) Test(org.junit.Test)

Example 4 with SingularityDockerPortMapping

use of com.hubspot.mesos.SingularityDockerPortMapping 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

SingularityDockerInfo (com.hubspot.mesos.SingularityDockerInfo)4 SingularityDockerPortMapping (com.hubspot.mesos.SingularityDockerPortMapping)4 SingularityContainerInfo (com.hubspot.mesos.SingularityContainerInfo)3 SingularityVolume (com.hubspot.mesos.SingularityVolume)3 SingularityDeployBuilder (com.hubspot.singularity.SingularityDeployBuilder)2 Parameter (org.apache.mesos.v1.Protos.Parameter)2 ByteString (com.google.protobuf.ByteString)1 Resources (com.hubspot.mesos.Resources)1 SingularityDockerParameter (com.hubspot.mesos.SingularityDockerParameter)1 SingularityDockerVolume (com.hubspot.mesos.SingularityDockerVolume)1 SingularityPortMapping (com.hubspot.mesos.SingularityPortMapping)1 SingularityVolumeSource (com.hubspot.mesos.SingularityVolumeSource)1 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)1 SingularityRequest (com.hubspot.singularity.SingularityRequest)1 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)1 SingularityTaskRequest (com.hubspot.singularity.SingularityTaskRequest)1 SingularityMesosTaskHolder (com.hubspot.singularity.helpers.SingularityMesosTaskHolder)1 ArrayList (java.util.ArrayList)1 Protos (org.apache.mesos.v1.Protos)1 ContainerInfo (org.apache.mesos.v1.Protos.ContainerInfo)1