Search in sources :

Example 1 with VolumePair

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();
}
Also used : VolumeFactory(io.stackgres.operator.conciliation.factory.VolumeFactory) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) ImmutableVolumePair(io.stackgres.operator.conciliation.factory.ImmutableVolumePair) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) ImmutableList(com.google.common.collect.ImmutableList) Tuple4(org.jooq.lambda.tuple.Tuple4) Locale(java.util.Locale) Volume(io.fabric8.kubernetes.api.model.Volume) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) ResourceUtil(io.stackgres.operatorframework.resource.ResourceUtil) Collectors(java.util.stream.Collectors) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) List(java.util.List) Stream(java.util.stream.Stream) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) NotNull(org.jetbrains.annotations.NotNull) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ImmutableVolumePair(io.stackgres.operator.conciliation.factory.ImmutableVolumePair) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with VolumePair

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);
}
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 3 with VolumePair

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

Aggregations

Volume (io.fabric8.kubernetes.api.model.Volume)3 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)3 StackGresVersion (io.stackgres.operator.common.StackGresVersion)3 OperatorVersionBinder (io.stackgres.operator.conciliation.OperatorVersionBinder)3 VolumePair (io.stackgres.operator.conciliation.factory.VolumePair)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 Inject (javax.inject.Inject)3 Singleton (javax.inject.Singleton)3 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)2 PersistentVolumeClaimSpecBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder)2 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)2 StatefulSetBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)2 StatefulSetUpdateStrategyBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder)2 ImmutableStorageConfig (io.stackgres.common.ImmutableStorageConfig)2 StackGresUtil (io.stackgres.common.StackGresUtil)2 StorageConfig (io.stackgres.common.StorageConfig)2