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