use of com.mesosphere.sdk.offer.taskdata.TaskLabelWriter in project dcos-commons by mesosphere.
the class PodInfoBuilder method createTaskInfo.
private Protos.TaskInfo.Builder createTaskInfo(PodInstance podInstance, TaskSpec taskSpec, Map<String, String> environment, String serviceName, UUID targetConfigurationId, ArtifactQueries.TemplateUrlFactory templateUrlFactory, SchedulerConfig schedulerConfig, GoalStateOverride override) throws InvalidRequirementException {
if (override == null) {
override = GoalStateOverride.NONE;
}
PodSpec podSpec = podInstance.getPod();
Protos.TaskInfo.Builder taskInfoBuilder = Protos.TaskInfo.newBuilder().setName(TaskSpec.getInstanceName(podInstance, taskSpec)).setTaskId(CommonIdUtils.emptyTaskId()).setSlaveId(CommonIdUtils.emptyAgentId());
// create default labels:
taskInfoBuilder.setLabels(new TaskLabelWriter(taskInfoBuilder).setTargetConfiguration(targetConfigurationId).setGoalState(taskSpec.getGoal()).setType(podInstance.getPod().getType()).setIndex(podInstance.getIndex()).toProto());
if (taskSpec.getCommand().isPresent()) {
Protos.CommandInfo.Builder commandBuilder = taskInfoBuilder.getCommandBuilder();
commandBuilder.setEnvironment(EnvUtils.toProto(getTaskEnvironment(serviceName, podInstance, taskSpec, schedulerConfig)));
if (override.equals(GoalStateOverride.PAUSED)) {
LOGGER.info("Overriding task command: {}", override);
commandBuilder.setValue(schedulerConfig.getPauseOverrideCmd());
} else {
commandBuilder.setValue(taskSpec.getCommand().get().getValue());
}
setBootstrapConfigFileEnv(taskInfoBuilder.getCommandBuilder(), taskSpec);
extendEnv(taskInfoBuilder.getCommandBuilder(), environment);
// Always add the bootstrap URI as the paused command depends on it
if (override.equals(GoalStateOverride.PAUSED)) {
commandBuilder.addUrisBuilder().setValue(schedulerConfig.getBootstrapURI());
}
if (useDefaultExecutor) {
// Any URIs defined in PodSpec itself.
for (URI uri : podSpec.getUris()) {
commandBuilder.addUrisBuilder().setValue(uri.toString());
}
for (ConfigFileSpec config : taskSpec.getConfigFiles()) {
commandBuilder.addUrisBuilder().setValue(templateUrlFactory.get(targetConfigurationId, podSpec.getType(), taskSpec.getName(), config.getName())).setOutputFile(getConfigTemplateDownloadPath(config)).setExtract(false);
}
// Secrets are constructed differently from other envvars where the proto is concerned:
for (SecretSpec secretSpec : podInstance.getPod().getSecrets()) {
if (secretSpec.getEnvKey().isPresent()) {
commandBuilder.getEnvironmentBuilder().addVariablesBuilder().setName(secretSpec.getEnvKey().get()).setType(Protos.Environment.Variable.Type.SECRET).setSecret(getReferenceSecret(secretSpec.getSecretPath()));
}
}
if (podSpec.getUser().isPresent()) {
commandBuilder.setUser(podSpec.getUser().get());
}
}
}
if (taskSpec.getDiscovery().isPresent()) {
taskInfoBuilder.setDiscovery(getDiscoveryInfo(taskSpec.getDiscovery().get(), podInstance.getIndex()));
}
if (useDefaultExecutor) {
taskInfoBuilder.setContainer(getContainerInfo(podInstance.getPod(), true, true));
} else if (!podInstance.getPod().getNetworks().isEmpty()) {
taskInfoBuilder.setContainer(getContainerInfo(podInstance.getPod(), false, true));
} else {
taskInfoBuilder.setContainer(Protos.ContainerInfo.newBuilder().setType(Protos.ContainerInfo.Type.MESOS));
}
setHealthCheck(taskInfoBuilder, serviceName, podInstance, taskSpec, override, schedulerConfig);
setReadinessCheck(taskInfoBuilder, serviceName, podInstance, taskSpec, override, schedulerConfig);
setTaskKillGracePeriod(taskInfoBuilder, taskSpec);
return taskInfoBuilder;
}
use of com.mesosphere.sdk.offer.taskdata.TaskLabelWriter in project dcos-commons by mesosphere.
the class RoundRobinByAttributeRuleTest method getTaskInfo.
private static TaskInfo getTaskInfo(String taskName, String attrName, String attrVal) {
TaskInfo.Builder infoBuilder = TaskTestUtils.getTaskInfo(Collections.emptyList()).toBuilder().setName(taskName).setTaskId(CommonIdUtils.toTaskId(TestConstants.SERVICE_NAME, taskName));
infoBuilder.setLabels(new TaskLabelWriter(infoBuilder).setOfferAttributes(offerWithAttribute(attrName, attrVal)).toProto());
return infoBuilder.build();
}
use of com.mesosphere.sdk.offer.taskdata.TaskLabelWriter in project dcos-commons by mesosphere.
the class MaxPerHostnameRuleTest method getTask.
private static TaskInfo getTask(String id, Offer offer) {
TaskInfo.Builder taskBuilder = TaskTestUtils.getTaskInfo(Collections.emptyList()).toBuilder();
taskBuilder.getTaskIdBuilder().setValue(id);
try {
taskBuilder.setName(CommonIdUtils.toTaskName(taskBuilder.getTaskId()));
} catch (Exception e) {
throw new IllegalStateException(e);
}
taskBuilder.setLabels(new TaskLabelWriter(taskBuilder).setHostname(offer).toProto());
return taskBuilder.build();
}
use of com.mesosphere.sdk.offer.taskdata.TaskLabelWriter in project dcos-commons by mesosphere.
the class DefaultStepFactoryTest method testTaskWithFinishGoalStateCanReachGoalState.
@Test
public void testTaskWithFinishGoalStateCanReachGoalState() throws Exception {
Capabilities mockCapabilities = Mockito.mock(Capabilities.class);
Mockito.when(mockCapabilities.supportsDefaultExecutor()).thenReturn(true);
Capabilities.overrideCapabilities(mockCapabilities);
PodInstance podInstance = getPodInstanceWithGoalState(GoalState.FINISH);
List<String> tasksToLaunch = podInstance.getPod().getTasks().stream().map(taskSpec -> taskSpec.getName()).collect(Collectors.toList());
UUID configId = UUID.randomUUID();
configStore.setTargetConfig(configId);
String taskName = podInstance.getName() + '-' + tasksToLaunch.get(0);
stateStore.storeTasks(ImmutableList.of(Protos.TaskInfo.newBuilder().setName(taskName).setTaskId(CommonIdUtils.toTaskId(TestConstants.SERVICE_NAME, taskName)).setSlaveId(Protos.SlaveID.newBuilder().setValue("proto-field-required")).setLabels(new TaskLabelWriter(TestConstants.TASK_INFO).setTargetConfiguration(configId).toProto()).build()));
Protos.TaskInfo taskInfo = stateStore.fetchTask(taskName).get();
stateStore.storeStatus(taskName, Protos.TaskStatus.newBuilder().setState(Protos.TaskState.TASK_RUNNING).setTaskId(taskInfo.getTaskId()).build());
assertThat(((DefaultStepFactory) stepFactory).hasReachedGoalState(podInstance, stateStore.fetchTask(taskName).get()), is(false));
stateStore.storeStatus(taskName, Protos.TaskStatus.newBuilder().setState(Protos.TaskState.TASK_FINISHED).setTaskId(taskInfo.getTaskId()).build());
assertThat(((DefaultStepFactory) stepFactory).hasReachedGoalState(podInstance, stateStore.fetchTask(taskName).get()), is(true));
}
use of com.mesosphere.sdk.offer.taskdata.TaskLabelWriter in project dcos-commons by mesosphere.
the class DefaultStepFactoryTest method testInitialStateForRunningTaskOnCustomExecutorIsRunning.
@Test
public void testInitialStateForRunningTaskOnCustomExecutorIsRunning() throws Exception {
Capabilities mockCapabilities = Mockito.mock(Capabilities.class);
Mockito.when(mockCapabilities.supportsDefaultExecutor()).thenReturn(false);
Capabilities.overrideCapabilities(mockCapabilities);
PodInstance podInstance = getPodInstanceWithASingleTask();
List<String> tasksToLaunch = podInstance.getPod().getTasks().stream().map(taskSpec -> taskSpec.getName()).collect(Collectors.toList());
UUID configId = UUID.randomUUID();
configStore.setTargetConfig(configId);
String taskName = podInstance.getName() + '-' + tasksToLaunch.get(0);
stateStore.storeTasks(ImmutableList.of(Protos.TaskInfo.newBuilder().setName(taskName).setTaskId(CommonIdUtils.toTaskId(TestConstants.SERVICE_NAME, taskName)).setSlaveId(Protos.SlaveID.newBuilder().setValue("proto-field-required")).setLabels(new TaskLabelWriter(TestConstants.TASK_INFO).setTargetConfiguration(configId).setReadinessCheck(Protos.HealthCheck.newBuilder().build()).toProto()).build()));
Protos.TaskInfo taskInfo = stateStore.fetchTask(taskName).get();
stateStore.storeStatus(taskName, Protos.TaskStatus.newBuilder().setState(Protos.TaskState.TASK_RUNNING).setTaskId(taskInfo.getTaskId()).setLabels(Protos.Labels.newBuilder().addLabels(Protos.Label.newBuilder().setKey("readiness_check_passed").setValue("false").build()).build()).build());
assertThat(((DefaultStepFactory) stepFactory).hasReachedGoalState(podInstance, stateStore.fetchTask(taskName).get()), is(true));
final Step step = stepFactory.getStep(podInstance, tasksToLaunch);
assertThat(step.isComplete(), is(true));
assertThat(step.isPending(), is(false));
}
Aggregations