Search in sources :

Example 1 with StackGresClusterPostgresServices

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

the class DefaultPostgresServicesMutator method mutate.

@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
    final StackGresClusterPostgresServices postgresServices = review.getRequest().getObject().getSpec().getPostgresServices();
    if (review.getRequest().getOperation() == Operation.CREATE || review.getRequest().getOperation() == Operation.UPDATE) {
        if (postgresServices != null) {
            if (postgresServices.getPrimary() != null) {
                if (postgresServices.getPrimary().getEnabled() == null) {
                    postgresServices.getPrimary().setEnabled(Boolean.TRUE);
                }
                if (postgresServices.getPrimary().getType() == null) {
                    postgresServices.getPrimary().setType(StackGresPostgresServiceType.CLUSTER_IP.toString());
                }
            } else {
                postgresServices.setPrimary(createNewPostgresService());
            }
            if (postgresServices.getReplicas() != null) {
                if (postgresServices.getReplicas().getEnabled() == null) {
                    postgresServices.getReplicas().setEnabled(Boolean.TRUE);
                }
                if (postgresServices.getReplicas().getType() == null) {
                    postgresServices.getReplicas().setType(StackGresPostgresServiceType.CLUSTER_IP.toString());
                }
            } else {
                postgresServices.setReplicas(createNewPostgresService());
            }
            JsonNode target = JSON_MAPPER.valueToTree(postgresServices);
            ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
            operations.add(applyReplaceValue(postgresServicesPointer, target));
            return operations.build();
        } else {
            StackGresClusterPostgresServices pgServices = new StackGresClusterPostgresServices();
            pgServices.setPrimary(createNewPostgresService());
            pgServices.setReplicas(createNewPostgresService());
            JsonNode target = JSON_MAPPER.valueToTree(pgServices);
            ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
            operations.add(applyAddValue(postgresServicesPointer, target));
            return operations.build();
        }
    }
    return ImmutableList.of();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation)

Example 2 with StackGresClusterPostgresServices

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

the class PatroniServices method generateResource.

/**
 * Create the Services associated with the cluster.
 */
@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
    final StackGresCluster cluster = context.getSource();
    final String namespace = cluster.getMetadata().getNamespace();
    final Map<String, String> clusterLabels = labelFactory.clusterLabels(cluster);
    Service config = createConfigService(namespace, configName(context), clusterLabels);
    Service rest = createPatroniRestService(context);
    Seq<HasMetadata> services = Seq.of(config, rest);
    boolean isPrimaryServiceEnabled = Optional.of(cluster).map(StackGresCluster::getSpec).map(StackGresClusterSpec::getPostgresServices).map(StackGresClusterPostgresServices::getPrimary).map(StackGresPostgresService::getEnabled).orElse(true);
    if (isPrimaryServiceEnabled) {
        Service patroni = createPatroniService(context);
        services = services.append(patroni);
        Service primary = createPrimaryService(context);
        services = services.append(primary);
    }
    boolean isReplicaServiceEnabled = Optional.of(cluster).map(StackGresCluster::getSpec).map(StackGresClusterSpec::getPostgresServices).map(StackGresClusterPostgresServices::getReplicas).map(StackGresPostgresService::getEnabled).orElse(true);
    if (isReplicaServiceEnabled) {
        Service replicas = createReplicaService(context);
        services = services.append(replicas);
    }
    return services;
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)

Example 3 with StackGresClusterPostgresServices

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

the class PatroniServices method createPatroniService.

private Service createPatroniService(StackGresClusterContext context) {
    StackGresCluster cluster = context.getSource();
    final Map<String, String> primaryLabels = labelFactory.patroniPrimaryLabels(cluster);
    Map<String, String> annotations = Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getMetadata).map(StackGresClusterSpecMetadata::getAnnotations).map(StackGresClusterSpecAnnotations::getPrimaryService).orElse(Map.of());
    String serviceType = Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getPostgresServices).map(StackGresClusterPostgresServices::getPrimary).map(StackGresPostgresService::getType).orElse(StackGresPostgresServiceType.CLUSTER_IP.toString());
    return new ServiceBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context)).withLabels(primaryLabels).withAnnotations(annotations).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 : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 4 with StackGresClusterPostgresServices

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

the class DefaultPostgresServicesMutatorTest method clusterWithPostgresServiceNoType_shouldSetClusterIP.

@Test
void clusterWithPostgresServiceNoType_shouldSetClusterIP() {
    StackGresPostgresService primary = new StackGresPostgresService();
    StackGresPostgresService replica = new StackGresPostgresService();
    primary.setEnabled(Boolean.TRUE);
    replica.setEnabled(Boolean.FALSE);
    setPostgresServices(primary, replica);
    StackGresCluster actualCluster = mutate(review);
    StackGresClusterPostgresServices pgServices = actualCluster.getSpec().getPostgresServices();
    assertEquals(Boolean.TRUE, pgServices.getPrimary().getEnabled());
    assertEquals("ClusterIP", pgServices.getPrimary().getType());
    assertEquals(Boolean.FALSE, pgServices.getReplicas().getEnabled());
    assertEquals("ClusterIP", pgServices.getReplicas().getType());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) Test(org.junit.jupiter.api.Test)

Example 5 with StackGresClusterPostgresServices

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

the class DefaultPostgresServicesMutatorTest method clusterWithPostgresServiceNoReplicas_shouldSetValue.

@Test
void clusterWithPostgresServiceNoReplicas_shouldSetValue() {
    StackGresPostgresService primary = new StackGresPostgresService();
    primary.setEnabled(Boolean.FALSE);
    primary.setType("LoadBalancing");
    setPostgresServices(primary, null);
    StackGresCluster actualCluster = mutate(review);
    StackGresClusterPostgresServices pgServices = actualCluster.getSpec().getPostgresServices();
    assertEquals(Boolean.FALSE, pgServices.getPrimary().getEnabled());
    assertEquals("LoadBalancing", pgServices.getPrimary().getType());
    assertEquals(Boolean.TRUE, pgServices.getReplicas().getEnabled());
    assertEquals("ClusterIP", pgServices.getReplicas().getType());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresClusterPostgresServices (io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)18 StackGresPostgresService (io.stackgres.common.crd.postgres.service.StackGresPostgresService)10 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)9 Test (org.junit.jupiter.api.Test)8 StackGresClusterSpecMetadata (io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)4 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)2 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)2 PostgresService (io.stackgres.apiweb.app.postgres.service.PostgresService)2 ClusterConfiguration (io.stackgres.apiweb.dto.cluster.ClusterConfiguration)2 ClusterInitData (io.stackgres.apiweb.dto.cluster.ClusterInitData)2 ClusterPostgresServices (io.stackgres.apiweb.dto.cluster.ClusterPostgresServices)2 ClusterSpecAnnotations (io.stackgres.apiweb.dto.cluster.ClusterSpecAnnotations)2 ClusterSpecLabels (io.stackgres.apiweb.dto.cluster.ClusterSpecLabels)2 ClusterSpecMetadata (io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata)2 ClusterSsl (io.stackgres.apiweb.dto.cluster.ClusterSsl)2 ClusterPodSchedulingConverter (io.stackgres.apiweb.transformer.converter.cluster.ClusterPodSchedulingConverter)2 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)2 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)2 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)2