Search in sources :

Example 21 with PodSpec

use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.

the class DomainCapabilityValidator method validate.

@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
    if (Capabilities.getInstance().supportsDomains()) {
        return Collections.emptyList();
    }
    List<PodSpec> podSpecs = newConfig.getPods().stream().filter(podSpec -> podSpec.getPlacementRule().isPresent()).collect(Collectors.toList());
    List<ConfigValidationError> errors = new ArrayList<>();
    for (PodSpec podSpec : podSpecs) {
        PlacementRule placementRule = podSpec.getPlacementRule().get();
        if (PlacementUtils.placementRuleReferencesZone(podSpec)) {
            String errMsg = String.format("The PlacementRule for PodSpec '%s' may not reference Zones prior to DC/OS 1.11.", podSpec.getType());
            errors.add(ConfigValidationError.valueError("PlacementRule", placementRule.toString(), errMsg));
        }
        if (PlacementUtils.placementRuleReferencesRegion(podSpec)) {
            String errMsg = String.format("The PlacementRule for PodSpec '%s' may not reference Regions prior to DC/OS 1.11.", podSpec.getType());
            errors.add(ConfigValidationError.valueError("PlacementRule", placementRule.toString(), errMsg));
        }
    }
    return errors;
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec) Capabilities(com.mesosphere.sdk.dcos.Capabilities) java.util(java.util) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) PlacementUtils(com.mesosphere.sdk.offer.evaluate.placement.PlacementUtils) Collectors(java.util.stream.Collectors) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)

Example 22 with PodSpec

use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.

the class DefaultConfigurationUpdater method fixServiceSpecUser.

/**
 * Detects whether the previous {@link ServiceSpec} set the user. If it didn't, we set it to "root"
 * as Mesos treats a non-set user as "root".
 *
 * @param targetConfig The previous service spec from the config
 */
private Optional<ServiceSpec> fixServiceSpecUser(Optional<ServiceSpec> targetConfig) {
    if (!targetConfig.isPresent()) {
        return Optional.empty();
    }
    DefaultServiceSpec.Builder serviceSpecWithUser = DefaultServiceSpec.newBuilder(targetConfig.get());
    if (targetConfig.get().getUser() == null) {
        serviceSpecWithUser.user(DcosConstants.DEFAULT_SERVICE_USER);
    }
    List<PodSpec> podsWithUser = new ArrayList<>();
    for (PodSpec podSpec : targetConfig.get().getPods()) {
        podsWithUser.add(podSpec.getUser() != null && podSpec.getUser().isPresent() ? podSpec : DefaultPodSpec.newBuilder(podSpec).user(DcosConstants.DEFAULT_SERVICE_USER).build());
    }
    serviceSpecWithUser.pods(podsWithUser);
    return Optional.of(serviceSpecWithUser.build());
}
Also used : DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec)

Example 23 with PodSpec

use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.

the class ConfigurationUpdaterTest method testUserSetAtServiceLevelButNotPodLevel.

@Test
public void testUserSetAtServiceLevelButNotPodLevel() throws ConfigStoreException {
    final ConfigurationUpdater<ServiceSpec> configurationUpdater = new DefaultConfigurationUpdater(mockStateStore, mockConfigStore, DefaultServiceSpec.getComparatorInstance(), DefaultConfigValidators.getValidators(SchedulerConfigTestUtils.getTestSchedulerConfig()));
    when(mockConfigStore.getTargetConfig()).thenReturn(TARGET_ID);
    DefaultServiceSpec.Builder serviceSpecWithUser = DefaultServiceSpec.newBuilder(ORIGINAL_SERVICE_SPECIFICATION).user(DcosConstants.DEFAULT_SERVICE_USER);
    List<PodSpec> podsWithoutUsers = new ArrayList<>();
    for (PodSpec podSpec : ORIGINAL_SERVICE_SPECIFICATION.getPods()) {
        podsWithoutUsers.add(DefaultPodSpec.newBuilder(podSpec).user(null).build());
    }
    serviceSpecWithUser.pods(podsWithoutUsers);
    when(mockConfigStore.fetch(TARGET_ID)).thenReturn(serviceSpecWithUser.build());
    // Note: the new service spec sets pod users with a non-root user
    ConfigurationUpdater.UpdateResult result = configurationUpdater.updateConfiguration(ORIGINAL_SERVICE_SPECIFICATION_WITH_USER);
    Assert.assertEquals(TARGET_ID, result.getTargetId());
    // since the 2 pods don't set the user their user defaults to "root" which conflicts with the user set as noted above
    Assert.assertEquals(2, result.getErrors().size());
}
Also used : DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) Test(org.junit.Test)

Example 24 with PodSpec

use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.

the class ZoneValidator method validate.

private static Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig, String podType) {
    if (!oldConfig.isPresent()) {
        return Collections.emptyList();
    }
    Optional<PodSpec> oldPod = getPodSpec(oldConfig.get(), podType);
    if (!oldPod.isPresent()) {
        // Maybe the pod or task was renamed? Lets avoid enforcing whether those are rename- able and assume it's OK
        return Collections.emptyList();
    }
    Optional<PodSpec> newPod = getPodSpec(newConfig, podType);
    if (!newPod.isPresent()) {
        throw new IllegalArgumentException(String.format("Unable to find requested pod=%s, in config: %s", podType, newConfig));
    }
    boolean oldReferencesZones = PlacementUtils.placementRuleReferencesZone(oldPod.get());
    boolean newReferencesZones = PlacementUtils.placementRuleReferencesZone(newPod.get());
    if (oldReferencesZones != newReferencesZones) {
        Optional<PlacementRule> oldRule = oldPod.get().getPlacementRule();
        Optional<PlacementRule> newRule = newPod.get().getPlacementRule();
        ConfigValidationError error = ConfigValidationError.transitionError(String.format("%s.PlacementRule", podType), oldRule.toString(), newRule.toString(), String.format("PlacementRule cannot change from %s to %s", oldRule, newRule));
        return Arrays.asList(error);
    }
    return Collections.emptyList();
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec) ConfigValidationError(com.mesosphere.sdk.config.validate.ConfigValidationError) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)

Example 25 with PodSpec

use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.

the class PlacementRuleEvaluationStageTest method testOfferFailsPlacementRule.

@Test
public void testOfferFailsPlacementRule() throws Exception {
    String agent = "test-agent";
    Protos.Resource offered = ResourceTestUtils.getUnreservedCpus(1.0);
    PlacementRule rule = AgentRule.require(agent);
    Protos.Offer offer = offerWithAgent("other-agent", offered);
    MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));
    PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
    DefaultPodSpec.newBuilder(podSpec).placementRule(rule);
    PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
    List<String> taskNames = TaskUtils.getTaskNames(podInstance);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, taskNames).build();
    PlacementRuleEvaluationStage placementRuleEvaluationStage = new PlacementRuleEvaluationStage(Collections.emptyList(), rule);
    EvaluationOutcome outcome = placementRuleEvaluationStage.evaluate(mesosResourcePool, new PodInfoBuilder(podInstanceRequirement, TestConstants.SERVICE_NAME, UUID.randomUUID(), ArtifactResource.getUrlFactory(TestConstants.SERVICE_NAME), SchedulerConfigTestUtils.getTestSchedulerConfig(), Collections.emptyList(), TestConstants.FRAMEWORK_ID, true, Collections.emptyMap()));
    Assert.assertFalse(outcome.isPassing());
    Assert.assertEquals(3, mesosResourcePool.getUnreservedMergedPool().size());
    Assert.assertTrue(Math.abs(mesosResourcePool.getUnreservedMergedPool().get("cpus").getScalar().getValue() - 1.1) < 0.01);
}
Also used : DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) PodInstance(com.mesosphere.sdk.specification.PodInstance) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) MesosResourcePool(com.mesosphere.sdk.offer.MesosResourcePool) Protos(org.apache.mesos.Protos) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) Test(org.junit.Test)

Aggregations

PodSpec (com.mesosphere.sdk.specification.PodSpec)35 DefaultPodSpec (com.mesosphere.sdk.specification.DefaultPodSpec)21 Test (org.junit.Test)16 PlacementRule (com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)10 PodInstance (com.mesosphere.sdk.specification.PodInstance)10 DefaultPodInstance (com.mesosphere.sdk.scheduler.plan.DefaultPodInstance)9 Protos (org.apache.mesos.Protos)8 Collectors (java.util.stream.Collectors)7 PodInstanceRequirement (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement)6 ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)6 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)4 TaskLabelReader (com.mesosphere.sdk.offer.taskdata.TaskLabelReader)4 DefaultServiceSpec (com.mesosphere.sdk.specification.DefaultServiceSpec)4 List (java.util.List)4 InvalidRequirementException (com.mesosphere.sdk.offer.InvalidRequirementException)3 TaskException (com.mesosphere.sdk.offer.TaskException)3 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)3 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 ConfigValidationError (com.mesosphere.sdk.config.validate.ConfigValidationError)2