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