Search in sources :

Example 26 with PodSpec

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

the class PreReservationCannotChange method validate.

@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
    if (!oldConfig.isPresent()) {
        return Collections.emptyList();
    }
    final Map<String, PodSpec> newPods = newConfig.getPods().stream().collect(Collectors.toMap(podSpec -> podSpec.getType(), podSpec -> podSpec));
    final List<ConfigValidationError> errors = new ArrayList<>();
    for (final PodSpec oldPod : oldConfig.get().getPods()) {
        final PodSpec newPod = newPods.get(oldPod.getType());
        if (newPod == null) {
            continue;
        }
        final String oldPodPreReservedRole = oldPod.getPreReservedRole();
        final String newPodPreReservedRole = newPod.getPreReservedRole();
        if (!StringUtils.equals(newPodPreReservedRole, oldPodPreReservedRole)) {
            errors.add(ConfigValidationError.transitionError(String.format("PodSpec[pre-reserved-role:%s]", oldPodPreReservedRole), oldPodPreReservedRole, newPodPreReservedRole, String.format("New config has changed the pre-reserved-role of PodSpec named '%s'" + " (expected it to stay '%s')", oldPod.getType(), oldPodPreReservedRole)));
        }
    }
    return errors;
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec) java.util(java.util) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec)

Example 27 with PodSpec

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

the class PodSpecsCannotChangeNetworkRegime method validate.

@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
    if (!oldConfig.isPresent()) {
        return Collections.emptyList();
    }
    Map<String, PodSpec> newPods;
    try {
        newPods = newConfig.getPods().stream().collect(Collectors.toMap(podSpec -> podSpec.getType(), podSpec -> podSpec));
    } catch (IllegalStateException e) {
        return Arrays.asList(ConfigValidationError.valueError("PodSpecs", "null", "Duplicate pod types detected."));
    }
    // check the PodSpecs to make sure none of them make a transition from a state where they use host ports
    // to one where they don't (or vice versa).
    Collection<ConfigValidationError> errors = new ArrayList<>();
    Map<String, PodSpec> oldPods = oldConfig.get().getPods().stream().collect(Collectors.toMap(PodSpec::getType, podSpec -> podSpec));
    for (Map.Entry<String, PodSpec> kv : newPods.entrySet()) {
        PodSpec newPod = kv.getValue();
        // first we check that the new pod's networks (if present) do not erroneously use port mapping
        List<String> offendingNetworkNames = getNetworksWithInvalidPortMappings(newPod);
        if (offendingNetworkNames.size() > 0) {
            errors.add(ConfigValidationError.transitionError(String.format("PodSpec[name:%s]", newPod.getType()), "null", newPod.getNetworks().toString(), String.format("New config has pod %s that indicates port mapping for virtual network(s) %s, " + "that do not support port mapping.", newPod.getType(), StringUtils.join(offendingNetworkNames, ", "))));
        }
        // now we check that the none of the new pods move from not using host ports to using them (or vice
        // versa)
        PodSpec oldPod = oldPods.get(kv.getKey());
        if (oldPod != null && podSpecUsesHostPorts(oldPod) != podSpecUsesHostPorts(newPod)) {
            errors.add(ConfigValidationError.transitionError(String.format("PodSpec[name:%s]", oldPod.getType()), oldPod.getNetworks().toString(), newPod.getNetworks().toString(), String.format("New config has pod %s moving networks from %s to %s, changing its " + "host ports requirements from %s to %s, not allowed.", newPod.getType(), oldPod.getNetworks().toString(), newPod.getNetworks().toString(), podSpecUsesHostPorts(oldPod), podSpecUsesHostPorts(newPod))));
        }
    }
    return errors;
}
Also used : Arrays(java.util.Arrays) Collection(java.util.Collection) NetworkSpec(com.mesosphere.sdk.specification.NetworkSpec) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) ArrayList(java.util.ArrayList) PodSpec(com.mesosphere.sdk.specification.PodSpec) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Collections(java.util.Collections) DcosConstants(com.mesosphere.sdk.dcos.DcosConstants) PodSpec(com.mesosphere.sdk.specification.PodSpec) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 28 with PodSpec

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

the class UserCannotChange method checkForUserEqualityAmongPods.

private List<ConfigValidationError> checkForUserEqualityAmongPods(Map<String, PodSpec> oldPods, List<PodSpec> newPods) {
    List<ConfigValidationError> errors = new ArrayList<>();
    PodSpec oldPod;
    for (PodSpec newPod : newPods) {
        // about not updating the user for existing pods in this validation.
        if (oldPods.containsKey(newPod.getType())) {
            oldPod = oldPods.get(newPod.getType());
            if (!oldPod.getUser().equals(newPod.getUser())) {
                errors.add(ConfigValidationError.transitionError("user", oldPod.getUser().isPresent() ? oldPod.getUser().get() : null, newPod.getUser().isPresent() ? newPod.getUser().get() : null, String.format(USER_CHANGED_ERROR_MESSAGE, oldPod.getType())));
            }
        }
    }
    return errors;
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec)

Example 29 with PodSpec

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

the class UserCannotChange method validate.

@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
    if (!oldConfig.isPresent()) {
        return Collections.emptyList();
    }
    List<ConfigValidationError> errors = new ArrayList<>();
    if (oldConfig.get().getUser() != null && !oldConfig.get().getUser().equals(newConfig.getUser())) {
        errors.add(ConfigValidationError.transitionError("user", oldConfig.get().getUser(), newConfig.getUser(), "User for old service must remain the same across deployments."));
    }
    // We can't rely on the order of pods to test new pods against olds ones.
    Map<String, PodSpec> oldPods = new HashMap<>();
    for (PodSpec oldPod : oldConfig.get().getPods()) {
        oldPods.put(oldPod.getType(), oldPod);
    }
    errors.addAll(checkForUserEqualityAmongPods(oldPods, newConfig.getPods()));
    return errors;
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec)

Example 30 with PodSpec

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

the class RoundRobinByAttributeRuleTest method getPodInstance.

private static PodInstance getPodInstance(TaskInfo taskInfo) {
    try {
        TaskLabelReader labels = new TaskLabelReader(taskInfo);
        ResourceSet resourceSet = PodInstanceRequirementTestUtils.getCpuResourceSet(1.0);
        PodSpec podSpec = PodInstanceRequirementTestUtils.getRequirement(resourceSet, labels.getType(), labels.getIndex()).getPodInstance().getPod();
        return new DefaultPodInstance(podSpec, labels.getIndex());
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) ResourceSet(com.mesosphere.sdk.specification.ResourceSet) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) InvalidRequirementException(com.mesosphere.sdk.offer.InvalidRequirementException) TaskException(com.mesosphere.sdk.offer.TaskException)

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