use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.
the class TaskConfig method registration.
public ServiceRegistration registration() throws InterruptedException {
final ServiceRegistration.Builder builder = ServiceRegistration.newBuilder();
for (final Map.Entry<ServiceEndpoint, ServicePorts> entry : job.getRegistration().entrySet()) {
final ServiceEndpoint registration = entry.getKey();
final ServicePorts servicePorts = entry.getValue();
for (final Entry<String, ServicePortParameters> portEntry : servicePorts.getPorts().entrySet()) {
final String portName = portEntry.getKey();
final ServicePortParameters portParameters = portEntry.getValue();
final PortMapping mapping = job.getPorts().get(portName);
if (mapping == null) {
log.error("no '{}' port mapped for registration: '{}'", portName, registration);
continue;
}
final Integer externalPort;
if (mapping.getExternalPort() != null) {
// Use the statically assigned port if one is specified
externalPort = mapping.getExternalPort();
} else {
// Otherwise use the dynamically allocated port
externalPort = ports.get(portName);
}
if (externalPort == null) {
log.error("no external '{}' port for registration: '{}'", portName, registration);
continue;
}
builder.endpoint(registration.getName(), registration.getProtocol(), externalPort, fullyQualifiedRegistrationDomain(), host, portParameters.getTags(), endpointHealthCheck(portName));
}
}
return builder.build();
}
use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.
the class FlappingTest method test.
@Test
public void test() throws Exception {
// CircleCI boxes are too slow -- the job doesn't stop or restart fast enough to ever flap
assumeFalse(isCircleCi());
startDefaultMaster();
final String host = testHost();
startDefaultAgent(host);
final HeliosClient client = defaultClient();
awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
final Job flapper = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("nc", "-p", "4711", "-l")).addPort("poke", PortMapping.of(4711)).build();
final JobId jobId = createJob(flapper);
deployJob(jobId, host);
awaitTaskState(jobId, host, RUNNING);
// Poke the container to make it exit until it's classified as flapping
Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
final JobStatus jobStatus = getOrNull(client.jobStatus(jobId));
final TaskStatus taskStatus = jobStatus.getTaskStatuses().get(host);
if (taskStatus.getThrottled() == FLAPPING) {
return true;
}
final PortMapping port = taskStatus.getPorts().get("poke");
assert port.getExternalPort() != null;
poke(port.getExternalPort());
return null;
}
});
// Verify that the job recovers after we stop poking
awaitJobThrottle(client, host, jobId, NO, LONG_WAIT_SECONDS, SECONDS);
}
use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.
the class CliDeploymentTest method testDeployAndUndeployJob.
@Test
public void testDeployAndUndeployJob() throws Exception {
startDefaultMaster();
// Wait for master to come up
Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<String>() {
@Override
public String call() throws Exception {
final String output = cli("masters");
return output.contains(masterName()) ? output : null;
}
});
startDefaultAgent(testHost());
final String image = BUSYBOX;
final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
final Map<String, String> env = ImmutableMap.of("BAD", "f00d");
// Wait for agent to come up
awaitHostRegistered(testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Create job
final JobId jobId = createJob(testJobName, testJobVersion, image, IDLE_COMMAND, env, ports, registration);
// Query for job
final Job expected = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(image).setCommand(IDLE_COMMAND).setEnv(env).setPorts(ports).setRegistration(registration).setCreatingUser(TEST_USER).build();
final String inspectOutput = cli("inspect", "--json", jobId.toString());
final Job parsed = Json.read(inspectOutput, Job.class);
assertJobEquals(expected, parsed);
assertThat(cli("jobs", testJobName, "-q"), containsString(jobId.toString()));
assertThat(cli("jobs", testJobName + ":" + testJobVersion, "-q"), containsString(jobId.toString()));
assertEquals("job pattern foozbarz matched no jobs", cli("jobs", "foozbarz").trim());
assertTrue(cli("jobs", "foozbarz", "-q").isEmpty());
// Create a new job using the first job as a template
final Job expectedCloned = expected.toBuilder().setVersion(expected.getId().getVersion() + "-cloned").build();
final JobId clonedJobId = JobId.parse(WHITESPACE.trimFrom(cli("create", "-q", "-t", testJobName + ":" + testJobVersion, testJobName + ":" + testJobVersion + "-cloned")));
final String clonedInspectOutput = cli("inspect", "--json", clonedJobId.toString());
final Job clonedParsed = Json.read(clonedInspectOutput, Job.class);
assertJobEquals(expectedCloned, clonedParsed);
// Verify that port mapping and environment variables are correct
final String statusString = cli("status", "--job", jobId.toString(), "--json");
final Map<JobId, JobStatus> statuses = Json.read(statusString, STATUSES_TYPE);
final Job job = statuses.get(jobId).getJob();
assertEquals(ServicePorts.of("foo"), job.getRegistration().get(ServiceEndpoint.of("foo-service", "tcp")));
assertEquals(ServicePorts.of("bar"), job.getRegistration().get(ServiceEndpoint.of("bar-service", "http")));
assertEquals(4711, job.getPorts().get("foo").getInternalPort());
assertEquals(PortMapping.of(5000, externalPort), job.getPorts().get("bar"));
assertEquals("f00d", job.getEnv().get("BAD"));
final String duplicateJob = cli("create", testJobName + ":" + testJobVersion, image, "--", IDLE_COMMAND);
assertThat(duplicateJob, containsString("JOB_ALREADY_EXISTS"));
final String prestop = stopJob(jobId, testHost());
assertThat(prestop, containsString("JOB_NOT_DEPLOYED"));
// Deploy job
deployJob(jobId, testHost());
// Stop job
final String stop1 = stopJob(jobId, BOGUS_HOST);
assertThat(stop1, containsString("HOST_NOT_FOUND"));
final String stop2 = stopJob(BOGUS_JOB, testHost());
assertThat(stop2, containsString("Unknown job"));
final String stop3 = stopJob(jobId, testHost());
assertThat(stop3, containsString(testHost() + ": done"));
// Verify that undeploying the job from a nonexistent host fails
assertThat(cli("undeploy", jobId.toString(), BOGUS_HOST), containsString("HOST_NOT_FOUND"));
// Verify that undeploying a nonexistent job from the host fails
assertThat(cli("undeploy", BOGUS_JOB.toString(), testHost()), containsString("Unknown job"));
// Undeploy job
undeployJob(jobId, testHost());
}
use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.
the class CliJobCreationTest method testMergeFileAndCliArgsEnvPrecedence.
@Test
public void testMergeFileAndCliArgsEnvPrecedence() throws Exception {
final String redundantEnvKey = "BAD";
final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
final Map<String, String> env = ImmutableMap.of(redundantEnvKey, "f00d");
final Map<String, String> volumes = Maps.newHashMap();
volumes.put("/etc/spotify/secret-keys.yaml:ro", "/etc/spotify/secret-keys.yaml");
// Create a new job, serialize it to JSON
final Job.Builder builder = Job.newBuilder().setCommand(Lists.newArrayList("server", "foo-service.yaml")).setEnv(env).setPorts(ports).setRegistration(registration).setVolumes(volumes).setCreatingUser(TEST_USER);
final Job job = builder.build();
final String jobConfigJsonString = job.toJsonString();
// Create temporary job config file
final File file = temporaryFolder.newFile();
final String absolutePath = file.getAbsolutePath();
// Write JSON config to temp file
try (final FileOutputStream outFile = new FileOutputStream(file)) {
outFile.write(Charsets.UTF_8.encode(jobConfigJsonString).array());
// Create job and specify the temp file.
cli("create", "--file", absolutePath, testJobNameAndVersion, BUSYBOX, "--env", redundantEnvKey + "=FOOD");
// Inspect the job.
final String actualJobConfigJson = cli("inspect", testJobNameAndVersion, "--json");
// Compare to make sure the created job has the expected configuration,
// i.e. the configuration resulting from a merge of the JSON file and CLI args.
final Job actualJob = Json.read(actualJobConfigJson, Job.class);
final Job.Builder actualJobBuilder = actualJob.toBuilder();
builder.setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setEnv(ImmutableMap.of(redundantEnvKey, "FOOD"));
assertJobEquals(builder.build(), actualJobBuilder.build());
}
}
use of com.spotify.helios.common.descriptors.PortMapping in project helios by spotify.
the class ConfigFileJobCreationTest method test.
@Test
public void test() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
final String name = testJobName;
final String version = "17";
final String image = BUSYBOX;
final Map<String, PortMapping> ports = ImmutableMap.of("foo", PortMapping.of(4711), "bar", PortMapping.of(5000, externalPort));
final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"), ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
final Map<String, String> env = ImmutableMap.of("BAD", "f00d");
final Map<String, Object> configuration = ImmutableMap.of("id", name + ":" + version, "image", image, "ports", ports, "registration", registration, "env", env);
final Path file = temporaryFolder.newFile().toPath();
Files.write(file, Json.asBytes(configuration));
final String output = cli("create", "-q", "-f", file.toAbsolutePath().toString());
final JobId jobId = JobId.parse(WHITESPACE.trimFrom(output));
final Map<JobId, Job> jobs = client.jobs().get();
final Job job = jobs.get(jobId);
assertEquals(name, job.getId().getName());
assertEquals(version, job.getId().getVersion());
assertEquals(ports, job.getPorts());
assertEquals(env, job.getEnv());
assertEquals(registration, job.getRegistration());
}
Aggregations