use of io.stackgres.operator.conciliation.factory.VolumePair in project stackgres by ongres.
the class PatroniScriptsConfigMap method buildVolumes.
@Override
@NotNull
public Stream<VolumePair> buildVolumes(StackGresClusterContext context) {
StackGresCluster cluster = context.getSource();
var indexedScripts = context.getIndexedScripts();
List<VolumePair> inlineScripts = indexedScripts.stream().filter(t -> t.v1.getScript() != null).map(t -> ImmutableVolumePair.builder().volume(new VolumeBuilder().withName(name(context, t)).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(name(context, t)).withOptional(false).build()).build()).source(new ConfigMapBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context, t)).withLabels(labelFactory.patroniClusterLabels(cluster)).endMetadata().withData(ImmutableMap.of(scriptName(t), t.v1.getScript())).build()).build()).collect(Collectors.toUnmodifiableList());
List<VolumePair> configMapScripts = indexedScripts.stream().filter(t -> t.v1.getScriptFrom() != null).filter(t -> t.v1.getScriptFrom().getConfigMapKeyRef() != null).map(t -> ImmutableVolumePair.builder().volume(new VolumeBuilder().withName(name(context, t)).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(t.v1.getScriptFrom().getConfigMapKeyRef().getName()).withOptional(false).build()).build()).build()).collect(Collectors.toUnmodifiableList());
List<VolumePair> secretScripts = indexedScripts.stream().filter(t -> t.v1.getScriptFrom() != null).filter(t -> t.v1.getScriptFrom().getSecretKeyRef() != null).map(t -> ImmutableVolumePair.builder().volume(new VolumeBuilder().withName(name(context, t)).withSecret(new SecretVolumeSourceBuilder().withSecretName(t.v1.getScriptFrom().getSecretKeyRef().getName()).withOptional(false).build()).build()).build()).collect(Collectors.toUnmodifiableList());
return ImmutableList.<VolumePair>builder().addAll(inlineScripts).addAll(configMapScripts).addAll(secretScripts).build().stream();
}
use of io.stackgres.operator.conciliation.factory.VolumePair 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);
}
use of io.stackgres.operator.conciliation.factory.VolumePair 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);
}
Aggregations