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;
}
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;
}
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()));
}
}
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;
}
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();
}
Aggregations