use of io.stackgres.common.crd.sgcluster.StackGresClusterRestore in project stackgres by ongres.
the class ClusterRequiredResourcesGenerator method getRequiredResources.
@Override
public List<HasMetadata> getRequiredResources(StackGresCluster config) {
final ObjectMeta metadata = config.getMetadata();
final String clusterName = metadata.getName();
final String clusterNamespace = metadata.getNamespace();
final StackGresClusterSpec spec = config.getSpec();
final StackGresClusterConfiguration clusterConfiguration = spec.getConfiguration();
final StackGresPostgresConfig clusterPgConfig = postgresConfigFinder.findByNameAndNamespace(clusterConfiguration.getPostgresConfig(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent SGPostgresConfig postgresconf"));
final StackGresProfile clusterProfile = profileFinder.findByNameAndNamespace(spec.getResourceProfile(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent " + StackGresProfile.KIND + " " + spec.getResourceProfile()));
final Optional<StackGresBackupConfig> backupConfig = Optional.ofNullable(clusterConfiguration.getBackupConfig()).flatMap(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, clusterNamespace));
final Optional<StackGresPoolingConfig> clusterPooling = Optional.ofNullable(clusterConfiguration.getConnectionPoolingConfig()).flatMap(poolingConfigName -> poolingConfigFinder.findByNameAndNamespace(poolingConfigName, clusterNamespace));
Optional<StackGresClusterRestore> restoreConfig = Optional.ofNullable(config.getSpec().getInitData()).map(StackGresClusterInitData::getRestore);
final Optional<StackGresBackup> restoreBackup;
if (restoreConfig.isEmpty()) {
restoreBackup = Optional.empty();
} else {
restoreBackup = restoreConfig.map(restore -> {
final List<StackGresBackup> backups = backupScanner.getResources();
return backups.stream().filter(backup -> backup.getMetadata().getUid().equals(restore.getFromBackup().getUid())).peek(backup -> {
Preconditions.checkNotNull(backup.getStatus(), "Backup is " + BackupPhase.RUNNING.label());
Preconditions.checkNotNull(backup.getStatus().getProcess(), "Backup is " + BackupPhase.RUNNING.label());
Preconditions.checkArgument(backup.getStatus().getProcess().getStatus().equals(BackupPhase.COMPLETED.label()), "Backup is " + backup.getStatus().getProcess().getStatus());
}).findFirst().orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have an invalid restore backup Uid"));
});
}
StackGresClusterContext context = ImmutableStackGresClusterContext.builder().source(config).postgresConfig(clusterPgConfig).stackGresProfile(clusterProfile).backupConfig(backupConfig).poolingConfig(clusterPooling).restoreBackup(restoreBackup).prometheus(getPrometheus(config)).internalScripts(List.of(getPostgresExporterInitScript())).databaseCredentials(secretFinder.findByNameAndNamespace(clusterName, clusterNamespace)).build();
final List<ResourceGenerator<StackGresClusterContext>> resourceGenerators = generators.getResourceGenerators(context);
final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
List<Decorator<StackGresClusterContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
decorators.forEach(decorator -> decorator.decorate(context, resources));
return resources;
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterRestore in project stackgres by ongres.
the class DefaultRestoreMutator method mutate.
@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
final StackGresClusterInitData initData = review.getRequest().getObject().getSpec().getInitData();
if (review.getRequest().getOperation() == Operation.CREATE && initData != null) {
StackGresClusterRestore restore = initData.getRestore();
if (restore != null) {
JsonNode target = mapper.valueToTree(restore);
ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
operations.addAll(applyDefaults(restorePointer, defaultNode, target));
return operations.build();
}
}
return ImmutableList.of();
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterRestore in project stackgres by ongres.
the class DefaultRestoreMutator method init.
@PostConstruct
public void init() throws NoSuchFieldException {
String initDataJson = ClusterMutator.getJsonMappingField("initData", StackGresClusterSpec.class);
String restoreJsonField = ClusterMutator.getJsonMappingField("restore", StackGresClusterInitData.class);
restorePointer = ClusterMutator.CLUSTER_CONFIG_POINTER.append(initDataJson).append(restoreJsonField);
StackGresClusterRestore defaultRestore = defaultRestoreFactory.buildResource();
defaultNode = mapper.valueToTree(defaultRestore);
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterRestore in project stackgres by ongres.
the class RestoreConfigValidator method checkBackup.
private void checkBackup(StackGresClusterReview review, StackGresClusterRestore restoreConfig) throws ValidationFailed {
String backupUid = restoreConfig.getFromBackup().getUid();
switch(review.getRequest().getOperation()) {
case CREATE:
Optional<StackGresBackup> config = findBackup(backupUid);
if (config.isEmpty()) {
final String message = "Backup uid " + backupUid + " not found";
fail(errorCrReferencerUri, message);
}
StackGresBackup backup = config.get();
final StackGresBackupProcess process = backup.getStatus().getProcess();
if (backup.getStatus() == null || !process.getStatus().equals("Completed")) {
final String message = "Cannot restore from backup " + backupUid + " because it's not ready";
fail(errorCrReferencerUri, message);
}
String backupMajorVersion = RestoreConfigValidator.getMajorVersion(backup);
String givenPgVersion = review.getRequest().getObject().getSpec().getPostgres().getVersion();
String givenMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(givenPgVersion);
if (!backupMajorVersion.equals(givenMajorVersion)) {
final String message = "Cannot restore from backup " + backupUid + " because it comes from an incompatible postgres version";
fail(errorPostgresMismatch, message);
}
break;
case UPDATE:
StackGresClusterRestore oldRestoreConfig = review.getRequest().getOldObject().getSpec().getInitData().getRestore();
String oldBackupUid = oldRestoreConfig.getFromBackup().getUid();
final String message = "Cannot update cluster's restore configuration";
if (backupUid == null && oldBackupUid != null || backupUid != null && oldBackupUid == null) {
fail(errorCrReferencerUri, message);
}
if (backupUid != null && !backupUid.equals(oldBackupUid)) {
fail(errorCrReferencerUri, message);
}
break;
default:
}
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterRestore in project stackgres by ongres.
the class RestoreConfigValidator method validate.
@Override
public void validate(StackGresClusterReview review) throws ValidationFailed {
StackGresCluster cluster = review.getRequest().getObject();
Optional<StackGresClusterRestore> restoreOpt = Optional.of(cluster.getSpec()).map(StackGresClusterSpec::getInitData).map(StackGresClusterInitData::getRestore);
checkRestoreConfig(review, restoreOpt);
if (restoreOpt.isPresent()) {
StackGresClusterRestore restoreConfig = restoreOpt.get();
checkBackup(review, restoreConfig);
}
}
Aggregations