Search in sources :

Example 1 with PodTemplateResult

use of io.stackgres.operator.conciliation.factory.PodTemplateResult in project stackgres by ongres.

the class ClusterStatefulSet method generateResource.

@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
    final StackGresCluster cluster = context.getSource();
    final ObjectMeta metadata = cluster.getMetadata();
    final String name = metadata.getName();
    final String namespace = metadata.getNamespace();
    final StackGresPodPersistentVolume persistentVolume = cluster.getSpec().getPod().getPersistentVolume();
    StorageConfig dataStorageConfig = ImmutableStorageConfig.builder().size(persistentVolume.getSize()).storageClass(Optional.ofNullable(persistentVolume.getStorageClass()).orElse(null)).build();
    final Map<String, String> labels = labelFactory.clusterLabels(cluster);
    final Map<String, String> podLabels = labelFactory.statefulSetPodLabels(cluster);
    Map<String, VolumePair> availableVolumesPairs = volumeDiscoverer.discoverVolumes(context);
    final PersistentVolumeClaimSpecBuilder volumeClaimSpec = new PersistentVolumeClaimSpecBuilder().withAccessModes("ReadWriteOnce").withResources(dataStorageConfig.getResourceRequirements()).withStorageClassName(dataStorageConfig.getStorageClass());
    Map<String, Volume> availableVolumes = availableVolumesPairs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, vp -> vp.getValue().getVolume()));
    final PodTemplateResult podTemplateSpec = podTemplateSpecFactory.getPodTemplateSpec(ImmutableStackGresClusterContainerContext.builder().clusterContext(context).availableVolumes(availableVolumes).dataVolumeName(dataName(context)).build());
    StatefulSet clusterStatefulSet = new StatefulSetBuilder().withNewMetadata().withNamespace(namespace).withName(name).withLabels(labels).endMetadata().withNewSpec().withReplicas(cluster.getSpec().getInstances()).withSelector(new LabelSelectorBuilder().addToMatchLabels(podLabels).build()).withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withServiceName(name).withTemplate(podTemplateSpec.getSpec()).withVolumeClaimTemplates(new PersistentVolumeClaimBuilder().withNewMetadata().withNamespace(namespace).withName(dataName(context)).withLabels(labels).endMetadata().withSpec(volumeClaimSpec.build()).build()).endSpec().build();
    var volumeDependencies = podTemplateSpec.claimedVolumes().stream().map(availableVolumesPairs::get).filter(Objects::nonNull).map(VolumePair::getSource).filter(Optional::isPresent).map(Optional::get);
    return Stream.concat(Stream.of(clusterStatefulSet), volumeDependencies);
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) ImmutableStorageConfig(io.stackgres.common.ImmutableStorageConfig) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) PersistentVolumeClaimSpecBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) VolumeDiscoverer(io.stackgres.operator.conciliation.factory.VolumeDiscoverer) Map(java.util.Map) Volume(io.fabric8.kubernetes.api.model.Volume) ClusterContext(io.stackgres.common.ClusterContext) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) PodTemplateFactory(io.stackgres.operator.conciliation.factory.PodTemplateFactory) StackGresUtil(io.stackgres.common.StackGresUtil) StorageConfig(io.stackgres.common.StorageConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Objects(java.util.Objects) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Stream(java.util.stream.Stream) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) Optional(java.util.Optional) ImmutableStorageConfig(io.stackgres.common.ImmutableStorageConfig) StorageConfig(io.stackgres.common.StorageConfig) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Volume(io.fabric8.kubernetes.api.model.Volume) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) PersistentVolumeClaimSpecBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Map(java.util.Map)

Example 2 with PodTemplateResult

use of io.stackgres.operator.conciliation.factory.PodTemplateResult 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 3 with PodTemplateResult

use of io.stackgres.operator.conciliation.factory.PodTemplateResult in project stackgres by ongres.

the class DistributedLogsStatefulSet method generateResource.

@Override
public Stream<HasMetadata> generateResource(StackGresDistributedLogsContext context) {
    final StackGresDistributedLogs cluster = context.getSource();
    final ObjectMeta metadata = cluster.getMetadata();
    final String name = metadata.getName();
    final String namespace = metadata.getNamespace();
    final StackGresDistributedLogsPersistentVolume persistentVolume = cluster.getSpec().getPersistentVolume();
    StorageConfig dataStorageConfig = ImmutableStorageConfig.builder().size(persistentVolume.getSize()).storageClass(Optional.ofNullable(persistentVolume.getStorageClass()).orElse(null)).build();
    final PersistentVolumeClaimSpecBuilder volumeClaimSpec = new PersistentVolumeClaimSpecBuilder().withAccessModes("ReadWriteOnce").withResources(dataStorageConfig.getResourceRequirements()).withStorageClassName(dataStorageConfig.getStorageClass());
    final Map<String, String> labels = labelFactory.clusterLabels(cluster);
    final Map<String, String> podLabels = labelFactory.statefulSetPodLabels(cluster);
    Map<String, VolumePair> availableVolumesPairs = volumeDiscoverer.discoverVolumes(context);
    Map<String, Volume> availableVolumes = availableVolumesPairs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, vp -> vp.getValue().getVolume()));
    final PodTemplateResult buildPodTemplate = podTemplateSpecFactory.getPodTemplateSpec(ImmutableDistributedLogsContainerContext.builder().distributedLogsContext(context).availableVolumes(availableVolumes).dataVolumeName(dataName(cluster)).build());
    StatefulSet clusterStatefulSet = new StatefulSetBuilder().withNewMetadata().withNamespace(namespace).withName(name).withLabels(labels).endMetadata().withNewSpec().withReplicas(1).withSelector(new LabelSelectorBuilder().addToMatchLabels(podLabels).build()).withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withServiceName(name).withTemplate(buildPodTemplate.getSpec()).withVolumeClaimTemplates(Stream.of(Stream.of(new PersistentVolumeClaimBuilder().withNewMetadata().withNamespace(namespace).withName(dataName(cluster)).withLabels(labels).endMetadata().withSpec(volumeClaimSpec.build()).build())).flatMap(s -> s).toArray(PersistentVolumeClaim[]::new)).endSpec().build();
    var volumeDependencies = buildPodTemplate.claimedVolumes().stream().map(availableVolumesPairs::get).filter(Objects::nonNull).map(VolumePair::getSource).filter(Optional::isPresent).map(Optional::get);
    return Stream.concat(Stream.of(clusterStatefulSet), volumeDependencies);
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) ImmutableStorageConfig(io.stackgres.common.ImmutableStorageConfig) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) PersistentVolumeClaimSpecBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) VolumeDiscoverer(io.stackgres.operator.conciliation.factory.VolumeDiscoverer) Map(java.util.Map) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Volume(io.fabric8.kubernetes.api.model.Volume) StackGresDistributedLogsContext(io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) PodTemplateFactory(io.stackgres.operator.conciliation.factory.PodTemplateFactory) StackGresUtil(io.stackgres.common.StackGresUtil) StorageConfig(io.stackgres.common.StorageConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Objects(java.util.Objects) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Stream(java.util.stream.Stream) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) StackGresDistributedLogsPersistentVolume(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPersistentVolume) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) Optional(java.util.Optional) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ImmutableStorageConfig(io.stackgres.common.ImmutableStorageConfig) StorageConfig(io.stackgres.common.StorageConfig) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) StackGresDistributedLogsPersistentVolume(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPersistentVolume) Volume(io.fabric8.kubernetes.api.model.Volume) StackGresDistributedLogsPersistentVolume(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsPersistentVolume) PersistentVolumeClaimSpecBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Map(java.util.Map)

Example 4 with PodTemplateResult

use of io.stackgres.operator.conciliation.factory.PodTemplateResult in project stackgres by ongres.

the class PodTemplateSpecFactory method getPodTemplateSpec.

@Override
public PodTemplateResult getPodTemplateSpec(StackGresClusterContainerContext context) {
    final List<ContainerFactory<StackGresClusterContainerContext>> containerFactories = containerFactoryDiscoverer.discoverContainers(context);
    final List<Container> containers = containerFactories.stream().map(f -> f.getContainer(context)).collect(Collectors.toUnmodifiableList());
    Map<String, String> componentVersions = containerFactories.stream().map(f -> f.getComponentVersions(context)).map(Map::entrySet).flatMap(Set::stream).distinct().collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
    final List<ContainerFactory<StackGresClusterContainerContext>> 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());
    StackGresCluster cluster = context.getClusterContext().getSource();
    final Map<String, String> podLabels = labelFactory.statefulSetPodLabels(cluster);
    final Map<String, String> customPodLabels = podsCustomLabels(cluster);
    var podTemplate = new PodTemplateSpecBuilder().withMetadata(new ObjectMetaBuilder().addToLabels(customPodLabels).addToLabels(podLabels).addToAnnotations(StackGresContext.VERSION_KEY, StackGresProperty.OPERATOR_VERSION.getString()).addToAnnotations(componentVersions).build()).withNewSpec().withAffinity(new AffinityBuilder().withNodeAffinity(buildPodNodeAffinity(cluster)).withPodAntiAffinity(new PodAntiAffinityBuilder().addAllToRequiredDuringSchedulingIgnoredDuringExecution(Optional.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()).filter(ignore -> Optional.ofNullable(cluster.getSpec().getNonProduction()).map(StackGresClusterNonProduction::getDisableClusterPodAntiAffinity).map(disableClusterPodAntiAffinity -> !disableClusterPodAntiAffinity).orElse(true)).stream().collect(Collectors.toList())).build()).build()).withNodeSelector(Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getPod).map(StackGresClusterPod::getScheduling).map(StackGresClusterPodScheduling::getNodeSelector).orElse(null)).withTolerations(Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getPod).map(StackGresClusterPod::getScheduling).map(StackGresClusterPodScheduling::getTolerations).map(tolerations -> Seq.seq(tolerations).map(TolerationBuilder::new).map(TolerationBuilder::build).toList()).orElse(null)).withShareProcessNamespace(Boolean.TRUE).withServiceAccountName(PatroniRoleGenerator.roleName(context.getClusterContext())).withSecurityContext(podSecurityContext.createResource(context.getClusterContext())).withVolumes(volumes).withContainers(containers).withInitContainers(initContainers).withTerminationGracePeriodSeconds(60L).endSpec().build();
    return ImmutablePodTemplateResult.builder().spec(podTemplate).claimedVolumes(claimedVolumes).build();
}
Also used : NodeAffinityBuilder(io.fabric8.kubernetes.api.model.NodeAffinityBuilder) 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) StackGresClusterNonProduction(io.stackgres.common.crd.sgcluster.StackGresClusterNonProduction) Container(io.fabric8.kubernetes.api.model.Container) StackGresProperty(io.stackgres.common.StackGresProperty) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) Singleton(javax.inject.Singleton) Seq(org.jooq.lambda.Seq) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) Inject(javax.inject.Inject) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) ImmutablePodTemplateResult(io.stackgres.operator.conciliation.factory.ImmutablePodTemplateResult) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) Map(java.util.Map) PodAntiAffinityBuilder(io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder) ContainerFactory(io.stackgres.operator.conciliation.factory.ContainerFactory) ResourceFactory(io.stackgres.operator.conciliation.factory.ResourceFactory) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) PatroniRoleGenerator(io.stackgres.operator.conciliation.factory.cluster.patroni.PatroniRoleGenerator) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) Set(java.util.Set) 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) NodeAffinity(io.fabric8.kubernetes.api.model.NodeAffinity) Objects(java.util.Objects) AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) PodAffinityTermBuilder(io.fabric8.kubernetes.api.model.PodAffinityTermBuilder) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Stream(java.util.stream.Stream) StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) Optional(java.util.Optional) InitContainerFactoryDiscover(io.stackgres.operator.conciliation.InitContainerFactoryDiscover) StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) Set(java.util.Set) NodeAffinityBuilder(io.fabric8.kubernetes.api.model.NodeAffinityBuilder) PodAntiAffinityBuilder(io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder) AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) PodAffinityTermBuilder(io.fabric8.kubernetes.api.model.PodAffinityTermBuilder) Container(io.fabric8.kubernetes.api.model.Container) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) LabelSelectorRequirementBuilder(io.fabric8.kubernetes.api.model.LabelSelectorRequirementBuilder) TolerationBuilder(io.fabric8.kubernetes.api.model.TolerationBuilder) PodAntiAffinityBuilder(io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) 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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)4 Volume (io.fabric8.kubernetes.api.model.Volume)4 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)4 PodTemplateFactory (io.stackgres.operator.conciliation.factory.PodTemplateFactory)4 PodTemplateResult (io.stackgres.operator.conciliation.factory.PodTemplateResult)4 Map (java.util.Map)4 Objects (java.util.Objects)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 Inject (javax.inject.Inject)4 Singleton (javax.inject.Singleton)4 AffinityBuilder (io.fabric8.kubernetes.api.model.AffinityBuilder)2 Container (io.fabric8.kubernetes.api.model.Container)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 LabelSelectorRequirementBuilder (io.fabric8.kubernetes.api.model.LabelSelectorRequirementBuilder)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)2 PodAffinityTermBuilder (io.fabric8.kubernetes.api.model.PodAffinityTermBuilder)2 PodAntiAffinityBuilder (io.fabric8.kubernetes.api.model.PodAntiAffinityBuilder)2