use of io.stackgres.common.crd.sgbackup.StackGresBackup in project stackgres by ongres.
the class ClusterValidator method checkIfClusterExists.
private void checkIfClusterExists(BackupReview review, String onError) throws ValidationFailed {
StackGresBackup backup = review.getRequest().getObject();
String cluster = backup.getSpec().getSgCluster();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
Optional<StackGresCluster> clusterOpt = clusterFinder.findByNameAndNamespace(cluster, namespace);
if (!clusterOpt.isPresent()) {
fail(onError);
}
}
use of io.stackgres.common.crd.sgbackup.StackGresBackup in project stackgres by ongres.
the class RestoreConfigValidatorTest method givenACreationWithBackupFromDifferentPgVersion_shouldFail.
@Test
void givenACreationWithBackupFromDifferentPgVersion_shouldFail() {
final StackGresClusterReview review = getCreationReview();
review.getRequest().getObject().getSpec().getPostgres().setVersion(secondPgMajorVersion);
String stackgresBackup = review.getRequest().getObject().getSpec().getInitData().getRestore().getFromBackup().getUid();
StackGresBackup backup = backupList.getItems().stream().filter(b -> b.getMetadata().getUid().equals(stackgresBackup)).findFirst().orElseThrow(AssertionError::new);
backup.getStatus().getBackupInformation().setPostgresVersion(firstPgMajorVersionNumber);
when(scanner.findResources()).thenReturn(Optional.of(backupList.getItems()));
ValidationUtils.assertValidationFailed(() -> validator.validate(review), "Cannot restore from backup " + stackgresBackup + " because it comes from an incompatible postgres version");
verify(scanner).findResources();
}
use of io.stackgres.common.crd.sgbackup.StackGresBackup in project stackgres by ongres.
the class BackupTransformer method toCustomResource.
@Override
public StackGresBackup toCustomResource(BackupDto source, StackGresBackup original) {
StackGresBackup transformation = Optional.ofNullable(original).orElseGet(StackGresBackup::new);
transformation.setMetadata(getCustomResourceMetadata(source, original));
transformation.setSpec(getCustomResourceSpec(source.getSpec()));
return transformation;
}
use of io.stackgres.common.crd.sgbackup.StackGresBackup 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.sgbackup.StackGresBackup in project stackgres by ongres.
the class BackupRequiredResourcesGenerator method getRequiredResources.
@Override
public List<HasMetadata> getRequiredResources(StackGresBackup config) {
final ObjectMeta metadata = config.getMetadata();
final String dbOpsName = metadata.getName();
final String dbOpsNamespace = metadata.getNamespace();
final StackGresBackupSpec spec = config.getSpec();
final StackGresCluster cluster = clusterFinder.findByNameAndNamespace(spec.getSgCluster(), dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target a non existent SGCluster " + spec.getSgCluster()));
final StackGresBackupConfig backupConfig = Optional.of(cluster.getSpec()).map(StackGresClusterSpec::getConfiguration).map(StackGresClusterConfiguration::getBackupConfig).map(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target SGCluster " + spec.getSgCluster() + " with a non existent SGBackupConfig " + backupConfigName))).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target SGCluster " + spec.getSgCluster() + " without a SGBackupConfig"));
StackGresBackupContext context = ImmutableStackGresBackupContext.builder().source(config).cluster(cluster).backupConfig(backupConfig).build();
final List<ResourceGenerator<StackGresBackupContext>> resourceGenerators = generators.getResourceGenerators(context);
final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
List<Decorator<StackGresBackupContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
decorators.forEach(decorator -> decorator.decorate(context, resources));
return resources;
}
Aggregations