use of io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig in project stackgres by ongres.
the class PostgresConfigTransformerTest method wrongConfigDto_shouldFail.
@Test
void wrongConfigDto_shouldFail() {
StackGresPostgresConfig original = createOriginal();
PostgresConfigDto source = createEmptyConfigDto();
source.getSpec().setPostgresqlConf("test");
Assertions.assertThrows(IllegalArgumentException.class, () -> transformer.toCustomResource(source, original));
}
use of io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig in project stackgres by ongres.
the class BlocklistValidatorTest method givenUpdateWithBlocklistedProperties_shouldFail.
@Test
void givenUpdateWithBlocklistedProperties_shouldFail() {
PgConfigReview review = JsonUtil.readFromJson("pgconfig_allow_request/valid_pgconfig_update.json", PgConfigReview.class);
StackGresPostgresConfig pgConfig = review.getRequest().getObject();
String[] blocklistedProperties = addBlocklistProperties(pgConfig);
assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
String errorMessage = ex.getResult().getMessage();
assertEquals("Invalid postgres configuration, properties: " + String.join(", ", blocklistedProperties) + " cannot be settled", errorMessage);
}
use of io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig in project stackgres by ongres.
the class PgConfigUpdateValidatorTest method updatesOfPgVersion_shouldFail.
@Test
void updatesOfPgVersion_shouldFail() {
final AdmissionRequest<StackGresPostgresConfig> request = review.getRequest();
request.getObject().getSpec().setPostgresVersion("12");
request.getOldObject().getSpec().setPostgresVersion("11");
ValidationFailed vf = ValidationUtils.assertErrorType(ErrorType.FORBIDDEN_CR_UPDATE, () -> validator.validate(review));
ValidationUtils.checkErrorCause(vf.getResult(), "spec.postgresVersion", "postgresVersion is not updatable", "FieldNotUpdatable");
}
use of io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig 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.sgpgconfig.StackGresPostgresConfig in project stackgres by ongres.
the class BlocklistValidatorTest method givenCreationWithBlocklistedProperties_shouldFail.
@Test
void givenCreationWithBlocklistedProperties_shouldFail() {
PgConfigReview review = JsonUtil.readFromJson("pgconfig_allow_request/valid_pgconfig.json", PgConfigReview.class);
StackGresPostgresConfig pgConfig = review.getRequest().getObject();
String[] blocklistedProperties = addBlocklistProperties(pgConfig);
ValidationFailed ex = assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
String errorMessage = ex.getResult().getMessage();
assertEquals("Invalid postgres configuration, properties: " + String.join(", ", blocklistedProperties) + " cannot be settled", errorMessage);
}
Aggregations