Search in sources :

Example 16 with StackGresClusterScriptEntry

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

the class ScriptsConfigValidatorTest method givenACreationUsingScriptFromSecret_shouldValidateSecretKeyReference.

@Test
void givenACreationUsingScriptFromSecret_shouldValidateSecretKeyReference() throws ValidationFailed {
    final StackGresClusterReview review = getCreationReview();
    String randomSecretKey = StringUtil.generateRandom();
    String randomSecretName = StringUtil.generateRandom();
    final StackGresCluster cluster = review.getRequest().getObject();
    prepareForSecret(review, randomSecretName, randomSecretKey);
    final String namespace = cluster.getMetadata().getNamespace();
    when(secretFinder.findByNameAndNamespace(randomSecretName, namespace)).thenReturn(Optional.of(new SecretBuilder().withNewMetadata().withName(randomSecretName).withNamespace(namespace).endMetadata().withData(ImmutableMap.of(randomSecretKey, "CREATE DATABASE test;")).build()));
    validator.validate(review);
    final List<StackGresClusterScriptEntry> scripts = cluster.getSpec().getInitData().getScripts();
    verify(secretFinder, times(scripts.size())).findByNameAndNamespace(randomSecretName, namespace);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) Test(org.junit.jupiter.api.Test)

Example 17 with StackGresClusterScriptEntry

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

the class ScriptsConfigValidatorTest method givenACreationUsingScriptsFromBinaryConfigMap_shouldValidateConfigMapReference.

@Test
void givenACreationUsingScriptsFromBinaryConfigMap_shouldValidateConfigMapReference() throws ValidationFailed {
    final StackGresClusterReview review = getCreationReview();
    String randomConfigMapName = StringUtil.generateRandom();
    String randomConfigMapKey = StringUtil.generateRandom();
    prepareForConfigMap(review, randomConfigMapName, randomConfigMapKey);
    final StackGresCluster cluster = review.getRequest().getObject();
    final String namespace = cluster.getMetadata().getNamespace();
    when(configMapFinder.findByNameAndNamespace(randomConfigMapName, namespace)).thenReturn(Optional.of(new ConfigMapBuilder().withNewMetadata().withName(randomConfigMapName).withNamespace(namespace).endMetadata().withBinaryData(ImmutableMap.of(randomConfigMapKey, "CREATE DATABASE test;")).build()));
    validator.validate(review);
    final List<StackGresClusterScriptEntry> scripts = cluster.getSpec().getInitData().getScripts();
    verify(configMapFinder, times(scripts.size())).findByNameAndNamespace(randomConfigMapName, namespace);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) Test(org.junit.jupiter.api.Test)

Example 18 with StackGresClusterScriptEntry

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

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

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

Aggregations

StackGresClusterScriptEntry (io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)21 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)15 Test (org.junit.jupiter.api.Test)15 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)14 StackGresClusterScriptFrom (io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom)12 ConstraintValidationTest (io.stackgres.operator.validation.ConstraintValidationTest)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 ConfigMapKeySelector (io.stackgres.common.crd.ConfigMapKeySelector)6 SecretKeySelector (io.stackgres.common.crd.SecretKeySelector)5 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)5 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)3 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)2 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)2 StackGresClusterPodScheduling (io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling)2 StackGresClusterSpecMetadata (io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)2 StackGresPodPersistentVolume (io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 ConfigMapKeySelector (io.fabric8.kubernetes.api.model.ConfigMapKeySelector)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 Secret (io.fabric8.kubernetes.api.model.Secret)1