use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec 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();
}
use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec in project stackgres by ongres.
the class DefaultPoolingFactory method buildResource.
@Override
StackGresPoolingConfig buildResource(String namespace) {
StackGresPoolingConfig config = new StackGresPoolingConfig();
config.getMetadata().setName(generateDefaultName());
config.getMetadata().setNamespace(namespace);
StackGresPoolingConfigSpec spec = new StackGresPoolingConfigSpec();
final StackGresPoolingConfigPgBouncer pgBouncer = new StackGresPoolingConfigPgBouncer();
final StackGresPoolingConfigPgBouncerPgbouncerIni pgbouncerIni = new StackGresPoolingConfigPgBouncerPgbouncerIni();
pgbouncerIni.setParameters(getDefaultValues());
pgBouncer.setPgbouncerIni(pgbouncerIni);
spec.setPgBouncer(pgBouncer);
config.setSpec(spec);
return config;
}
use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec in project stackgres by ongres.
the class PoolingConfigTransformer method getCustomResourceSpec.
private StackGresPoolingConfigSpec getCustomResourceSpec(PoolingConfigSpec source) {
if (source == null) {
return null;
}
StackGresPoolingConfigSpec transformation = new StackGresPoolingConfigSpec();
transformation.setPgBouncer(new StackGresPoolingConfigPgBouncer());
Optional<PoolingConfigPgBouncer> pgbouncer = Optional.of(source).map(PoolingConfigSpec::getPgBouncer);
INIConfiguration iniConfiguration = new INIConfiguration();
pgbouncer.map(PoolingConfigPgBouncer::getParameters).ifPresent(ini -> {
try {
String cleanup = ini.lines().filter(p -> !p.startsWith("%include")).map(m -> {
if (m.indexOf('=') != -1) {
int indexOfEquals = m.indexOf('=');
return m.substring(0, indexOfEquals) + " = \"" + m.substring(indexOfEquals + 1) + "\"";
}
return m;
}).collect(Collectors.joining("\n"));
iniConfiguration.read(new StringReader(cleanup));
} catch (ConfigurationException | IOException cause) {
throw new RuntimeException("Could not read INI configuration", cause);
}
});
StackGresPoolingConfigPgBouncerPgbouncerIni pgbouncerIni = transformation.getPgBouncer().getPgbouncerIni();
if (pgbouncerIni == null) {
pgbouncerIni = new StackGresPoolingConfigPgBouncerPgbouncerIni();
transformation.getPgBouncer().setPgbouncerIni(pgbouncerIni);
}
for (String sectionName : iniConfiguration.getSections()) {
SubnodeConfiguration section = iniConfiguration.getSection(sectionName);
if (section != null) {
Iterator<String> keys = section.getKeys();
while (keys.hasNext()) {
String key = keys.next();
String value = section.getString(key);
if (value != null) {
if ("pgbouncer".equals(sectionName) || sectionName == null) {
if (pgbouncerIni.getParameters() == null) {
pgbouncerIni.setParameters(new HashMap<>());
}
pgbouncerIni.getParameters().put(key, value);
}
if ("databases".equals(sectionName)) {
if (pgbouncerIni.getDatabases() == null) {
pgbouncerIni.setDatabases(new HashMap<>());
}
var databases = new HashMap<String, String>();
Matcher matcher = PARAMETER_PATTERN.matcher(value);
while (matcher.find()) {
String keyItem = matcher.group(1);
String valueItem = matcher.group(2);
databases.put(keyItem, valueItem);
}
pgbouncerIni.getDatabases().put(key, databases);
}
if ("users".equals(sectionName)) {
if (pgbouncerIni.getUsers() == null) {
pgbouncerIni.setUsers(new HashMap<>());
}
var users = new HashMap<String, String>();
Matcher matcher = PARAMETER_PATTERN.matcher(value);
while (matcher.find()) {
final String keyItem = matcher.group(1);
final String valueItem = matcher.group(2);
users.put(keyItem, valueItem);
}
pgbouncerIni.getUsers().put(key, users);
}
}
}
}
}
return transformation;
}
use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec in project stackgres by ongres.
the class PoolingConfigTransformer method getResourceStatus.
private PoolingConfigStatus getResourceStatus(List<String> clusters, StackGresPoolingConfigStatus source, StackGresPoolingConfigSpec sourceSpec) {
PoolingConfigStatus transformation = new PoolingConfigStatus();
transformation.setClusters(clusters);
transformation.setPgBouncer(new PoolingConfigPgBouncerStatus());
transformation.getPgBouncer().setParameters(Seq.seq(sourceSpec.getPgBouncer().getPgbouncerIni().getParameters()).map(t -> t.concat(new PgBouncerIniParameter())).peek(t -> t.v3.setParameter(t.v1)).peek(t -> t.v3.setValue(t.v2)).map(Tuple3::v3).toList());
if (source != null && source.getPgBouncer() != null) {
transformation.getPgBouncer().setDefaultParameters(source.getPgBouncer().getDefaultParameters());
}
return transformation;
}
use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec in project stackgres by ongres.
the class PoolingConfigTransformer method getResourceSpec.
private PoolingConfigSpec getResourceSpec(StackGresPoolingConfigSpec source) {
PoolingConfigSpec transformation = new PoolingConfigSpec();
transformation.setPgBouncer(new PoolingConfigPgBouncer());
INIConfiguration ini = new INIConfiguration();
StackGresPoolingConfigPgBouncer pgBouncer = source.getPgBouncer();
if (pgBouncer.getPgbouncerIni().getDatabases() != null) {
addPropertiesToIni(source.getPgBouncer().getPgbouncerIni().getDatabases().entrySet(), ini, "databases");
}
if (pgBouncer.getPgbouncerIni().getUsers() != null) {
addPropertiesToIni(pgBouncer.getPgbouncerIni().getUsers().entrySet(), ini, "users");
}
if (pgBouncer.getPgbouncerIni().getParameters() != null) {
pgBouncer.getPgbouncerIni().getParameters().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
ini.addProperty("pgbouncer." + entry.getKey(), entry.getValue());
});
}
StringWriter stringWriter = new StringWriter();
try {
ini.write(stringWriter);
} catch (ConfigurationException | IOException cause) {
throw new RuntimeException("Could not write INI configuration", cause);
}
transformation.getPgBouncer().setParameters(stringWriter.toString());
return transformation;
}
Aggregations