Search in sources :

Example 21 with StackGresClusterInitData

use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.

the class ClusterTransformer method getCustomResourceSpec.

private StackGresClusterSpec getCustomResourceSpec(ClusterSpec source) {
    if (source == null) {
        return null;
    }
    StackGresClusterSpec transformation = new StackGresClusterSpec();
    transformation.setPostgres(new StackGresClusterPostgres());
    transformation.getPostgres().setVersion(source.getPostgres().getVersion());
    transformation.getPostgres().setExtensions(Optional.ofNullable(source.getPostgres().getExtensions()).stream().flatMap(List::stream).map(this::getCustomResourceExtension).collect(ImmutableList.toImmutableList()));
    final ClusterSsl sourceClusterSsl = source.getPostgres().getSsl();
    if (sourceClusterSsl != null) {
        transformation.getPostgres().setSsl(new StackGresClusterSsl());
        transformation.getPostgres().getSsl().setEnabled(sourceClusterSsl.getEnabled());
        transformation.getPostgres().getSsl().setCertificateSecretKeySelector(sourceClusterSsl.getCertificateSecretKeySelector());
        transformation.getPostgres().getSsl().setPrivateKeySecretKeySelector(sourceClusterSsl.getPrivateKeySecretKeySelector());
    }
    final ClusterConfiguration sourceClusterConfiguration = source.getConfigurations();
    if (sourceClusterConfiguration != null) {
        transformation.setConfiguration(new StackGresClusterConfiguration());
        transformation.getConfiguration().setBackupConfig(source.getConfigurations().getSgBackupConfig());
        transformation.getConfiguration().setConnectionPoolingConfig(source.getConfigurations().getSgPoolingConfig());
        transformation.getConfiguration().setPostgresConfig(source.getConfigurations().getSgPostgresConfig());
    }
    transformation.setInstances(source.getInstances());
    transformation.setNonProduction(getCustomResourceNonProduction(source.getNonProduction()));
    transformation.setPrometheusAutobind(source.getPrometheusAutobind());
    transformation.setResourceProfile(source.getSgInstanceProfile());
    final ClusterSpecMetadata specMetadata = source.getMetadata();
    if (specMetadata != null) {
        transformation.setMetadata(new StackGresClusterSpecMetadata());
        final ClusterSpecAnnotations sourceAnnotations = specMetadata.getAnnotations();
        if (sourceAnnotations != null) {
            StackGresClusterSpecAnnotations targetAnnotations = new StackGresClusterSpecAnnotations();
            targetAnnotations.setAllResources(sourceAnnotations.getAllResources());
            targetAnnotations.setClusterPods(sourceAnnotations.getClusterPods());
            targetAnnotations.setServices(sourceAnnotations.getServices());
            targetAnnotations.setPrimaryService(sourceAnnotations.getPrimaryService());
            targetAnnotations.setReplicasService(sourceAnnotations.getReplicasService());
            transformation.getMetadata().setAnnotations(targetAnnotations);
        }
        final ClusterSpecLabels sourceLabels = specMetadata.getLabels();
        if (sourceLabels != null) {
            StackGresClusterSpecLabels targetLabels = new StackGresClusterSpecLabels();
            targetLabels.setClusterPods(sourceLabels.getClusterPods());
            transformation.getMetadata().setLabels(targetLabels);
        }
    }
    final ClusterPostgresServices sourcePostgresServices = source.getPostgresServices();
    if (sourcePostgresServices != null) {
        transformation.setPostgresServices(new StackGresClusterPostgresServices());
        final StackGresClusterPostgresServices targetPostgresService = transformation.getPostgresServices();
        final PostgresService sourcePrimaryService = sourcePostgresServices.getPrimary();
        if (sourcePrimaryService != null) {
            targetPostgresService.setPrimary(new StackGresPostgresService());
            final StackGresPostgresService targetPrimaryService = targetPostgresService.getPrimary();
            targetPrimaryService.setType(sourcePrimaryService.getType());
            targetPrimaryService.setEnabled(sourcePrimaryService.getEnabled());
        }
        final PostgresService sourceReplicaService = sourcePostgresServices.getReplicas();
        if (sourceReplicaService != null) {
            targetPostgresService.setReplicas(new StackGresPostgresService());
            final StackGresPostgresService targetReplicaService = targetPostgresService.getReplicas();
            targetReplicaService.setEnabled(sourceReplicaService.getEnabled());
            targetReplicaService.setType(sourceReplicaService.getType());
        }
    }
    final ClusterInitData sourceInitData = source.getInitData();
    if (sourceInitData != null) {
        final StackGresClusterInitData targetInitData = new StackGresClusterInitData();
        transformation.setInitData(targetInitData);
        if (sourceInitData.getRestore() != null) {
            targetInitData.setRestore(getCustomResourceRestore(sourceInitData.getRestore()));
        }
        if (sourceInitData.getScripts() != null) {
            targetInitData.setScripts(getCustomResourceScripts(sourceInitData.getScripts()));
        }
    }
    final StackGresClusterPod targetPod = new StackGresClusterPod();
    transformation.setPod(targetPod);
    targetPod.setPersistentVolume(new StackGresPodPersistentVolume());
    targetPod.getPersistentVolume().setStorageClass(source.getPods().getPersistentVolume().getStorageClass());
    targetPod.getPersistentVolume().setSize(source.getPods().getPersistentVolume().getSize());
    targetPod.setDisableConnectionPooling(source.getPods().getDisableConnectionPooling());
    targetPod.setDisableMetricsExporter(source.getPods().getDisableMetricsExporter());
    targetPod.setDisablePostgresUtil(source.getPods().getDisablePostgresUtil());
    targetPod.setScheduling(Optional.ofNullable(source.getPods().getScheduling()).map(sourceScheduling -> {
        return new ClusterPodSchedulingConverter().to(sourceScheduling);
    }).orElse(null));
    transformation.setDistributedLogs(getCustomResourceDistributedLogs(source.getDistributedLogs()));
    return transformation;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ClusterPodSchedulingConverter(io.stackgres.apiweb.transformer.converter.cluster.ClusterPodSchedulingConverter) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) ClusterPostgresServices(io.stackgres.apiweb.dto.cluster.ClusterPostgresServices) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ClusterConfiguration(io.stackgres.apiweb.dto.cluster.ClusterConfiguration) ClusterSpecAnnotations(io.stackgres.apiweb.dto.cluster.ClusterSpecAnnotations) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) PostgresService(io.stackgres.apiweb.app.postgres.service.PostgresService) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) ClusterSpecMetadata(io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) ClusterSsl(io.stackgres.apiweb.dto.cluster.ClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) ClusterSpecLabels(io.stackgres.apiweb.dto.cluster.ClusterSpecLabels) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)

Example 22 with StackGresClusterInitData

use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.

the class PatroniConfigMap method buildSource.

@NotNull
public HasMetadata buildSource(StackGresClusterContext context) {
    final StackGresCluster cluster = context.getSource();
    final String pgVersion = StackGresComponent.POSTGRESQL.findVersion(cluster.getSpec().getPostgres().getVersion());
    final String patroniLabels;
    final Map<String, String> value = labelFactory.patroniClusterLabels(cluster);
    try {
        patroniLabels = objectMapper.writeValueAsString(value);
    } catch (JsonProcessingException ex) {
        throw new RuntimeException(ex);
    }
    // NOPMD
    final String pgHost = "127.0.0.1";
    final int pgRawPort = EnvoyUtil.PG_REPL_ENTRY_PORT;
    final int pgPort = EnvoyUtil.PG_ENTRY_PORT;
    Map<String, String> data = new HashMap<>();
    data.put("PATRONI_SCOPE", labelFactory.clusterScope(cluster));
    data.put("PATRONI_KUBERNETES_SCOPE_LABEL", labelFactory.labelMapper().clusterScopeKey());
    data.put("PATRONI_KUBERNETES_LABELS", patroniLabels);
    data.put("PATRONI_KUBERNETES_USE_ENDPOINTS", "true");
    data.put("PATRONI_KUBERNETES_PORTS", getKubernetesPorts(pgPort, pgRawPort));
    data.put("PATRONI_SUPERUSER_USERNAME", "postgres");
    data.put("PATRONI_REPLICATION_USERNAME", "replicator");
    data.put("PATRONI_POSTGRESQL_LISTEN", pgHost + ":" + EnvoyUtil.PG_PORT);
    data.put("PATRONI_POSTGRESQL_CONNECT_ADDRESS", "${PATRONI_KUBERNETES_POD_IP}:" + pgRawPort);
    data.put("PATRONI_RESTAPI_LISTEN", "0.0.0.0:8008");
    data.put("PATRONI_POSTGRESQL_DATA_DIR", ClusterStatefulSetPath.PG_DATA_PATH.path());
    data.put("PATRONI_POSTGRESQL_BIN_DIR", "/usr/lib/postgresql/" + pgVersion + "/bin");
    data.put("PATRONI_POSTGRES_UNIX_SOCKET_DIRECTORY", ClusterStatefulSetPath.PG_RUN_PATH.path());
    if (Optional.ofNullable(cluster.getSpec().getDistributedLogs()).map(StackGresClusterDistributedLogs::getDistributedLogs).isPresent()) {
        data.put("PATRONI_LOG_DIR", ClusterStatefulSetPath.PG_LOG_PATH.path());
        data.put("PATRONI_LOG_FILE_NUM", "2");
        data.put("PATRONI_LOG_FILE_SIZE", String.valueOf(PATRONI_LOG_FILE_SIZE));
    }
    if (PATRONI_LOGGER.isTraceEnabled()) {
        data.put("PATRONI_LOG_LEVEL", "DEBUG");
    }
    data.put("PATRONI_SCRIPTS", Optional.ofNullable(cluster.getSpec().getInitData()).map(StackGresClusterInitData::getScripts).map(List::size).map(String::valueOf).orElse("0"));
    return new ConfigMapBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context)).withLabels(value).endMetadata().withData(StackGresUtil.addMd5Sum(data)).build();
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)22 StackGresClusterScriptEntry (io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)16 StackGresClusterScriptFrom (io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom)12 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)12 ConstraintValidationTest (io.stackgres.operator.validation.ConstraintValidationTest)12 Test (org.junit.jupiter.api.Test)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 ConfigMapKeySelector (io.stackgres.common.crd.ConfigMapKeySelector)6 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)6 SecretKeySelector (io.stackgres.common.crd.SecretKeySelector)5 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)5 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)5 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)5 StackGresClusterRestore (io.stackgres.common.crd.sgcluster.StackGresClusterRestore)5 ClusterConfiguration (io.stackgres.apiweb.dto.cluster.ClusterConfiguration)4 ClusterInitData (io.stackgres.apiweb.dto.cluster.ClusterInitData)4 ClusterPod (io.stackgres.apiweb.dto.cluster.ClusterPod)3 ClusterPodPersistentVolume (io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume)3 ClusterSpecLabels (io.stackgres.apiweb.dto.cluster.ClusterSpecLabels)3 ClusterSpecMetadata (io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata)3