Search in sources :

Example 6 with PortMapping

use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.

the class TaskConfig method portBindings.

/**
   * Create a port binding configuration for the job.
   * @return The port bindings.
   */
private Map<String, List<PortBinding>> portBindings() {
    final Map<String, List<PortBinding>> bindings = Maps.newHashMap();
    for (final Map.Entry<String, PortMapping> e : job.getPorts().entrySet()) {
        final PortMapping mapping = e.getValue();
        final Integer jobDefinedExtPort = mapping.getExternalPort();
        // If the job didn't specify an external port, use dynamically allocated ports
        final String externalPort = jobDefinedExtPort == null ? ports.get(e.getKey()).toString() : jobDefinedExtPort.toString();
        final PortBinding binding = PortBinding.of(mapping.getIp(), externalPort);
        final String entry = containerPort(mapping.getInternalPort(), mapping.getProtocol());
        bindings.put(entry, Collections.singletonList(binding));
    }
    return bindings;
}
Also used : PortBinding(com.spotify.docker.client.messages.PortBinding) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with PortMapping

use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.

the class TaskConfig method containerExposedPorts.

/**
   * Create container port exposure configuration for a job.
   * @return The exposed ports.
   */
private Set<String> containerExposedPorts() {
    final Set<String> ports = Sets.newHashSet();
    for (final Map.Entry<String, PortMapping> entry : job.getPorts().entrySet()) {
        final PortMapping mapping = entry.getValue();
        ports.add(containerPort(mapping.getInternalPort(), mapping.getProtocol()));
    }
    return ports;
}
Also used : PortMapping(com.spotify.helios.common.descriptors.PortMapping) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with PortMapping

use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.

the class PortMappingParserTest method testParsePortMappingGoodSpecs.

@Test
public void testParsePortMappingGoodSpecs() throws Exception {
    for (final TestData d : GOOD_SPECS) {
        final PortMappingWithName mappingWithName = PortMappingParser.parsePortMapping(d.getSpec());
        final PortMapping portMapping = mappingWithName.portMapping();
        assertThat(mappingWithName.name(), equalTo(d.getName()));
        assertThat(portMapping.getInternalPort(), equalTo(d.getPortMapping().getInternalPort()));
        assertThat(portMapping.getExternalPort(), equalTo(d.getPortMapping().getExternalPort()));
        assertThat(portMapping.getProtocol(), equalTo(d.getPortMapping().getProtocol()));
    }
}
Also used : PortMappingWithName(com.spotify.helios.cli.command.PortMappingParser.PortMappingWithName) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Test(org.junit.Test)

Example 9 with PortMapping

use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.

the class JobValidator method validate.

public Set<String> validate(final Job job) {
    final Set<String> errors = Sets.newHashSet();
    errors.addAll(validateJobId(job));
    errors.addAll(validateJobImage(job.getImage()));
    errors.addAll(validateJobHostName(job.getHostname()));
    // Check that there's not external port collision
    final Set<Integer> externalPorts = Sets.newHashSet();
    for (final PortMapping mapping : job.getPorts().values()) {
        final Integer externalMappedPort = mapping.getExternalPort();
        if (externalPorts.contains(externalMappedPort) && externalMappedPort != null) {
            errors.add(format("Duplicate external port mapping: %s", externalMappedPort));
        }
        externalPorts.add(externalMappedPort);
    }
    // Verify port mappings
    for (final Map.Entry<String, PortMapping> entry : job.getPorts().entrySet()) {
        final String name = entry.getKey();
        final PortMapping mapping = entry.getValue();
        if (!PORT_MAPPING_PROTO_PATTERN.matcher(mapping.getProtocol()).matches()) {
            errors.add(format("Invalid port mapping protocol: %s", mapping.getProtocol()));
        }
        if (!legalPort(mapping.getInternalPort())) {
            errors.add(format("Invalid internal port: %d", mapping.getInternalPort()));
        }
        if (mapping.getExternalPort() != null && !legalPort(mapping.getExternalPort())) {
            errors.add(format("Invalid external port: %d", mapping.getExternalPort()));
        }
        if (!PORT_MAPPING_NAME_PATTERN.matcher(name).matches()) {
            errors.add(format("Invalid port mapping endpoint name: %s", name));
        }
    }
    // Verify service registrations
    for (final ServiceEndpoint registration : job.getRegistration().keySet()) {
        final ServicePorts servicePorts = job.getRegistration().get(registration);
        if (servicePorts == null || servicePorts.getPorts() == null) {
            errors.add(format("registration for '%s' is malformed: does not have a port mapping", registration.getName()));
            continue;
        }
        for (final String portName : servicePorts.getPorts().keySet()) {
            if (!job.getPorts().containsKey(portName)) {
                errors.add(format("Service registration refers to missing port mapping: %s=%s", registration, portName));
            }
            if (!REGISTRATION_NAME_PATTERN.matcher(registration.getName()).matches()) {
                errors.add(format("Invalid service registration name: %s", registration.getName()));
            }
        }
    }
    // Validate volumes
    for (final Map.Entry<String, String> entry : job.getVolumes().entrySet()) {
        final String path = entry.getKey();
        final String source = entry.getValue();
        if (!path.startsWith("/")) {
            errors.add("Volume path is not absolute: " + path);
            continue;
        }
        if (!isNullOrEmpty(source) && !source.startsWith("/")) {
            errors.add("Volume source is not absolute: " + source);
            continue;
        }
        final String[] parts = path.split(":", 3);
        if (path.isEmpty() || path.equals("/") | parts.length > 2 || (parts.length > 1 && parts[1].isEmpty())) {
            errors.add(format("Invalid volume path: %s", path));
        }
    }
    // Validate Expiry
    final Date expiry = job.getExpires();
    if (expiry != null && expiry.before(new Date())) {
        errors.add("Job expires in the past");
    }
    errors.addAll(validateJobHealthCheck(job));
    errors.addAll(validateJobNetworkMode(job));
    if (shouldValidateAddCapabilities) {
        errors.addAll(validateAddCapabilities(job));
    }
    return errors;
}
Also used : ServicePorts(com.spotify.helios.common.descriptors.ServicePorts) PortMapping(com.spotify.helios.common.descriptors.PortMapping) Map(java.util.Map) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Date(java.util.Date)

Example 10 with PortMapping

use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.

the class TemporaryJob method port.

/**
   * Returns the port that a job can be reached at given the host and name of registered port.
   * This is useful to discover the value of a dynamically allocated port.
   * @param host the host where the job is deployed
   * @param port the name of the registered port
   * @return the port where the job can be reached, or null if the host or port name is not found
   */
public Integer port(final String host, final String port) {
    checkArgument(hosts.contains(host), "host %s not found", host);
    checkArgument(job.getPorts().containsKey(port), "port %s not found", port);
    final TaskStatus status = statuses.get(host);
    if (status == null) {
        return null;
    }
    final PortMapping portMapping = status.getPorts().get(port);
    if (portMapping == null) {
        return null;
    }
    return portMapping.getExternalPort();
}
Also used : PortMapping(com.spotify.helios.common.descriptors.PortMapping) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus)

Aggregations

PortMapping (com.spotify.helios.common.descriptors.PortMapping)21 Test (org.junit.Test)11 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)9 ServicePorts (com.spotify.helios.common.descriptors.ServicePorts)9 Job (com.spotify.helios.common.descriptors.Job)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 JobId (com.spotify.helios.common.descriptors.JobId)7 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)6 HeliosClient (com.spotify.helios.client.HeliosClient)5 File (java.io.File)3 DockerClient (com.spotify.docker.client.DockerClient)2 HostStatus (com.spotify.helios.common.descriptors.HostStatus)2 JobStatus (com.spotify.helios.common.descriptors.JobStatus)2 ServicePortParameters (com.spotify.helios.common.descriptors.ServicePortParameters)2 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)2 ServiceRegistration (com.spotify.helios.serviceregistration.ServiceRegistration)2 FileOutputStream (java.io.FileOutputStream)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ImmutableList (com.google.common.collect.ImmutableList)1