Search in sources :

Example 31 with StackGresClusterSpec

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

the class ScriptsConfigValidator method validate.

@Override
public void validate(StackGresClusterReview review) throws ValidationFailed {
    StackGresCluster cluster = review.getRequest().getObject();
    List<StackGresClusterScriptEntry> scripts = Optional.of(cluster.getSpec()).map(StackGresClusterSpec::getInitData).map(StackGresClusterInitData::getScripts).orElse(ImmutableList.of());
    checkScriptsConfig(review, scripts);
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)

Example 32 with StackGresClusterSpec

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

the class ClusterDefaultResourcesMutator method mutate.

@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
    if (review.getRequest().getOperation() == Operation.CREATE) {
        StackGresCluster targetCluster = review.getRequest().getObject();
        String targetNamespace = targetCluster.getMetadata().getNamespace();
        StackGresClusterSpec spec = targetCluster.getSpec();
        if (installedNamespace.equals(targetNamespace)) {
            if (isEmpty(spec.getConfiguration().getBackupConfig())) {
                spec.getConfiguration().setBackupConfig(defaultBackup.getMetadata().getName());
            }
            if (isEmpty(spec.getConfiguration().getPostgresConfig())) {
                spec.getConfiguration().setPostgresConfig(defaultPostgresConfig.getMetadata().getName());
            }
            if (isEmpty(spec.getResourceProfile())) {
                spec.setResourceProfile(defaultProfile.getMetadata().getName());
            }
            if (isEmpty(spec.getConfiguration().getConnectionPoolingConfig())) {
                spec.getConfiguration().setConnectionPoolingConfig(defaultPgBouncerConfig.getMetadata().getName());
            }
        }
    }
    return new ArrayList<>();
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ArrayList(java.util.ArrayList)

Example 33 with StackGresClusterSpec

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

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

the class ClusterRequiredResourcesGeneratorTest method givenClusterWithNoPoolingConfig_shouldNotFail.

@Test
void givenClusterWithNoPoolingConfig_shouldNotFail() {
    final ObjectMeta metadata = cluster.getMetadata();
    final String clusterNamespace = metadata.getNamespace();
    final StackGresClusterSpec clusterSpec = cluster.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
    final String backupConfigName = clusterConfiguration.getBackupConfig();
    mockBackupConfig(clusterNamespace, backupConfigName);
    final String postgresConfigName = clusterConfiguration.getPostgresConfig();
    mockPgConfig(clusterNamespace, postgresConfigName);
    final String connectionPoolingConfig = clusterConfiguration.getConnectionPoolingConfig();
    when(poolingConfigFinder.findByNameAndNamespace(connectionPoolingConfig, clusterNamespace)).thenReturn(Optional.empty());
    final String resourceProfile = clusterSpec.getResourceProfile();
    when(profileConfigFinder.findByNameAndNamespace(resourceProfile, clusterNamespace)).thenReturn(Optional.of(instanceProfile));
    when(backupScanner.getResources()).thenReturn(backups);
    mockSecrets(clusterNamespace);
    generator.getRequiredResources(cluster);
    verify(backupConfigFinder).findByNameAndNamespace(backupConfigName, clusterNamespace);
    verify(postgresConfigFinder).findByNameAndNamespace(postgresConfigName, clusterNamespace);
    verify(poolingConfigFinder).findByNameAndNamespace(connectionPoolingConfig, clusterNamespace);
    verify(profileConfigFinder).findByNameAndNamespace(resourceProfile, clusterNamespace);
    verify(backupScanner, atLeastOnce()).getResources();
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 35 with StackGresClusterSpec

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

the class ClusterRequiredResourcesGeneratorTest method givenAClusterWithInvalidPgConfig_getRequiredResourcesShouldFail.

@Test
void givenAClusterWithInvalidPgConfig_getRequiredResourcesShouldFail() {
    final ObjectMeta metadata = cluster.getMetadata();
    final String clusterName = metadata.getName();
    final String clusterNamespace = metadata.getNamespace();
    final StackGresClusterSpec clusterSpec = cluster.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
    final String postgresConfigName = clusterConfiguration.getPostgresConfig();
    when(postgresConfigFinder.findByNameAndNamespace(postgresConfigName, clusterNamespace)).thenReturn(Optional.empty());
    assertException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent SGPostgresConfig " + postgresConfigName);
    verify(postgresConfigFinder).findByNameAndNamespace(postgresConfigName, clusterNamespace);
    verify(profileConfigFinder, never()).findByNameAndNamespace(any(), any());
    verify(backupConfigFinder, never()).findByNameAndNamespace(any(), any());
    verify(poolingConfigFinder, never()).findByNameAndNamespace(any(), any());
    verify(backupScanner, never()).getResources();
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)46 Test (org.junit.jupiter.api.Test)26 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)23 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)18 QuarkusTest (io.quarkus.test.junit.QuarkusTest)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)15 List (java.util.List)11 Optional (java.util.Optional)11 Inject (javax.inject.Inject)10 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)7 Collectors (java.util.stream.Collectors)7 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)6 StackGresClusterPostgres (io.stackgres.common.crd.sgcluster.StackGresClusterPostgres)6 StackGresComponent (io.stackgres.common.StackGresComponent)5 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)5 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)5 ValidationFailed (io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed)5