use of com.mesosphere.sdk.config.validate.ConfigValidationError 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.config.validate.ConfigValidationError in project dcos-commons by mesosphere.
the class DefaultConfigurationUpdater method updateConfiguration.
@Override
public UpdateResult updateConfiguration(ServiceSpec candidateConfig) throws ConfigStoreException {
// Get the currently stored target configuration
UUID targetConfigId;
try {
targetConfigId = configStore.getTargetConfig();
} catch (ConfigStoreException e) {
LOGGER.debug("No target configuration ID was set. First launch?");
targetConfigId = null;
}
Optional<ServiceSpec> targetConfig;
if (targetConfigId != null) {
LOGGER.info("Loading current target configuration: {}", targetConfigId);
targetConfig = Optional.of(configStore.fetch(targetConfigId));
} else {
targetConfig = Optional.empty();
}
// Log the config state (with diff of changes vs prior state) before proceeding with checks.
final List<ConfigValidationError> errors = new ArrayList<>();
String candidateConfigJson = null;
try {
candidateConfigJson = candidateConfig.toJsonString();
LOGGER.info("New prospective config:\n{}", candidateConfigJson);
} catch (Exception e) {
LOGGER.error(String.format("Unable to get JSON representation of new prospective config object: %s", candidateConfig), e);
errors.add(ConfigValidationError.valueError("NewConfigAsJson", "jsonString", String.format("Unable to serialize new config to JSON for logging: %s", e.getMessage())));
}
if (!targetConfig.isPresent()) {
LOGGER.info("Skipping config diff: There is no old config target to diff against");
} else if (candidateConfigJson == null) {
LOGGER.error("Skipping config diff: New target couldn't be represented as JSON");
} else {
LOGGER.info("Prior target config:\n{}", targetConfig.get().toJsonString());
printConfigDiff(targetConfig.get(), targetConfigId, candidateConfigJson);
}
targetConfig = fixServiceSpecUser(targetConfig);
// whether it's considered equal by the ConfigComparator.
for (ConfigValidator<ServiceSpec> validator : validators) {
errors.addAll(validator.validate(targetConfig, candidateConfig));
}
// there are validation errors against the new config, we continue using the prior target.
if (!errors.isEmpty()) {
StringJoiner sj = new StringJoiner("\n");
int i = 1;
for (ConfigValidationError error : errors) {
sj.add(String.format("%d: %s", i++, error.toString()));
}
LOGGER.warn("New configuration failed validation against current target " + "configuration {}, with {} errors across {} validators:\n{}", targetConfigId, errors.size(), validators.size(), sj.toString());
if (!targetConfig.isPresent()) {
throw new ConfigStoreException(Reason.LOGIC_ERROR, String.format("Configuration failed validation without any prior target configuration" + "available for fallback. Initial launch with invalid configuration? " + "%d Errors: %s", errors.size(), sj.toString()));
}
} else if (!targetConfig.isPresent() || !configComparator.equals(targetConfig.get(), candidateConfig)) {
UUID oldTargetId = targetConfigId;
targetConfigId = configStore.store(candidateConfig);
LOGGER.info("Updating target configuration: " + "Prior target configuration '{}' is different from new configuration '{}'. ", oldTargetId, targetConfigId);
targetConfig = Optional.of(candidateConfig);
configStore.setTargetConfig(targetConfigId);
} else {
LOGGER.info("No changes detected between current target configuration '{}' and new configuration. " + "Leaving current configuration as the target.", targetConfigId);
}
// Update config IDs on tasks whose config contents match the current target, then clean up
// leftover configs which are not the target and which are not referenced by any tasks.
cleanupDuplicateAndUnusedConfigs(targetConfig.get(), targetConfigId);
return new ConfigurationUpdater.UpdateResult(targetConfigId, errors);
}
use of com.mesosphere.sdk.config.validate.ConfigValidationError 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();
}
Aggregations