Search in sources :

Example 1 with TaskLabelWriter

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;
}
Also used : TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) URI(java.net.URI)

Example 2 with TaskLabelWriter

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();
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)

Example 3 with TaskLabelWriter

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();
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) IOException(java.io.IOException)

Example 4 with TaskLabelWriter

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));
}
Also used : SchedulerConfigTestUtils(com.mesosphere.sdk.testutils.SchedulerConfigTestUtils) Protos(org.apache.mesos.Protos) SchedulerConfig(com.mesosphere.sdk.scheduler.SchedulerConfig) Arrays(java.util.Arrays) TestConstants(com.mesosphere.sdk.testutils.TestConstants) CommonIdUtils(com.mesosphere.sdk.offer.CommonIdUtils) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) MemPersister(com.mesosphere.sdk.storage.MemPersister) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) ConfigStore(com.mesosphere.sdk.state.ConfigStore) Assert.assertThat(org.junit.Assert.assertThat) Mockito(org.mockito.Mockito) Capabilities(com.mesosphere.sdk.dcos.Capabilities) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) com.mesosphere.sdk.specification(com.mesosphere.sdk.specification) StateStore(com.mesosphere.sdk.state.StateStore) Persister(com.mesosphere.sdk.storage.Persister) Matchers.is(org.hamcrest.Matchers.is) TestPodFactory(com.mesosphere.sdk.testutils.TestPodFactory) Assert(org.junit.Assert) Protos(org.apache.mesos.Protos) Capabilities(com.mesosphere.sdk.dcos.Capabilities) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with TaskLabelWriter

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));
}
Also used : SchedulerConfigTestUtils(com.mesosphere.sdk.testutils.SchedulerConfigTestUtils) Protos(org.apache.mesos.Protos) SchedulerConfig(com.mesosphere.sdk.scheduler.SchedulerConfig) Arrays(java.util.Arrays) TestConstants(com.mesosphere.sdk.testutils.TestConstants) CommonIdUtils(com.mesosphere.sdk.offer.CommonIdUtils) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) MemPersister(com.mesosphere.sdk.storage.MemPersister) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) ConfigStore(com.mesosphere.sdk.state.ConfigStore) Assert.assertThat(org.junit.Assert.assertThat) Mockito(org.mockito.Mockito) Capabilities(com.mesosphere.sdk.dcos.Capabilities) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) com.mesosphere.sdk.specification(com.mesosphere.sdk.specification) StateStore(com.mesosphere.sdk.state.StateStore) Persister(com.mesosphere.sdk.storage.Persister) Matchers.is(org.hamcrest.Matchers.is) TestPodFactory(com.mesosphere.sdk.testutils.TestPodFactory) Assert(org.junit.Assert) Protos(org.apache.mesos.Protos) Capabilities(com.mesosphere.sdk.dcos.Capabilities) TaskLabelWriter(com.mesosphere.sdk.offer.taskdata.TaskLabelWriter) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

TaskLabelWriter (com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)21 Protos (org.apache.mesos.Protos)11 Test (org.junit.Test)9 UUID (java.util.UUID)7 StateStore (com.mesosphere.sdk.state.StateStore)6 MemPersister (com.mesosphere.sdk.storage.MemPersister)6 Persister (com.mesosphere.sdk.storage.Persister)6 TaskInfo (org.apache.mesos.Protos.TaskInfo)6 ImmutableList (com.google.common.collect.ImmutableList)5 Capabilities (com.mesosphere.sdk.dcos.Capabilities)5 CommonIdUtils (com.mesosphere.sdk.offer.CommonIdUtils)5 SchedulerConfig (com.mesosphere.sdk.scheduler.SchedulerConfig)5 com.mesosphere.sdk.specification (com.mesosphere.sdk.specification)5 ConfigStore (com.mesosphere.sdk.state.ConfigStore)5 SchedulerConfigTestUtils (com.mesosphere.sdk.testutils.SchedulerConfigTestUtils)5 TestConstants (com.mesosphere.sdk.testutils.TestConstants)5 TestPodFactory (com.mesosphere.sdk.testutils.TestPodFactory)5 Arrays (java.util.Arrays)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5