Search in sources :

Example 1 with StackGresDistributedLogsNonProduction

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction 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 2 with StackGresDistributedLogsNonProduction

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

the class StackGresDistributedLogsUtil method getStackGresClusterForDistributedLogs.

static StackGresCluster getStackGresClusterForDistributedLogs(StackGresDistributedLogs distributedLogs) {
    final StackGresCluster distributedLogsCluster = new StackGresCluster();
    distributedLogsCluster.getMetadata().setNamespace(distributedLogs.getMetadata().getNamespace());
    distributedLogsCluster.getMetadata().setName(distributedLogs.getMetadata().getName());
    distributedLogsCluster.getMetadata().setUid(distributedLogs.getMetadata().getUid());
    final StackGresClusterSpec spec = new StackGresClusterSpec();
    spec.setPostgres(new StackGresClusterPostgres());
    spec.getPostgres().setVersion(getPostgresVersion());
    spec.setInstances(1);
    final StackGresClusterPod pod = new StackGresClusterPod();
    final StackGresPodPersistentVolume persistentVolume = new StackGresPodPersistentVolume();
    persistentVolume.setSize(distributedLogs.getSpec().getPersistentVolume().getSize());
    persistentVolume.setStorageClass(distributedLogs.getSpec().getPersistentVolume().getStorageClass());
    pod.setPersistentVolume(persistentVolume);
    spec.setPostgresServices(buildPostgresServices(distributedLogs.getSpec()));
    StackGresClusterPodScheduling scheduling = new StackGresClusterPodScheduling();
    Optional.of(distributedLogs).map(StackGresDistributedLogs::getSpec).map(StackGresDistributedLogsSpec::getScheduling).ifPresent(distributedLogsScheduling -> {
        scheduling.setNodeSelector(distributedLogsScheduling.getNodeSelector());
        scheduling.setTolerations(distributedLogsScheduling.getTolerations());
    });
    pod.setScheduling(scheduling);
    spec.setPod(pod);
    final StackGresClusterInitData initData = new StackGresClusterInitData();
    final StackGresClusterScriptEntry script = new StackGresClusterScriptEntry();
    script.setName("distributed-logs-template");
    script.setDatabase("template1");
    script.setScript(Unchecked.supplier(() -> Resources.asCharSource(StackGresDistributedLogsUtil.class.getResource("/distributed-logs-template.sql"), StandardCharsets.UTF_8).read()).get());
    initData.setScripts(ImmutableList.of(script));
    spec.setInitData(initData);
    final StackGresClusterNonProduction nonProduction = new StackGresClusterNonProduction();
    nonProduction.setDisableClusterPodAntiAffinity(Optional.ofNullable(distributedLogs.getSpec().getNonProduction()).map(StackGresDistributedLogsNonProduction::getDisableClusterPodAntiAffinity).orElse(false));
    spec.setNonProduction(nonProduction);
    final StackGresClusterSpecMetadata metadata = new StackGresClusterSpecMetadata();
    final StackGresClusterSpecAnnotations annotations = new StackGresClusterSpecAnnotations();
    Optional.of(distributedLogs).map(StackGresDistributedLogs::getSpec).map(StackGresDistributedLogsSpec::getMetadata).map(StackGresDistributedLogsSpecMetadata::getAnnotations).ifPresent(distributedLogsAnnotations -> {
        annotations.setAllResources(distributedLogsAnnotations.getAllResources());
        annotations.setClusterPods(distributedLogsAnnotations.getPods());
        annotations.setPrimaryService(distributedLogsAnnotations.getServices());
        annotations.setReplicasService(distributedLogsAnnotations.getServices());
    });
    metadata.setAnnotations(annotations);
    spec.setMetadata(metadata);
    spec.setToInstallPostgresExtensions(Optional.ofNullable(distributedLogs.getSpec()).map(StackGresDistributedLogsSpec::getToInstallPostgresExtensions).orElse(null));
    distributedLogsCluster.setSpec(spec);
    return distributedLogsCluster;
}
Also used : StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresDistributedLogsNonProduction(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterNonProduction(io.stackgres.common.crd.sgcluster.StackGresClusterNonProduction) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)

Aggregations

StackGresDistributedLogs (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs)2 StackGresDistributedLogsNonProduction (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction)2 StackGresDistributedLogsSpec (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec)2 ImmutableList (com.google.common.collect.ImmutableList)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 PodAffinityTermBuilder (io.fabric8.kubernetes.api.model.PodAffinityTermBuilder)1 PodAntiAffinityBuilder (io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder)1 PodSecurityContext (io.fabric8.kubernetes.api.model.PodSecurityContext)1 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)1 TolerationBuilder (io.fabric8.kubernetes.api.model.TolerationBuilder)1 Volume (io.fabric8.kubernetes.api.model.Volume)1 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)1 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)1 StackGresContext (io.stackgres.common.StackGresContext)1 StackGresProperty (io.stackgres.common.StackGresProperty)1 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)1