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