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);
}
}
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;
}
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);
}
}
}
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;
}
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();
}
Aggregations