Search in sources :

Example 1 with StackGresPoolingConfigPgBouncer

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer 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)

Example 2 with StackGresPoolingConfigPgBouncer

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer 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;
}
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)

Example 3 with StackGresPoolingConfigPgBouncer

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer 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;
}
Also used : StackGresPoolingConfigSpec(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec) PoolingConfigPgBouncer(io.stackgres.apiweb.dto.pooling.PoolingConfigPgBouncer) PgBouncerIniParameter(io.stackgres.apiweb.dto.pooling.PgBouncerIniParameter) HashMap(java.util.HashMap) StackGresPoolingConfigStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigStatus) Seq(org.jooq.lambda.Seq) PoolingConfigPgBouncerStatus(io.stackgres.apiweb.dto.pooling.PoolingConfigPgBouncerStatus) StackGresPoolingConfigPgBouncer(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer) Matcher(java.util.regex.Matcher) Tuple3(org.jooq.lambda.tuple.Tuple3) PoolingConfigDto(io.stackgres.apiweb.dto.pooling.PoolingConfigDto) Map(java.util.Map) SubnodeConfiguration(org.apache.commons.configuration2.SubnodeConfiguration) StackGresPoolingConfigSpec(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec) INIConfiguration(org.apache.commons.configuration2.INIConfiguration) Iterator(java.util.Iterator) StackGresPoolingConfigPgBouncerPgbouncerIni(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerPgbouncerIni) StringWriter(java.io.StringWriter) Set(java.util.Set) IOException(java.io.IOException) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) Collectors(java.util.stream.Collectors) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) List(java.util.List) StringReader(java.io.StringReader) Entry(java.util.Map.Entry) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Pattern(java.util.regex.Pattern) PoolingConfigStatus(io.stackgres.apiweb.dto.pooling.PoolingConfigStatus) PoolingConfigSpec(io.stackgres.apiweb.dto.pooling.PoolingConfigSpec) StackGresPoolingConfigPgBouncerPgbouncerIni(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerPgbouncerIni) SubnodeConfiguration(org.apache.commons.configuration2.SubnodeConfiguration) StackGresPoolingConfigSpec(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec) PoolingConfigSpec(io.stackgres.apiweb.dto.pooling.PoolingConfigSpec) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) INIConfiguration(org.apache.commons.configuration2.INIConfiguration) PoolingConfigPgBouncer(io.stackgres.apiweb.dto.pooling.PoolingConfigPgBouncer) StackGresPoolingConfigPgBouncer(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer) StackGresPoolingConfigPgBouncer(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) StringReader(java.io.StringReader)

Example 4 with StackGresPoolingConfigPgBouncer

use of io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer 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;
}
Also used : INIConfiguration(org.apache.commons.configuration2.INIConfiguration) StackGresPoolingConfigSpec(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec) PoolingConfigSpec(io.stackgres.apiweb.dto.pooling.PoolingConfigSpec) PoolingConfigPgBouncer(io.stackgres.apiweb.dto.pooling.PoolingConfigPgBouncer) StackGresPoolingConfigPgBouncer(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer) StringWriter(java.io.StringWriter) StackGresPoolingConfigPgBouncer(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) IOException(java.io.IOException)

Aggregations

StackGresPoolingConfigPgBouncer (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncer)4 StackGresPoolingConfigSpec (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigSpec)4 StackGresPoolingConfig (io.stackgres.common.crd.sgpooling.StackGresPoolingConfig)3 StackGresPoolingConfigPgBouncerPgbouncerIni (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerPgbouncerIni)3 PoolingConfigPgBouncer (io.stackgres.apiweb.dto.pooling.PoolingConfigPgBouncer)2 PoolingConfigSpec (io.stackgres.apiweb.dto.pooling.PoolingConfigSpec)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 INIConfiguration (org.apache.commons.configuration2.INIConfiguration)2 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)2 AddOperation (com.github.fge.jsonpatch.AddOperation)1 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)1 ImmutableList (com.google.common.collect.ImmutableList)1 PgBouncerIniParameter (io.stackgres.apiweb.dto.pooling.PgBouncerIniParameter)1 PoolingConfigDto (io.stackgres.apiweb.dto.pooling.PoolingConfigDto)1 PoolingConfigPgBouncerStatus (io.stackgres.apiweb.dto.pooling.PoolingConfigPgBouncerStatus)1 PoolingConfigStatus (io.stackgres.apiweb.dto.pooling.PoolingConfigStatus)1 StackGresPoolingConfigStatus (io.stackgres.common.crd.sgpooling.StackGresPoolingConfigStatus)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1