Search in sources :

Example 1 with StackGresPoolingConfig

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfig in project stackgres by ongres.

the class PgBouncerConfigResourceQuarkusTest method setUp.

@BeforeEach
void setUp() {
    StackGresPoolingConfig customResource = getCustomResource();
    customResource.getMetadata().setNamespace(StringUtils.getRandomNamespace());
    customResource.getMetadata().setName(StringUtils.getRandomClusterName());
    customResource.getMetadata().setSelfLink(null);
    try (KubernetesClient client = factory.create()) {
        this.resource = client.customResources(StackGresPoolingConfig.class, StackGresPoolingConfigList.class).inNamespace(customResource.getMetadata().getNamespace()).withName(customResource.getMetadata().getNamespace()).create(customResource);
    }
}
Also used : StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with StackGresPoolingConfig

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfig 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 3 with StackGresPoolingConfig

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfig in project stackgres by ongres.

the class PoolingConfigValidator method checkIfPoolingConfigExists.

private void checkIfPoolingConfigExists(StackGresClusterReview review, String onError) throws ValidationFailed {
    StackGresCluster cluster = review.getRequest().getObject();
    String poolingConfig = cluster.getSpec().getConfiguration().getConnectionPoolingConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    if (Boolean.FALSE.equals(cluster.getSpec().getPod().getDisableConnectionPooling())) {
        checkIfProvided(poolingConfig, "sgPoolingConfig");
        Optional<StackGresPoolingConfig> poolingConfigOpt = configFinder.findByNameAndNamespace(poolingConfig, namespace);
        if (poolingConfigOpt.isEmpty()) {
            fail(onError);
        }
    }
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig)

Example 4 with StackGresPoolingConfig

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfig in project stackgres by ongres.

the class PgBouncerDefaultStateMutator method mutate.

@Override
public List<JsonPatchOperation> mutate(PoolingReview review) {
    List<JsonPatchOperation> operations = new ArrayList<>();
    StackGresPoolingConfig pgBouncerConfig = review.getRequest().getObject();
    if (pgBouncerConfig.getStatus() == null) {
        pgBouncerConfig.setStatus(new StackGresPoolingConfigStatus());
        operations.add(buildAddOperation(PG_BOUNCER_DEFAULT_PARAMETERS_POINTER.parent().parent(), MAPPER.createObjectNode()));
    }
    if (pgBouncerConfig.getStatus().getPgBouncer() == null) {
        pgBouncerConfig.getStatus().setPgBouncer(new StackGresPoolingConfigPgBouncerStatus());
        operations.add(buildAddOperation(PG_BOUNCER_DEFAULT_PARAMETERS_POINTER.parent(), MAPPER.createObjectNode()));
    }
    operations.addAll(mutate(PG_BOUNCER_DEFAULT_PARAMETERS_POINTER, pgBouncerConfig));
    return operations;
}
Also used : StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) ArrayList(java.util.ArrayList) StackGresPoolingConfigPgBouncerStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerStatus) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresPoolingConfigStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigStatus)

Example 5 with StackGresPoolingConfig

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfig in project stackgres by ongres.

the class PgBouncerDefaultValuesMutator method mutate.

@Override
public List<JsonPatchOperation> mutate(PoolingReview review) {
    ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
    StackGresPoolingConfig pgBouncerConfig = review.getRequest().getObject();
    StackGresPoolingConfigSpec spec = pgBouncerConfig.getSpec();
    if (spec == null) {
        spec = new StackGresPoolingConfigSpec();
        pgBouncerConfig.setSpec(spec);
        operations.add(new AddOperation(PG_BOUNCER_CONFIG_POINTER.parent().parent().parent(), FACTORY.objectNode()));
    }
    StackGresPoolingConfigPgBouncer pgBouncer = spec.getPgBouncer();
    if (pgBouncer == null) {
        pgBouncer = new StackGresPoolingConfigPgBouncer();
        spec.setPgBouncer(pgBouncer);
        operations.add(new AddOperation(PG_BOUNCER_CONFIG_POINTER.parent().parent(), FACTORY.objectNode()));
    }
    var pgBouncerIni = pgBouncer.getPgbouncerIni();
    if (pgBouncerIni == null) {
        pgBouncerIni = new StackGresPoolingConfigPgBouncerPgbouncerIni();
        pgBouncer.setPgbouncerIni(pgBouncerIni);
        operations.add(new AddOperation(PG_BOUNCER_CONFIG_POINTER.parent(), FACTORY.objectNode()));
    }
    if (pgBouncerIni.getParameters() == null) {
        pgBouncerIni.setParameters(Map.of());
        operations.add(new AddOperation(PG_BOUNCER_CONFIG_POINTER, FACTORY.objectNode()));
    }
    operations.addAll(mutate(PG_BOUNCER_CONFIG_POINTER, pgBouncerConfig));
    return operations.build();
}
Also used : StackGresPoolingConfigSpec(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec) StackGresPoolingConfigPgBouncerPgbouncerIni(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerPgbouncerIni) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) StackGresPoolingConfigPgBouncer(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer) ImmutableList(com.google.common.collect.ImmutableList) AddOperation(com.github.fge.jsonpatch.AddOperation) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation)

Aggregations

StackGresPoolingConfig (io.stackgres.common.crd.sgpooling.StackGresPoolingConfig)7 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)2 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)2 StackGresPoolingConfigPgBouncer (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer)2 StackGresPoolingConfigPgBouncerPgbouncerIni (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerPgbouncerIni)2 StackGresPoolingConfigSpec (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec)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 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 Secret (io.fabric8.kubernetes.api.model.Secret)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)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 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)1