Search in sources :

Example 1 with StackGresPodPersistentVolume

use of io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume in project stackgres by ongres.

the class ClusterResourceMockedTest method checkDto.

@Override
protected void checkDto(ClusterDto dto, StackGresCluster resource) {
    if (resource.getMetadata() != null) {
        assertNotNull(dto.getMetadata());
        assertEquals(resource.getMetadata().getNamespace(), dto.getMetadata().getNamespace());
        assertEquals(resource.getMetadata().getName(), dto.getMetadata().getName());
        assertEquals(resource.getMetadata().getUid(), dto.getMetadata().getUid());
    } else {
        assertNull(dto.getMetadata());
    }
    final StackGresClusterSpec resourceSpec = resource.getSpec();
    final ClusterSpec dtoSpec = dto.getSpec();
    if (resourceSpec != null) {
        assertNotNull(dtoSpec);
        assertEquals(resourceSpec.getInstances(), dtoSpec.getInstances());
        assertEquals(resourceSpec.getPostgres().getVersion(), dtoSpec.getPostgres().getVersion());
        assertEquals(resourceSpec.getPrometheusAutobind(), dtoSpec.getPrometheusAutobind());
        assertEquals(resourceSpec.getResourceProfile(), dtoSpec.getSgInstanceProfile());
        final ClusterConfiguration dtoClusterConfigurations = dtoSpec.getConfigurations();
        final StackGresClusterConfiguration resourceClusterConfiguration = resourceSpec.getConfiguration();
        if (resourceClusterConfiguration != null) {
            assertNotNull(dtoClusterConfigurations);
            assertEquals(resourceClusterConfiguration.getBackupConfig(), dtoClusterConfigurations.getSgBackupConfig());
            assertEquals(resourceClusterConfiguration.getConnectionPoolingConfig(), dtoClusterConfigurations.getSgPoolingConfig());
            assertEquals(resourceClusterConfiguration.getPostgresConfig(), dtoClusterConfigurations.getSgPostgresConfig());
        } else {
            assertNull(dtoClusterConfigurations);
        }
        final ClusterPod dtoSpecPods = dtoSpec.getPods();
        final StackGresClusterPod resourcePod = resourceSpec.getPod();
        if (resourcePod != null) {
            assertNotNull(dtoSpecPods);
            assertEquals(resourcePod.getDisableConnectionPooling(), dtoSpecPods.getDisableConnectionPooling());
            assertEquals(resourcePod.getDisableMetricsExporter(), dtoSpecPods.getDisableMetricsExporter());
            assertEquals(resourcePod.getDisablePostgresUtil(), dtoSpecPods.getDisablePostgresUtil());
            final ClusterPodPersistentVolume resourcePV = dtoSpecPods.getPersistentVolume();
            final StackGresPodPersistentVolume dtoPV = resourcePod.getPersistentVolume();
            if (dtoPV != null) {
                assertNotNull(resourcePV);
                assertEquals(dtoPV.getSize(), resourcePV.getSize());
                assertEquals(dtoPV.getStorageClass(), resourcePV.getStorageClass());
            } else {
                assertNull(resourcePV);
            }
            if (resourceSpec.getMetadata() != null) {
                assertNotNull(dtoSpec.getMetadata());
                assertEquals(resourceSpec.getMetadata().getLabels().getClusterPods(), dtoSpec.getMetadata().getLabels().getClusterPods());
            } else {
                assertNull(dtoSpec.getMetadata());
            }
            if (resourcePod.getScheduling() != null) {
                assertNotNull(dtoSpecPods.getScheduling());
                assertEquals(resourcePod.getScheduling().getNodeSelector(), dtoSpecPods.getScheduling().getNodeSelector());
                assertEquals(resourcePod.getScheduling().getNodeAffinity(), dtoSpecPods.getScheduling().getNodeAffinity());
            } else {
                assertNull(dtoSpecPods.getScheduling());
            }
        } else {
            assertNull(dtoSpecPods);
        }
        if (resourceSpec.getDistributedLogs() != null) {
            assertNotNull(dtoSpec.getDistributedLogs());
            assertEquals(resourceSpec.getDistributedLogs().getDistributedLogs(), dtoSpec.getDistributedLogs().getDistributedLogs());
        } else {
            assertNull(dtoSpec.getDistributedLogs());
        }
        final StackGresClusterInitData resourceInitData = resourceSpec.getInitData();
        if (resourceInitData != null) {
            final ClusterInitData dtoInitData = dtoSpec.getInitData();
            assertNotNull(dtoInitData);
            if (resourceInitData.getRestore() != null) {
                assertNotNull(dtoInitData.getRestore());
                assertEquals(resourceInitData.getRestore().getFromBackup().getUid(), dtoInitData.getRestore().getFromBackup().getUid());
                assertEquals(resourceInitData.getRestore().getDownloadDiskConcurrency(), dtoInitData.getRestore().getDownloadDiskConcurrency());
            } else {
                assertNull(dtoInitData.getRestore());
            }
            if (resourceInitData.getScripts() != null) {
                assertNotNull(dtoInitData.getScripts());
                Seq.zip(resourceInitData.getScripts(), dtoInitData.getScripts()).forEach(tuple -> {
                    assertEquals(tuple.v1.getDatabase(), tuple.v2.getDatabase());
                    assertEquals(tuple.v2.getName(), tuple.v2.getName());
                    assertEquals(tuple.v2.getScript(), tuple.v2.getScript());
                    final StackGresClusterScriptFrom resourceScriptFrom = tuple.v1.getScriptFrom();
                    final ClusterScriptFrom dtoScriptFrom = tuple.v2.getScriptFrom();
                    if (resourceScriptFrom != null) {
                        assertNotNull(dtoScriptFrom);
                        if (resourceScriptFrom.getSecretKeyRef() != null) {
                            assertNotNull(dtoScriptFrom.getSecretKeyRef());
                            assertEquals(resourceScriptFrom.getSecretKeyRef().getKey(), dtoScriptFrom.getSecretKeyRef().getKey());
                            assertEquals(resourceScriptFrom.getSecretKeyRef().getName(), dtoScriptFrom.getSecretKeyRef().getName());
                        } else {
                            assertNull(dtoScriptFrom.getSecretKeyRef());
                        }
                        if (resourceScriptFrom.getConfigMapKeyRef() != null) {
                            assertNotNull(dtoScriptFrom.getConfigMapKeyRef());
                            assertEquals(resourceScriptFrom.getConfigMapKeyRef().getKey(), dtoScriptFrom.getConfigMapKeyRef().getKey());
                            assertEquals(resourceScriptFrom.getConfigMapKeyRef().getName(), dtoScriptFrom.getConfigMapKeyRef().getName());
                            assertEquals(configMap.getData().get(resourceScriptFrom.getConfigMapKeyRef().getKey()), dtoScriptFrom.getConfigMapScript());
                        } else {
                            assertNull(dtoScriptFrom.getConfigMapKeyRef());
                        }
                    } else {
                        assertNull(dtoScriptFrom);
                    }
                });
            }
        }
    } else {
        assertNull(dtoSpec);
    }
    if (dto.getPods() != null) {
        assertEquals(1, dto.getPodsReady());
        assertEquals(2, dto.getPods().size());
        assertEquals(4, dto.getPods().get(0).getContainers());
        assertEquals(4, dto.getPods().get(0).getContainersReady());
        assertEquals("10.244.3.23", dto.getPods().get(0).getIp());
        assertEquals("stackgres-0", dto.getPods().get(0).getName());
        assertEquals("stackgres", dto.getPods().get(0).getNamespace());
        assertEquals("primary", dto.getPods().get(0).getRole());
        assertEquals("Active", dto.getPods().get(0).getStatus());
        assertEquals(4, dto.getPods().get(0).getComponentVersions().size());
        assertEquals("12.2", dto.getPods().get(0).getComponentVersions().get("postgresql"));
        assertEquals("1.6.4", dto.getPods().get(0).getComponentVersions().get("patroni"));
        assertEquals("1.13.1", dto.getPods().get(0).getComponentVersions().get("envoy"));
        assertEquals("0.8", dto.getPods().get(0).getComponentVersions().get("prometheus-postgres-exporter"));
        assertEquals(4, dto.getPods().get(1).getContainers());
        assertEquals(0, dto.getPods().get(1).getContainersReady());
        assertNull(dto.getPods().get(1).getIp());
        assertEquals("stackgres-1", dto.getPods().get(1).getName());
        assertEquals("stackgres", dto.getPods().get(1).getNamespace());
        assertNull(dto.getPods().get(1).getRole());
        assertEquals("Pending", dto.getPods().get(1).getStatus());
        assertEquals(4, dto.getPods().get(1).getComponentVersions().size());
        assertEquals("12.2", dto.getPods().get(1).getComponentVersions().get("postgresql"));
        assertEquals("1.6.4", dto.getPods().get(1).getComponentVersions().get("patroni"));
        assertEquals("1.13.1", dto.getPods().get(1).getComponentVersions().get("envoy"));
        assertEquals("0.8", dto.getPods().get(1).getComponentVersions().get("prometheus-postgres-exporter"));
    }
    if (dto.getInfo() != null) {
        String appendDns = "." + resource.getMetadata().getNamespace() + ".svc.cluster.local";
        String expectedPrimaryDns = PatroniUtil.readWriteName(resource.getMetadata().getName()) + appendDns;
        String expectedReplicasDns = "f4611c56942064ed5a468d8ce0a894ec.us-east-1.elb.amazonaws.com";
        assertEquals(expectedPrimaryDns, dto.getInfo().getPrimaryDns());
        assertEquals(expectedReplicasDns, dto.getInfo().getReplicasDns());
        assertEquals("postgres", dto.getInfo().getSuperuserUsername());
        assertEquals("superuser-password", dto.getInfo().getSuperuserPasswordKey());
        assertEquals(resource.getMetadata().getName(), dto.getInfo().getSuperuserSecretName());
    }
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ClusterSpec(io.stackgres.apiweb.dto.cluster.ClusterSpec) ClusterConfiguration(io.stackgres.apiweb.dto.cluster.ClusterConfiguration) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) ClusterPod(io.stackgres.apiweb.dto.cluster.ClusterPod) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) ClusterPodPersistentVolume(io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom)

Example 2 with StackGresPodPersistentVolume

use of io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume 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 StackGresPodPersistentVolume

use of io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume in project stackgres by ongres.

the class ClusterResourceMockedTest method checkCustomResource.

@Override
protected void checkCustomResource(StackGresCluster resource, ClusterDto resourceDto, Operation operation) {
    final Metadata dtoMetadata = resourceDto.getMetadata();
    final ObjectMeta resourceMetadata = resource.getMetadata();
    if (dtoMetadata != null) {
        assertNotNull(resourceMetadata);
        assertEquals(dtoMetadata.getName(), resourceMetadata.getName());
        assertEquals(dtoMetadata.getNamespace(), resourceMetadata.getNamespace());
        assertEquals(dtoMetadata.getUid(), resourceMetadata.getUid());
    } else {
        assertNull(resourceMetadata);
    }
    final ClusterSpec dtoSpec = resourceDto.getSpec();
    final StackGresClusterSpec resourceSpec = resource.getSpec();
    if (dtoSpec != null) {
        assertNotNull(resourceSpec);
        assertEquals(dtoSpec.getPrometheusAutobind(), resourceSpec.getPrometheusAutobind());
        assertEquals(dtoSpec.getInstances(), resourceSpec.getInstances());
        assertEquals(dtoSpec.getPostgres().getVersion(), resourceSpec.getPostgres().getVersion());
        assertEquals(dtoSpec.getSgInstanceProfile(), resourceSpec.getResourceProfile());
        final ClusterConfiguration dtoSpecConfigurations = dtoSpec.getConfigurations();
        final StackGresClusterConfiguration resourceSpecConfiguration = resourceSpec.getConfiguration();
        if (dtoSpecConfigurations != null) {
            assertNotNull(resourceSpecConfiguration);
            assertEquals(dtoSpecConfigurations.getSgBackupConfig(), resourceSpecConfiguration.getBackupConfig());
            assertEquals(dtoSpecConfigurations.getSgPoolingConfig(), resourceSpecConfiguration.getConnectionPoolingConfig());
            assertEquals(dtoSpecConfigurations.getSgPostgresConfig(), resourceSpecConfiguration.getPostgresConfig());
        } else {
            assertNull(resourceSpecConfiguration);
        }
        final ClusterPod dtoSpecPods = dtoSpec.getPods();
        if (dtoSpecPods != null) {
            final StackGresClusterPod resourceSpecPod = resourceSpec.getPod();
            assertNotNull(resourceSpecPod);
            assertEquals(dtoSpecPods.getDisableConnectionPooling(), resourceSpecPod.getDisableConnectionPooling());
            assertEquals(dtoSpecPods.getDisableMetricsExporter(), resourceSpecPod.getDisableMetricsExporter());
            assertEquals(dtoSpecPods.getDisableMetricsExporter(), resourceSpecPod.getDisableMetricsExporter());
            final ClusterPodPersistentVolume dtoPV = dtoSpecPods.getPersistentVolume();
            final StackGresPodPersistentVolume resourcePV = resourceSpecPod.getPersistentVolume();
            if (dtoPV != null) {
                assertNotNull(resourcePV);
                assertEquals(dtoPV.getSize(), resourcePV.getSize());
                assertEquals(dtoPV.getStorageClass(), resourcePV.getStorageClass());
            } else {
                assertNull(resourcePV);
            }
            final StackGresClusterSpecLabels resourceMetadataLabels = Optional.ofNullable(resourceSpec.getMetadata()).map(StackGresClusterSpecMetadata::getLabels).orElse(null);
            final ClusterSpecLabels dtoMetadataLabels = Optional.ofNullable(dtoSpec.getMetadata()).map(ClusterSpecMetadata::getLabels).orElse(null);
            if (dtoMetadataLabels != null) {
                assertNotNull(resourceMetadataLabels);
                assertEquals(dtoMetadataLabels.getClusterPods(), resourceMetadataLabels.getClusterPods());
            } else {
                assertNull(resourceMetadataLabels);
            }
            final ClusterPodScheduling podScheduling = dtoSpecPods.getScheduling();
            final StackGresClusterPodScheduling resourceScheduling = resourceSpecPod.getScheduling();
            if (podScheduling != null) {
                assertNotNull(resourceScheduling);
                assertEquals(podScheduling.getNodeSelector(), resourceScheduling.getNodeSelector());
                assertEquals(podScheduling.getNodeAffinity(), resourceScheduling.getNodeAffinity());
            } else {
                assertNull(resourceScheduling);
            }
        }
        final ClusterInitData dtoInitData = dtoSpec.getInitData();
        final StackGresClusterInitData resourceInitData = resourceSpec.getInitData();
        if (dtoInitData != null) {
            assertNotNull(resourceInitData);
            final ClusterRestore dtoRestore = dtoInitData.getRestore();
            final StackGresClusterRestore resourceRestore = resourceInitData.getRestore();
            if (dtoRestore != null) {
                assertNotNull(resourceRestore);
                assertEquals(dtoRestore.getFromBackup().getUid(), resourceRestore.getFromBackup().getUid());
            } else {
                assertNull(resourceRestore);
            }
            if (dtoInitData.getScripts() != null) {
                assertNotNull(resourceInitData.getScripts());
                assertEquals(dtoInitData.getScripts().size(), resourceInitData.getScripts().size());
                Seq.zip(dtoInitData.getScripts(), resourceInitData.getScripts()).forEach(entryTuple -> {
                    ClusterScriptEntry dtoEntry = entryTuple.v1;
                    StackGresClusterScriptEntry resourceEntry = entryTuple.v2;
                    assertEquals(dtoEntry.getDatabase(), resourceEntry.getDatabase());
                    assertEquals(dtoEntry.getName(), resourceEntry.getName());
                    assertEquals(dtoEntry.getScript(), resourceEntry.getScript());
                    final ClusterScriptFrom dtoScriptFrom = dtoEntry.getScriptFrom();
                    final StackGresClusterScriptFrom resourceScriptFrom = resourceEntry.getScriptFrom();
                    if (dtoScriptFrom != null) {
                        assertNotNull(resourceScriptFrom);
                        final SecretKeySelector dtoSecretKeyRef = dtoScriptFrom.getSecretKeyRef();
                        final SecretKeySelector resourceSecretKeyRef = resourceScriptFrom.getSecretKeyRef();
                        if (dtoSecretKeyRef != null) {
                            assertNotNull(resourceSecretKeyRef);
                            assertEquals(dtoSecretKeyRef.getName(), resourceSecretKeyRef.getName());
                            assertEquals(dtoSecretKeyRef.getKey(), resourceSecretKeyRef.getKey());
                        } else {
                            assertNull(resourceSecretKeyRef);
                        }
                        final ConfigMapKeySelector resourceConfigMapKeyRef = resourceScriptFrom.getConfigMapKeyRef();
                        final ConfigMapKeySelector dtoConfigMapKeyRef = dtoScriptFrom.getConfigMapKeyRef();
                        if (dtoConfigMapKeyRef != null) {
                            assertNotNull(resourceConfigMapKeyRef);
                            assertEquals(dtoConfigMapKeyRef.getName(), resourceConfigMapKeyRef.getName());
                            assertEquals(dtoConfigMapKeyRef.getKey(), resourceConfigMapKeyRef.getKey());
                        } else {
                            assertNull(resourceConfigMapKeyRef);
                        }
                    } else {
                        assertNull(resourceScriptFrom);
                    }
                });
            }
        } else {
            assertNull(resourceInitData);
        }
        if (dtoSpec.getDistributedLogs() != null) {
            assertNotNull(resourceSpec.getDistributedLogs());
            assertEquals(dtoSpec.getDistributedLogs().getDistributedLogs(), resourceSpec.getDistributedLogs().getDistributedLogs());
        } else {
            assertNull(resourceSpec.getDistributedLogs());
        }
    } else {
        assertNull(resourceSpec);
    }
}
Also used : StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) ClusterSpecMetadata(io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata) Metadata(io.stackgres.apiweb.dto.Metadata) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ClusterSpec(io.stackgres.apiweb.dto.cluster.ClusterSpec) ClusterConfiguration(io.stackgres.apiweb.dto.cluster.ClusterConfiguration) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ClusterPodScheduling(io.stackgres.apiweb.dto.cluster.ClusterPodScheduling) StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) SecretKeySelector(io.stackgres.common.crd.SecretKeySelector) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) ClusterPod(io.stackgres.apiweb.dto.cluster.ClusterPod) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) ClusterPodPersistentVolume(io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ClusterRestore(io.stackgres.apiweb.dto.cluster.ClusterRestore) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) ClusterSpecLabels(io.stackgres.apiweb.dto.cluster.ClusterSpecLabels) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom) ConfigMapKeySelector(io.stackgres.common.crd.ConfigMapKeySelector)

Example 4 with StackGresPodPersistentVolume

use of io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume 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;
}
Also used : StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresDistributedLogsNonProduction(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterNonProduction(io.stackgres.common.crd.sgcluster.StackGresClusterNonProduction) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)

Example 5 with StackGresPodPersistentVolume

use of io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume in project stackgres by ongres.

the class ClusterTransformer method getCustomResourceSpec.

private StackGresClusterSpec getCustomResourceSpec(ClusterSpec source) {
    if (source == null) {
        return null;
    }
    StackGresClusterSpec transformation = new StackGresClusterSpec();
    transformation.setPostgres(new StackGresClusterPostgres());
    transformation.getPostgres().setVersion(source.getPostgres().getVersion());
    transformation.getPostgres().setExtensions(Optional.ofNullable(source.getPostgres().getExtensions()).stream().flatMap(List::stream).map(this::getCustomResourceExtension).collect(ImmutableList.toImmutableList()));
    final ClusterSsl sourceClusterSsl = source.getPostgres().getSsl();
    if (sourceClusterSsl != null) {
        transformation.getPostgres().setSsl(new StackGresClusterSsl());
        transformation.getPostgres().getSsl().setEnabled(sourceClusterSsl.getEnabled());
        transformation.getPostgres().getSsl().setCertificateSecretKeySelector(sourceClusterSsl.getCertificateSecretKeySelector());
        transformation.getPostgres().getSsl().setPrivateKeySecretKeySelector(sourceClusterSsl.getPrivateKeySecretKeySelector());
    }
    final ClusterConfiguration sourceClusterConfiguration = source.getConfigurations();
    if (sourceClusterConfiguration != null) {
        transformation.setConfiguration(new StackGresClusterConfiguration());
        transformation.getConfiguration().setBackupConfig(source.getConfigurations().getSgBackupConfig());
        transformation.getConfiguration().setConnectionPoolingConfig(source.getConfigurations().getSgPoolingConfig());
        transformation.getConfiguration().setPostgresConfig(source.getConfigurations().getSgPostgresConfig());
    }
    transformation.setInstances(source.getInstances());
    transformation.setNonProduction(getCustomResourceNonProduction(source.getNonProduction()));
    transformation.setPrometheusAutobind(source.getPrometheusAutobind());
    transformation.setResourceProfile(source.getSgInstanceProfile());
    final ClusterSpecMetadata specMetadata = source.getMetadata();
    if (specMetadata != null) {
        transformation.setMetadata(new StackGresClusterSpecMetadata());
        final ClusterSpecAnnotations sourceAnnotations = specMetadata.getAnnotations();
        if (sourceAnnotations != null) {
            StackGresClusterSpecAnnotations targetAnnotations = new StackGresClusterSpecAnnotations();
            targetAnnotations.setAllResources(sourceAnnotations.getAllResources());
            targetAnnotations.setClusterPods(sourceAnnotations.getClusterPods());
            targetAnnotations.setServices(sourceAnnotations.getServices());
            targetAnnotations.setPrimaryService(sourceAnnotations.getPrimaryService());
            targetAnnotations.setReplicasService(sourceAnnotations.getReplicasService());
            transformation.getMetadata().setAnnotations(targetAnnotations);
        }
        final ClusterSpecLabels sourceLabels = specMetadata.getLabels();
        if (sourceLabels != null) {
            StackGresClusterSpecLabels targetLabels = new StackGresClusterSpecLabels();
            targetLabels.setClusterPods(sourceLabels.getClusterPods());
            transformation.getMetadata().setLabels(targetLabels);
        }
    }
    final ClusterPostgresServices sourcePostgresServices = source.getPostgresServices();
    if (sourcePostgresServices != null) {
        transformation.setPostgresServices(new StackGresClusterPostgresServices());
        final StackGresClusterPostgresServices targetPostgresService = transformation.getPostgresServices();
        final PostgresService sourcePrimaryService = sourcePostgresServices.getPrimary();
        if (sourcePrimaryService != null) {
            targetPostgresService.setPrimary(new StackGresPostgresService());
            final StackGresPostgresService targetPrimaryService = targetPostgresService.getPrimary();
            targetPrimaryService.setType(sourcePrimaryService.getType());
            targetPrimaryService.setEnabled(sourcePrimaryService.getEnabled());
        }
        final PostgresService sourceReplicaService = sourcePostgresServices.getReplicas();
        if (sourceReplicaService != null) {
            targetPostgresService.setReplicas(new StackGresPostgresService());
            final StackGresPostgresService targetReplicaService = targetPostgresService.getReplicas();
            targetReplicaService.setEnabled(sourceReplicaService.getEnabled());
            targetReplicaService.setType(sourceReplicaService.getType());
        }
    }
    final ClusterInitData sourceInitData = source.getInitData();
    if (sourceInitData != null) {
        final StackGresClusterInitData targetInitData = new StackGresClusterInitData();
        transformation.setInitData(targetInitData);
        if (sourceInitData.getRestore() != null) {
            targetInitData.setRestore(getCustomResourceRestore(sourceInitData.getRestore()));
        }
        if (sourceInitData.getScripts() != null) {
            targetInitData.setScripts(getCustomResourceScripts(sourceInitData.getScripts()));
        }
    }
    final StackGresClusterPod targetPod = new StackGresClusterPod();
    transformation.setPod(targetPod);
    targetPod.setPersistentVolume(new StackGresPodPersistentVolume());
    targetPod.getPersistentVolume().setStorageClass(source.getPods().getPersistentVolume().getStorageClass());
    targetPod.getPersistentVolume().setSize(source.getPods().getPersistentVolume().getSize());
    targetPod.setDisableConnectionPooling(source.getPods().getDisableConnectionPooling());
    targetPod.setDisableMetricsExporter(source.getPods().getDisableMetricsExporter());
    targetPod.setDisablePostgresUtil(source.getPods().getDisablePostgresUtil());
    targetPod.setScheduling(Optional.ofNullable(source.getPods().getScheduling()).map(sourceScheduling -> {
        return new ClusterPodSchedulingConverter().to(sourceScheduling);
    }).orElse(null));
    transformation.setDistributedLogs(getCustomResourceDistributedLogs(source.getDistributedLogs()));
    return transformation;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ClusterPodSchedulingConverter(io.stackgres.apiweb.transformer.converter.cluster.ClusterPodSchedulingConverter) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) ClusterPostgresServices(io.stackgres.apiweb.dto.cluster.ClusterPostgresServices) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ClusterConfiguration(io.stackgres.apiweb.dto.cluster.ClusterConfiguration) ClusterSpecAnnotations(io.stackgres.apiweb.dto.cluster.ClusterSpecAnnotations) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) PostgresService(io.stackgres.apiweb.app.postgres.service.PostgresService) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) ClusterSpecMetadata(io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) ClusterSsl(io.stackgres.apiweb.dto.cluster.ClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) ClusterSpecLabels(io.stackgres.apiweb.dto.cluster.ClusterSpecLabels) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)

Aggregations

StackGresPodPersistentVolume (io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume)5 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)4 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)4 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)4 ClusterConfiguration (io.stackgres.apiweb.dto.cluster.ClusterConfiguration)3 ClusterInitData (io.stackgres.apiweb.dto.cluster.ClusterInitData)3 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)3 StackGresClusterSpecMetadata (io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)3 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 ClusterPod (io.stackgres.apiweb.dto.cluster.ClusterPod)2 ClusterPodPersistentVolume (io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume)2 ClusterScriptFrom (io.stackgres.apiweb.dto.cluster.ClusterScriptFrom)2 ClusterSpec (io.stackgres.apiweb.dto.cluster.ClusterSpec)2 ClusterSpecLabels (io.stackgres.apiweb.dto.cluster.ClusterSpecLabels)2 ClusterSpecMetadata (io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata)2 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)2 StackGresClusterPodScheduling (io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling)2 StackGresClusterPostgres (io.stackgres.common.crd.sgcluster.StackGresClusterPostgres)2 StackGresClusterScriptEntry (io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)2 StackGresClusterScriptFrom (io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom)2