use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testColocateAgents.
@Test
public void testColocateAgents() throws Exception {
Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
// Don't launch
PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList("some-random-agent")).get();
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(0, recommendations.size());
// Launch
placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList(TestConstants.AGENT_ID.getValue())).get();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
podInstance = new DefaultPodInstance(podSpec, 0);
podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(5, recommendations.size());
}
use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testAvoidAgents.
@Test
public void testAvoidAgents() throws Exception {
Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
// Don't launch
PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList(TestConstants.AGENT_ID.getValue()), Collections.emptyList()).get();
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(0, recommendations.size());
// Launch
placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList("some-random-agent"), Collections.emptyList()).get();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
podInstance = new DefaultPodInstance(podSpec, 0);
podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(5, recommendations.size());
}
use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.
the class YAMLToInternalMappers method convertPod.
private static PodSpec convertPod(RawPod rawPod, ConfigTemplateReader configTemplateReader, String podName, Map<String, String> additionalEnv, String role, String principal, String executorUri, String user) throws Exception {
DefaultPodSpec.Builder builder = DefaultPodSpec.newBuilder(executorUri).count(rawPod.getCount()).type(podName).user(user).preReservedRole(rawPod.getPreReservedRole()).sharePidNamespace(rawPod.getSharePidNamespace()).allowDecommission(rawPod.getAllowDecommission());
List<String> networkNames = new ArrayList<>();
List<RLimitSpec> rlimits = new ArrayList<>();
for (Map.Entry<String, RawRLimit> entry : rawPod.getRLimits().entrySet()) {
RawRLimit rawRLimit = entry.getValue();
rlimits.add(new RLimitSpec(entry.getKey(), rawRLimit.getSoft(), rawRLimit.getHard()));
}
WriteOnceLinkedHashMap<String, RawNetwork> rawNetworks = rawPod.getNetworks();
final Collection<NetworkSpec> networks = new ArrayList<>();
if (MapUtils.isNotEmpty(rawNetworks)) {
networks.addAll(rawNetworks.entrySet().stream().map(rawNetworkEntry -> {
String networkName = rawNetworkEntry.getKey();
DcosConstants.warnIfUnsupportedNetwork(networkName);
networkNames.add(networkName);
RawNetwork rawNetwork = rawNetworks.get(networkName);
return convertNetwork(networkName, rawNetwork, collatePorts(rawPod));
}).collect(Collectors.toList()));
}
builder.image(rawPod.getImage()).networks(networks).rlimits(rlimits);
// Collect the resourceSets (if given)
final Collection<ResourceSet> resourceSets = new ArrayList<>();
WriteOnceLinkedHashMap<String, RawResourceSet> rawResourceSets = rawPod.getResourceSets();
if (MapUtils.isNotEmpty(rawResourceSets)) {
resourceSets.addAll(rawResourceSets.entrySet().stream().map(rawResourceSetEntry -> {
String rawResourceSetName = rawResourceSetEntry.getKey();
RawResourceSet rawResourceSet = rawResourceSets.get(rawResourceSetName);
return convertResourceSet(rawResourceSetName, rawResourceSet.getCpus(), rawResourceSet.getGpus(), rawResourceSet.getMemory(), rawResourceSet.getPorts(), rawResourceSet.getVolume(), rawResourceSet.getVolumes(), role, rawPod.getPreReservedRole(), principal, networkNames);
}).collect(Collectors.toList()));
}
if (!rawPod.getSecrets().isEmpty()) {
Collection<SecretSpec> secretSpecs = new ArrayList<>();
secretSpecs.addAll(rawPod.getSecrets().values().stream().map(v -> convertSecret(v)).collect(Collectors.toList()));
builder.secrets(secretSpecs);
}
if (rawPod.getVolume() != null || !rawPod.getVolumes().isEmpty()) {
Collection<VolumeSpec> volumeSpecs = new ArrayList<>(rawPod.getVolume() == null ? Collections.emptyList() : Arrays.asList(convertVolume(rawPod.getVolume(), role, rawPod.getPreReservedRole(), principal)));
volumeSpecs.addAll(rawPod.getVolumes().values().stream().map(v -> convertVolume(v, role, rawPod.getPreReservedRole(), principal)).collect(Collectors.toList()));
builder.volumes(volumeSpecs);
}
// Parse the TaskSpecs
List<TaskSpec> taskSpecs = new ArrayList<>();
for (Map.Entry<String, RawTask> entry : rawPod.getTasks().entrySet()) {
taskSpecs.add(convertTask(entry.getValue(), configTemplateReader, entry.getKey(), additionalEnv, resourceSets, role, rawPod.getPreReservedRole(), principal, networkNames));
}
builder.tasks(taskSpecs);
Collection<URI> podUris = new ArrayList<>();
for (String uriStr : rawPod.getUris()) {
podUris.add(new URI(uriStr));
}
builder.uris(podUris);
PlacementRule placementRule = MarathonConstraintParser.parse(podName, rawPod.getPlacement());
if (!(placementRule instanceof PassthroughRule)) {
builder.placementRule(placementRule);
}
return builder.build();
}
use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.
the class RegionValidator 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()) {
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 oldReferencesRegions = PlacementUtils.placementRuleReferencesRegion(oldPod.get());
boolean newReferencesRegions = PlacementUtils.placementRuleReferencesRegion(newPod.get());
if (oldReferencesRegions != newReferencesRegions) {
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.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.
the class PlacementRuleIsValid method validate.
@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
List<PodSpec> podSpecs = newConfig.getPods().stream().filter(podSpec -> podSpec.getPlacementRule().isPresent()).collect(Collectors.toList());
List<ConfigValidationError> errors = new ArrayList<>();
for (final PodSpec podSpec : podSpecs) {
PlacementRule placementRule = podSpec.getPlacementRule().get();
if (!isValid(placementRule)) {
String errMsg = String.format("The PlacementRule for PodSpec '%s' had invalid constraints", podSpec.getType());
errors.add(ConfigValidationError.valueError("PlacementRule", placementRule.toString(), errMsg));
}
}
return errors;
}
Aggregations