Search in sources :

Example 11 with StackGresDistributedLogs

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

the class PatroniServices method createPatroniService.

private Service createPatroniService(StackGresDistributedLogsContext context) {
    StackGresDistributedLogs cluster = context.getSource();
    final Map<String, String> primaryLabels = labelFactory.patroniPrimaryLabels(cluster);
    String serviceType = Optional.ofNullable(cluster.getSpec()).map(StackGresDistributedLogsSpec::getPostgresServices).map(StackGresPostgresServices::getPrimary).map(StackGresPostgresService::getType).orElse(CLUSTER_IP.toString());
    return new ServiceBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context)).withLabels(primaryLabels).endMetadata().withNewSpec().withPorts(new ServicePortBuilder().withProtocol("TCP").withName(PatroniConfigMap.POSTGRES_PORT_NAME).withPort(PatroniUtil.POSTGRES_SERVICE_PORT).withTargetPort(new IntOrString(PatroniConfigMap.POSTGRES_PORT_NAME)).build(), new ServicePortBuilder().withProtocol("TCP").withName(PatroniConfigMap.POSTGRES_REPLICATION_PORT_NAME).withPort(PatroniUtil.REPLICATION_SERVICE_PORT).withTargetPort(new IntOrString(PatroniConfigMap.POSTGRES_REPLICATION_PORT_NAME)).build()).withType(serviceType).endSpec().build();
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) StackGresPostgresServices(io.stackgres.common.crd.postgres.service.StackGresPostgresServices) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 12 with StackGresDistributedLogs

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

the class PatroniConfigMap method buildSource.

@NotNull
public HasMetadata buildSource(StackGresDistributedLogsContext context) {
    final StackGresDistributedLogs cluster = context.getSource();
    final String pgVersion = "12.6";
    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 = "0.0.0.0";
    final int pgRawPort = EnvoyUtil.PG_PORT;
    final int pgPort = EnvoyUtil.PG_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", PatroniEnvPaths.PG_DATA_PATH.getPath());
    data.put("PATRONI_POSTGRESQL_BIN_DIR", "/usr/lib/postgresql/" + pgVersion + "/bin");
    data.put("PATRONI_POSTGRES_UNIX_SOCKET_DIRECTORY", PatroniEnvPaths.PG_RUN_PATH.getPath());
    if (PATRONI_LOGGER.isTraceEnabled()) {
        data.put("PATRONI_LOG_LEVEL", "DEBUG");
    }
    data.put("PATRONI_SCRIPTS", "1");
    return new ConfigMapBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context)).withLabels(value).endMetadata().withData(StackGresUtil.addMd5Sum(data)).build();
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NotNull(org.jetbrains.annotations.NotNull)

Example 13 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 startScript = "/start-patroni.sh";
    return new ContainerBuilder().withName(StackgresClusterContainers.PATRONI).withImage(IMAGE_NAME).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(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 14 with StackGresDistributedLogs

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

the class DistributedLogsRequiredResourcesGeneratorTest method get095Cluster.

private StackGresDistributedLogs get095Cluster() {
    StackGresDistributedLogs distributedLogs095 = JsonUtil.readFromJson("distributedlogs/0.9.json", StackGresDistributedLogs.class);
    distributedLogs095.getMetadata().setNamespace(randomNamespace);
    distributedLogs095.getMetadata().setName(randomName);
    distributedLogs095.getMetadata().setUid(clusterUid);
    return distributedLogs095;
}
Also used : StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs)

Example 15 with StackGresDistributedLogs

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

the class DistributedLogsRequiredResourcesGeneratorTest method givenADistributedLogsIn095_shouldGenerateAStsCompatibleWithThatVersion.

@Test
void givenADistributedLogsIn095_shouldGenerateAStsCompatibleWithThatVersion() {
    StackGresDistributedLogs distributedLogs095 = get095Cluster();
    StatefulSet deployedS = get095Sts(distributedLogs095);
    StatefulSet generatedSts = generator.getRequiredResources(distributedLogs095).stream().filter(r -> r.getKind().equals("StatefulSet")).map(r -> (StatefulSet) r).findFirst().orElseThrow();
    /*
     * Sorting volumes and volume mounts since it has no effect on the stability of the containers,
     * but it could throw false failures if unsorted //
     */
    sortVolumes(deployedS);
    sortVolumes(generatedSts);
    assertTrue(stsComparator.isTheSameResource(generatedSts, deployedS));
    assertTrue(stsComparator.isResourceContentEqual(generatedSts, deployedS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Seq(org.jooq.lambda.Seq) Mockito.lenient(org.mockito.Mockito.lenient) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) StringUtils(io.stackgres.testutil.StringUtils) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Volume(io.fabric8.kubernetes.api.model.Volume) InjectMock(io.quarkus.test.junit.mockito.InjectMock) ImmutableMap(com.google.common.collect.ImmutableMap) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) ReconciliationScope(io.stackgres.operator.conciliation.ReconciliationScope) IOException(java.io.IOException) StackGresUtil(io.stackgres.common.StackGresUtil) Reader(java.io.Reader) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StandardCharsets(java.nio.charset.StandardCharsets) DefaultComparator(io.stackgres.operator.conciliation.comparator.DefaultComparator) JsonUtil(io.stackgres.testutil.JsonUtil) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) Comparator(java.util.Comparator) CommonDefinition(io.stackgres.common.crd.CommonDefinition) NotNull(org.jetbrains.annotations.NotNull) InputStream(java.io.InputStream) CustomResource(io.fabric8.kubernetes.client.CustomResource) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

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