Search in sources :

Example 1 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class DistributedLogsClusterReconciliator method reconcile.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "False positives")
protected ReconciliationResult<Boolean> reconcile(KubernetesClient client, StackGresDistributedLogsContext context) throws Exception {
    StackGresDistributedLogs distributedLogs = context.getDistributedLogs();
    if (distributedLogs.getStatus() == null || !isPatroniReady(context)) {
        LOGGER.warn("Waiting for distributedlogs cluster to become ready...");
        return new ReconciliationResult<>(false);
    }
    final ImmutableList.Builder<Exception> exceptions = ImmutableList.builder();
    boolean statusUpdated = false;
    for (StackGresDistributedLogsStatusCluster cluster : distributedLogs.getStatus().getConnectedClusters()) {
        String database = FluentdUtil.databaseName(cluster.getNamespace(), cluster.getName());
        try {
            if (!databaseManager.existsDatabase(context, database)) {
                LOGGER.info("Creating database {}", database);
                databaseManager.createDatabase(context, database);
            }
        } catch (Exception ex) {
            exceptions.add(ex);
            handleException(client, distributedLogs, cluster, ex);
            continue;
        }
        String retention = cluster.getConfig().getRetention();
        if (!Optional.of(distributedLogs.getStatus().getDatabases()).flatMap(databases -> databases.stream().filter(databaseStatus -> databaseStatus.getName().equals(database)).findAny()).map(StackGresDistributedLogsStatusDatabase::getRetention).map(currentRetention -> Objects.equals(retention, currentRetention)).orElse(false)) {
            for (String table : Seq.of(Tables.values()).map(Tables::getTableName)) {
                LOGGER.info("Updating retention window for database {} and table to {}", database, retention);
                try {
                    databaseManager.updateRetention(context, database, retention, table);
                } catch (Exception ex) {
                    exceptions.add(ex);
                    handleException(client, distributedLogs, cluster, ex);
                    continue;
                }
            }
        }
        if (retention != null) {
            for (String table : Seq.of(Tables.values()).map(Tables::getTableName)) {
                try {
                    databaseManager.reconcileRetention(context, database, retention, table).stream().forEach(output -> LOGGER.info("Reconcile retention for database {} and table {}: {}", database, table, output));
                } catch (Exception ex) {
                    exceptions.add(ex);
                    handleException(client, distributedLogs, cluster, ex);
                    continue;
                }
            }
        }
        statusUpdated = statusUpdated || updateStatus(distributedLogs, database, retention);
    }
    String fluentdConfigHash = configManager.getFluentdConfigHash();
    if (!Objects.equals(distributedLogs.getStatus().getFluentdConfigHash(), fluentdConfigHash)) {
        LOGGER.info("Reloading fluentd configuration");
        configManager.reloadFluentdConfiguration();
        distributedLogs.getStatus().setFluentdConfigHash(fluentdConfigHash);
    }
    return new ReconciliationResult<>(statusUpdated, exceptions.build());
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ReconciliationResult(io.stackgres.operatorframework.reconciliation.ReconciliationResult) ImmutableList(com.google.common.collect.ImmutableList) Tables(io.stackgres.common.distributedlogs.Tables) StackGresDistributedLogsStatusCluster(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsStatusCluster) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class PatroniContainer method getContainer.

@Override
public Container getContainer(DistributedLogsContainerContext context) {
    final StackGresDistributedLogs cluster = context.getDistributedLogsContext().getSource();
    final String pgVersion = StackGresDistributedLogsUtil.getPostgresVersion();
    final String patroniImageName = StackGresComponent.PATRONI.findImageName(StackGresComponent.LATEST, ImmutableMap.of(StackGresComponent.POSTGRESQL, pgVersion));
    final String startScript = "/start-patroni.sh";
    return new ContainerBuilder().withName(StackgresClusterContainers.PATRONI).withImage(patroniImageName).withCommand("/bin/sh", "-ex", PatroniEnvPaths.LOCAL_BIN_PATH.getPath() + startScript).withImagePullPolicy("IfNotPresent").withPorts(new ContainerPortBuilder().withName(PatroniConfigMap.POSTGRES_PORT_NAME).withProtocol("TCP").withContainerPort(EnvoyUtil.PG_PORT).build(), new ContainerPortBuilder().withName(PatroniConfigMap.POSTGRES_REPLICATION_PORT_NAME).withProtocol("TCP").withContainerPort(EnvoyUtil.PG_REPL_ENTRY_PORT).build(), new ContainerPortBuilder().withName(PATRONI_RESTAPI_PORT_NAME).withProtocol("TCP").withContainerPort(EnvoyUtil.PATRONI_ENTRY_PORT).build()).withVolumeMounts(getVolumeMounts(context)).withEnvFrom(new EnvFromSourceBuilder().withConfigMapRef(new ConfigMapEnvSourceBuilder().withName(cluster.getMetadata().getName()).build()).build()).withEnv(getEnvVar(context)).withLivenessProbe(new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/cluster").withPort(new IntOrString(EnvoyUtil.PATRONI_ENTRY_PORT)).withScheme("HTTP").build()).withInitialDelaySeconds(15).withPeriodSeconds(20).withFailureThreshold(6).build()).withReadinessProbe(new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/read-only").withPort(new IntOrString(EnvoyUtil.PATRONI_ENTRY_PORT)).withScheme("HTTP").build()).withInitialDelaySeconds(5).withPeriodSeconds(10).build()).build();
}
Also used : HTTPGetActionBuilder(io.fabric8.kubernetes.api.model.HTTPGetActionBuilder) ProbeBuilder(io.fabric8.kubernetes.api.model.ProbeBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) EnvFromSourceBuilder(io.fabric8.kubernetes.api.model.EnvFromSourceBuilder) ConfigMapEnvSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapEnvSourceBuilder)

Example 3 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class Fluentd method generateResource.

@Override
public Stream<HasMetadata> generateResource(StackGresDistributedLogsContext context) {
    final StackGresDistributedLogs cluster = context.getSource();
    final String namespace = cluster.getMetadata().getNamespace();
    final Map<String, String> labels = labelFactory.patroniPrimaryLabels(cluster);
    final Service service = new ServiceBuilder().withNewMetadata().withNamespace(namespace).withName(FluentdUtil.serviceName(cluster)).withLabels(labels).endMetadata().withNewSpec().withSelector(labels).withPorts(new ServicePortBuilder().withProtocol("TCP").withName(FluentdUtil.FORWARD_PORT_NAME).withPort(FluentdUtil.FORWARD_PORT).withTargetPort(new IntOrString(FluentdUtil.FORWARD_PORT_NAME)).build()).withType("ClusterIP").endSpec().build();
    return Seq.of(service);
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 4 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class PatroniInitScriptConfigMap method buildSource.

@NotNull
public HasMetadata buildSource(StackGresDistributedLogsContext context) {
    final StackGresDistributedLogs cluster = context.getSource();
    String data = Unchecked.supplier(() -> Resources.asCharSource(PatroniInitScriptConfigMap.class.getResource("/distributed-logs-template.sql"), StandardCharsets.UTF_8).read()).get();
    return new ConfigMapBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(cluster)).withLabels(labelFactory.patroniClusterLabels(cluster)).endMetadata().withData(ImmutableMap.of("distributed-logs-template.sql", data)).build();
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class PatroniRole method createServiceAccount.

/**
 * Create the ServiceAccount for patroni associated to the cluster.
 */
private ServiceAccount createServiceAccount(StackGresDistributedLogsContext context) {
    final StackGresDistributedLogs cluster = context.getSource();
    final Map<String, String> labels = labelFactory.clusterLabels(cluster);
    final String serviceAccountName = roleName(context);
    final String serviceAccountNamespace = cluster.getMetadata().getNamespace();
    return new ServiceAccountBuilder().withNewMetadata().withName(serviceAccountName).withNamespace(serviceAccountNamespace).withLabels(labels).endMetadata().build();
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder)

Aggregations

StackGresDistributedLogs (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs)48 NotNull (org.jetbrains.annotations.NotNull)14 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)13 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)12 Inject (javax.inject.Inject)11 Test (org.junit.jupiter.api.Test)11 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 List (java.util.List)9 Map (java.util.Map)9 Volume (io.fabric8.kubernetes.api.model.Volume)8 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)7 Container (io.fabric8.kubernetes.api.model.Container)7 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)7 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)7 StackGresUtil (io.stackgres.common.StackGresUtil)7 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ImmutableMap (com.google.common.collect.ImmutableMap)6