Search in sources :

Example 1 with StackGresDistributedLogsSpec

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

the class DistributedLogsTransformer method getResourceSpec.

private DistributedLogsSpec getResourceSpec(StackGresDistributedLogsSpec source) {
    DistributedLogsSpec transformation = new DistributedLogsSpec();
    transformation.setPersistentVolume(getResourcePersistentVolume(source.getPersistentVolume()));
    transformation.setNonProduction(getResourceNonProduction(source.getNonProduction()));
    transformation.setPostgresServices(new DistributedLogsPostgresServicesConverter().from(source.getPostgresServices()));
    transformation.setScheduling(Optional.ofNullable(source.getScheduling()).map(sourcePodScheduling -> {
        DistributedLogsPodScheduling podScheduling = new DistributedLogsPodScheduling();
        podScheduling.setNodeSelector(sourcePodScheduling.getNodeSelector());
        podScheduling.setTolerations(sourcePodScheduling.getTolerations());
        return podScheduling;
    }).orElse(null));
    Optional.ofNullable(source.getMetadata()).map(StackGresDistributedLogsSpecMetadata::getAnnotations).ifPresent(sourceAnnotations -> {
        transformation.setMetadata(new DistributedLogsSpecMetadata());
        final DistributedLogsSpecAnnotations targetAnnotations = new DistributedLogsSpecAnnotations();
        transformation.getMetadata().setAnnotations(targetAnnotations);
        if (sourceAnnotations.getAllResources() != null) {
            targetAnnotations.setAllResources(sourceAnnotations.getAllResources());
        }
        if (sourceAnnotations.getPods() != null) {
            targetAnnotations.setPods(sourceAnnotations.getPods());
        }
        if (sourceAnnotations.getServices() != null) {
            targetAnnotations.setServices(sourceAnnotations.getServices());
        }
    });
    if (source.getToInstallPostgresExtensions() != null) {
        transformation.setToInstallPostgresExtensions(source.getToInstallPostgresExtensions().stream().map(this::getClusterInstalledExtension).collect(ImmutableList.toImmutableList()));
    }
    return transformation;
}
Also used : StackGresDistributedLogsSpecMetadata(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpecMetadata) DistributedLogsSpecMetadata(io.stackgres.apiweb.dto.distributedlogs.DistributedLogsSpecMetadata) DistributedLogsPostgresServicesConverter(io.stackgres.apiweb.transformer.distributedlogs.DistributedLogsPostgresServicesConverter) DistributedLogsSpecAnnotations(io.stackgres.apiweb.dto.distributedlogs.DistributedLogsSpecAnnotations) StackGresDistributedLogsSpecAnnotations(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpecAnnotations) DistributedLogsSpec(io.stackgres.apiweb.dto.distributedlogs.DistributedLogsSpec) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) DistributedLogsPodScheduling(io.stackgres.apiweb.dto.distributedlogs.DistributedLogsPodScheduling) StackGresDistributedLogsPodScheduling(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPodScheduling)

Example 2 with StackGresDistributedLogsSpec

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

the class StackGresDistributedLogsUtilTest method shouldBuildSgDistributedLogsPostgresServices_withOnlyReplicas.

@Test
void shouldBuildSgDistributedLogsPostgresServices_withOnlyReplicas() {
    StackGresDistributedLogsSpec spec = new StackGresDistributedLogsSpecFixture().withReplicasPostgresServices(replicas).build();
    StackGresClusterPostgresServices postgresServices = StackGresDistributedLogsUtil.buildPostgresServices(spec);
    assertNull(postgresServices.getPrimary());
    assertNotNull(postgresServices.getReplicas());
    assertTrue(replicas.equals(postgresServices.getReplicas()));
}
Also used : StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) StackGresDistributedLogsSpecFixture(io.stackgres.common.fixture.StackGresDistributedLogsSpecFixture) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) Test(org.junit.jupiter.api.Test)

Example 3 with StackGresDistributedLogsSpec

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

the class StackGresDistributedLogsUtilTest method shouldBuildSgDistributedLogsPostgresServices_withOnlyPrimary.

@Test
void shouldBuildSgDistributedLogsPostgresServices_withOnlyPrimary() {
    StackGresDistributedLogsSpec spec = new StackGresDistributedLogsSpecFixture().withPrimaryPostgresServices(primary).build();
    StackGresClusterPostgresServices postgresServices = StackGresDistributedLogsUtil.buildPostgresServices(spec);
    assertNotNull(postgresServices.getPrimary());
    assertNull(postgresServices.getReplicas());
    assertTrue(primary.equals(postgresServices.getPrimary()));
}
Also used : StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) StackGresDistributedLogsSpecFixture(io.stackgres.common.fixture.StackGresDistributedLogsSpecFixture) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) Test(org.junit.jupiter.api.Test)

Example 4 with StackGresDistributedLogsSpec

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

the class DistributedLogsPodTemplateSpecFactory method getPodTemplateSpec.

@Override
public PodTemplateResult getPodTemplateSpec(DistributedLogsContainerContext context) {
    StackGresDistributedLogs cluster = context.getDistributedLogsContext().getSource();
    final Map<String, String> podLabels = labelFactory.statefulSetPodLabels(cluster);
    List<ContainerFactory<DistributedLogsContainerContext>> containerFactories = containerFactoryDiscoverer.discoverContainers(context);
    List<Container> containers = containerFactories.stream().map(f -> f.getContainer(context)).collect(Collectors.toUnmodifiableList());
    final List<ContainerFactory<DistributedLogsContainerContext>> initContainerFactories = initContainerFactoryDiscoverer.discoverContainers(context);
    List<Container> initContainers = initContainerFactories.stream().map(f -> f.getContainer(context)).collect(Collectors.toUnmodifiableList());
    final List<String> claimedVolumes = Stream.concat(containers.stream(), initContainers.stream()).flatMap(container -> container.getVolumeMounts().stream()).map(VolumeMount::getName).distinct().collect(Collectors.toUnmodifiableList());
    claimedVolumes.forEach(rv -> {
        if (!context.availableVolumes().containsKey(rv) && !context.getDataVolumeName().equals(rv)) {
            throw new IllegalStateException("Volume " + rv + " is required but not available");
        }
    });
    List<Volume> volumes = claimedVolumes.stream().map(volumeName -> context.availableVolumes().get(volumeName)).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList());
    var podTemplateSpec = new PodTemplateSpecBuilder().withMetadata(new ObjectMetaBuilder().addToLabels(podLabels).addToAnnotations(StackGresContext.VERSION_KEY, cluster.getMetadata().getAnnotations().getOrDefault(StackGresContext.VERSION_KEY, StackGresProperty.OPERATOR_VERSION.getString())).build()).withNewSpec().withAffinity(Optional.of(new AffinityBuilder().withPodAntiAffinity(new PodAntiAffinityBuilder().addAllToRequiredDuringSchedulingIgnoredDuringExecution(ImmutableList.of(new PodAffinityTermBuilder().withLabelSelector(new LabelSelectorBuilder().withMatchExpressions(new LabelSelectorRequirementBuilder().withKey(StackGresContext.APP_KEY).withOperator("In").withValues(labelFactory.labelMapper().appName()).build(), new LabelSelectorRequirementBuilder().withKey("cluster").withOperator("In").withValues("true").build()).build()).withTopologyKey("kubernetes.io/hostname").build())).build()).build()).filter(affinity -> Optional.ofNullable(cluster.getSpec().getNonProduction()).map(StackGresDistributedLogsNonProduction::getDisableClusterPodAntiAffinity).map(disableClusterPodAntiAffinity -> !disableClusterPodAntiAffinity).orElse(true)).orElse(null)).withNodeSelector(Optional.ofNullable(cluster.getSpec()).map(StackGresDistributedLogsSpec::getScheduling).map(StackGresDistributedLogsPodScheduling::getNodeSelector).orElse(null)).withTolerations(Optional.ofNullable(cluster.getSpec()).map(StackGresDistributedLogsSpec::getScheduling).map(StackGresDistributedLogsPodScheduling::getTolerations).map(tolerations -> Seq.seq(tolerations).map(TolerationBuilder::new).map(TolerationBuilder::build).toList()).orElse(null)).withShareProcessNamespace(Boolean.TRUE).withServiceAccountName(PatroniRole.roleName(context.getDistributedLogsContext())).withSecurityContext(podSecurityContext.createResource(context.getDistributedLogsContext())).withVolumes(volumes).withContainers(containers).withInitContainers(initContainers).withTerminationGracePeriodSeconds(60L).endSpec().build();
    return ImmutablePodTemplateResult.builder().spec(podTemplateSpec).claimedVolumes(claimedVolumes).build();
}
Also used : VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) StackGresContext(io.stackgres.common.StackGresContext) ContainerFactoryDiscoverer(io.stackgres.operator.conciliation.ContainerFactoryDiscoverer) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) Container(io.fabric8.kubernetes.api.model.Container) StackGresProperty(io.stackgres.common.StackGresProperty) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) PatroniRole(io.stackgres.operator.conciliation.factory.distributedlogs.patroni.PatroniRole) Singleton(javax.inject.Singleton) Seq(org.jooq.lambda.Seq) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ImmutablePodTemplateResult(io.stackgres.operator.conciliation.factory.ImmutablePodTemplateResult) StackGresDistributedLogsPodScheduling(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPodScheduling) Map(java.util.Map) PodAntiAffinityBuilder(io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder) ContainerFactory(io.stackgres.operator.conciliation.factory.ContainerFactory) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ResourceFactory(io.stackgres.operator.conciliation.factory.ResourceFactory) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) StackGresDistributedLogsNonProduction(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) StackGresDistributedLogsContext(io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) PodTemplateFactory(io.stackgres.operator.conciliation.factory.PodTemplateFactory) Collectors(java.util.stream.Collectors) LabelSelectorRequirementBuilder(io.fabric8.kubernetes.api.model.LabelSelectorRequirementBuilder) TolerationBuilder(io.fabric8.kubernetes.api.model.TolerationBuilder) Objects(java.util.Objects) AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) List(java.util.List) PodAffinityTermBuilder(io.fabric8.kubernetes.api.model.PodAffinityTermBuilder) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Stream(java.util.stream.Stream) Optional(java.util.Optional) InitContainerFactoryDiscover(io.stackgres.operator.conciliation.InitContainerFactoryDiscover) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) StackGresDistributedLogsPodScheduling(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPodScheduling) LabelSelectorRequirementBuilder(io.fabric8.kubernetes.api.model.LabelSelectorRequirementBuilder) TolerationBuilder(io.fabric8.kubernetes.api.model.TolerationBuilder) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) PodAntiAffinityBuilder(io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder) AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) PodAntiAffinityBuilder(io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder) PodAffinityTermBuilder(io.fabric8.kubernetes.api.model.PodAffinityTermBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) StackGresDistributedLogsNonProduction(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction) Container(io.fabric8.kubernetes.api.model.Container) Volume(io.fabric8.kubernetes.api.model.Volume) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) ContainerFactory(io.stackgres.operator.conciliation.factory.ContainerFactory) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount)

Example 5 with StackGresDistributedLogsSpec

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

the class DefaultPostgresServicesMutator method validatePgServices.

private ImmutableList<JsonPatchOperation> validatePgServices(ImmutableList.Builder<JsonPatchOperation> operations, StackGresDistributedLogsSpec spec) {
    return Optional.ofNullable(spec.getPostgresServices()).map(pgServices -> {
        mapPgPrimaryService(pgServices);
        mapPgReplicasService(pgServices);
        JsonNode target = JSON_MAPPER.valueToTree(pgServices);
        operations.add(applyReplaceValue(postgresServicesPointer, target));
        return operations.build();
    }).orElseGet(() -> {
        StackGresDistributedLogsPostgresServices pgServices = new StackGresDistributedLogsPostgresServices();
        pgServices.setPrimary(createPostgresServicePrimary());
        pgServices.setReplicas(createPostgresServiceReplicas());
        JsonNode target = JSON_MAPPER.valueToTree(pgServices);
        operations.add(applyAddValue(postgresServicesPointer, target));
        return operations.build();
    });
}
Also used : Operation(io.stackgres.operatorframework.admissionwebhook.Operation) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) CLUSTER_IP(io.stackgres.common.crd.postgres.service.StackGresPostgresServiceType.CLUSTER_IP) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) StackGresPostgresServiceType(io.stackgres.common.crd.postgres.service.StackGresPostgresServiceType) StackGresDistributedLogsReview(io.stackgres.operator.common.StackGresDistributedLogsReview) List(java.util.List) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresDistributedLogsPostgresServices(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPostgresServices) ImmutableList(com.google.common.collect.ImmutableList) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) AdmissionRequest(io.stackgres.operatorframework.admissionwebhook.AdmissionRequest) StackGresDistributedLogsPostgresServices(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPostgresServices) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

StackGresDistributedLogsSpec (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec)7 StackGresDistributedLogs (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs)3 StackGresDistributedLogsPodScheduling (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPodScheduling)3 ImmutableList (com.google.common.collect.ImmutableList)2 DistributedLogsPostgresServicesConverter (io.stackgres.apiweb.transformer.distributedlogs.DistributedLogsPostgresServicesConverter)2 StackGresClusterPostgresServices (io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)2 StackGresDistributedLogsNonProduction (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction)2 StackGresDistributedLogsSpecFixture (io.stackgres.common.fixture.StackGresDistributedLogsSpecFixture)2 List (java.util.List)2 Optional (java.util.Optional)2 Test (org.junit.jupiter.api.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)1 JsonPointer (com.github.fge.jackson.jsonpointer.JsonPointer)1 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)1 AffinityBuilder (io.fabric8.kubernetes.api.model.AffinityBuilder)1 Container (io.fabric8.kubernetes.api.model.Container)1 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)1 LabelSelectorRequirementBuilder (io.fabric8.kubernetes.api.model.LabelSelectorRequirementBuilder)1 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1