Search in sources :

Example 6 with StackGresPostgresConfig

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));
}
Also used : PostgresConfigDto(io.stackgres.apiweb.dto.pgconfig.PostgresConfigDto) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) Test(org.junit.jupiter.api.Test)

Example 7 with StackGresPostgresConfig

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);
}
Also used : PgConfigReview(io.stackgres.operator.common.PgConfigReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) Test(org.junit.jupiter.api.Test)

Example 8 with StackGresPostgresConfig

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");
}
Also used : ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) Test(org.junit.jupiter.api.Test)

Example 9 with StackGresPostgresConfig

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;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) Inject(javax.inject.Inject) OperatorPropertyContext(io.stackgres.operator.configuration.OperatorPropertyContext) ResourceGenerationDiscoverer(io.stackgres.operator.conciliation.ResourceGenerationDiscoverer) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) Map(java.util.Map) Prometheus(io.stackgres.operator.common.Prometheus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) CustomResourceScanner(io.stackgres.common.resource.CustomResourceScanner) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) Unchecked(org.jooq.lambda.Unchecked) Logger(org.slf4j.Logger) Resources(com.google.common.io.Resources) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PrometheusConfigSpec(io.stackgres.operator.customresource.prometheus.PrometheusConfigSpec) StandardCharsets(java.nio.charset.StandardCharsets) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) BackupPhase(io.stackgres.common.crd.sgbackup.BackupPhase) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) PrometheusInstallation(io.stackgres.operator.customresource.prometheus.PrometheusInstallation) DecoratorDiscoverer(io.stackgres.operator.conciliation.factory.DecoratorDiscoverer) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) Optional(java.util.Optional) Secret(io.fabric8.kubernetes.api.model.Secret) ResourceFinder(io.stackgres.common.resource.ResourceFinder) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Preconditions(com.google.common.base.Preconditions) OperatorProperty(io.stackgres.common.OperatorProperty) PrometheusConfig(io.stackgres.operator.customresource.prometheus.PrometheusConfig) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) List(java.util.List) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig)

Example 10 with StackGresPostgresConfig

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);
}
Also used : PgConfigReview(io.stackgres.operator.common.PgConfigReview) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresPostgresConfig (io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig)20 Test (org.junit.jupiter.api.Test)11 PostgresConfigDto (io.stackgres.apiweb.dto.pgconfig.PostgresConfigDto)8 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 StackGresPostgresConfigSpec (io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfigSpec)3 ValidationFailed (io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed)3 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)2 StackGresPostgresConfigStatus (io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfigStatus)2 PgConfigReview (io.stackgres.operator.common.PgConfigReview)2 AddOperation (com.github.fge.jsonpatch.AddOperation)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 Resources (com.google.common.io.Resources)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1 Secret (io.fabric8.kubernetes.api.model.Secret)1 OperatorProperty (io.stackgres.common.OperatorProperty)1 BackupPhase (io.stackgres.common.crd.sgbackup.BackupPhase)1 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)1 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)1