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