use of io.fabric8.kubernetes.api.model.PodAffinityTermBuilder 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.fabric8.kubernetes.api.model.PodAffinityTermBuilder 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();
}
Aggregations