Search in sources :

Example 1 with StackGresDistributedLogsContext

use of io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext in project stackgres by ongres.

the class OwnerReferenceDecorator method decorate.

@Override
public void decorate(StackGresDistributedLogsContext context, Iterable<? extends HasMetadata> resources) {
    List<OwnerReference> ownerReferences = List.of(ResourceUtil.getOwnerReference(context.getSource()));
    Seq.seq(resources).filter(resource -> Objects.equals(resource.getMetadata().getNamespace(), context.getSource().getMetadata().getNamespace())).filter(resource -> resource.getMetadata().getOwnerReferences().isEmpty()).forEach(resource -> {
        resource.getMetadata().setOwnerReferences(ownerReferences);
        if (resource.getKind().equals("StatefulSet")) {
            StatefulSet sts = (StatefulSet) resource;
            sts.getSpec().getVolumeClaimTemplates().forEach(vct -> vct.getMetadata().setOwnerReferences(ownerReferences));
        }
    });
}
Also used : Objects(java.util.Objects) StackGresDistributedLogsContext(io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext) List(java.util.List) ResourceUtil(io.stackgres.common.resource.ResourceUtil) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Seq(org.jooq.lambda.Seq) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Decorator(io.stackgres.operator.conciliation.factory.Decorator) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet)

Example 2 with StackGresDistributedLogsContext

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

use of io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext in project stackgres by ongres.

the class OwnerReferenceDecorator method decorate.

@Override
public void decorate(StackGresDistributedLogsContext context, Iterable<? extends HasMetadata> resources) {
    List<OwnerReference> ownerReferences = getOwnerReferences(context.getSource());
    resources.forEach(resource -> {
        if (!resource.getMetadata().getOwnerReferences().isEmpty()) {
            resource.getMetadata().getOwnerReferences().stream().filter(ownerReference -> ownerReference.getApiVersion().equals(CommonDefinition.GROUP + "/" + CommonDefinition.VERSION)).forEach(ownerReference -> ownerReference.setApiVersion(CommonDefinition.GROUP + "/v1beta1"));
        } else {
            resource.getMetadata().setOwnerReferences(ownerReferences);
            if (resource.getKind().equals("StatefulSet")) {
                StatefulSet sts = (StatefulSet) resource;
                sts.getSpec().getVolumeClaimTemplates().forEach(vct -> vct.getMetadata().setOwnerReferences(ownerReferences));
            }
        }
    });
}
Also used : StackGresDistributedLogsContext(io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext) List(java.util.List) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) StackGresVersion(io.stackgres.operator.common.StackGresVersion) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Singleton(javax.inject.Singleton) CommonDefinition(io.stackgres.common.crd.CommonDefinition) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Decorator(io.stackgres.operator.conciliation.factory.Decorator) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)3 StackGresVersion (io.stackgres.operator.common.StackGresVersion)3 OperatorVersionBinder (io.stackgres.operator.conciliation.OperatorVersionBinder)3 StackGresDistributedLogsContext (io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext)3 Singleton (javax.inject.Singleton)3 OwnerReference (io.fabric8.kubernetes.api.model.OwnerReference)2 StackGresDistributedLogs (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs)2 Decorator (io.stackgres.operator.conciliation.factory.Decorator)2 List (java.util.List)2 Objects (java.util.Objects)2 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 OwnerReferenceBuilder (io.fabric8.kubernetes.api.model.OwnerReferenceBuilder)1 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)1 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)1 PersistentVolumeClaimSpecBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder)1 Volume (io.fabric8.kubernetes.api.model.Volume)1 StatefulSetBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)1 StatefulSetUpdateStrategyBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder)1