Search in sources :

Example 1 with StackGresClusterScriptFrom

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

the class ClusterConstraintValidatorTest method validScriptFromSecret_shouldPass.

@Test
void validScriptFromSecret_shouldPass() throws ValidationFailed {
    StackGresClusterReview review = getValidReview();
    review.getRequest().getObject().getSpec().setInitData(new StackGresClusterInitData());
    review.getRequest().getObject().getSpec().getInitData().setScripts(new ArrayList<>());
    review.getRequest().getObject().getSpec().getInitData().getScripts().add(new StackGresClusterScriptEntry());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).setScriptFrom(new StackGresClusterScriptFrom());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().setSecretKeyRef(new SecretKeySelector());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getSecretKeyRef().setName("test");
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getSecretKeyRef().setKey("test");
    validator.validate(review);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) SecretKeySelector(io.stackgres.common.crd.SecretKeySelector) ConstraintValidationTest(io.stackgres.operator.validation.ConstraintValidationTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with StackGresClusterScriptFrom

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

the class ClusterConstraintValidatorTest method scriptFromConfigMapWithEmptyName_shouldFail.

@Test
void scriptFromConfigMapWithEmptyName_shouldFail() {
    StackGresClusterReview review = getValidReview();
    review.getRequest().getObject().getSpec().setInitData(new StackGresClusterInitData());
    review.getRequest().getObject().getSpec().getInitData().setScripts(new ArrayList<>());
    review.getRequest().getObject().getSpec().getInitData().getScripts().add(new StackGresClusterScriptEntry());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).setScriptFrom(new StackGresClusterScriptFrom());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().setConfigMapKeyRef(new ConfigMapKeySelector());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().setName("");
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().setKey("test");
    checkErrorCause(SecretKeySelector.class, "spec.initData.scripts[0].scriptFrom.configMapKeyRef.name", "isNameNotEmpty", review, AssertTrue.class);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ConfigMapKeySelector(io.stackgres.common.crd.ConfigMapKeySelector) ConstraintValidationTest(io.stackgres.operator.validation.ConstraintValidationTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with StackGresClusterScriptFrom

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

the class ClusterConstraintValidatorTest method missingScriptFrom_shouldFail.

@Test
void missingScriptFrom_shouldFail() {
    StackGresClusterReview review = getValidReview();
    review.getRequest().getObject().getSpec().setInitData(new StackGresClusterInitData());
    review.getRequest().getObject().getSpec().getInitData().setScripts(new ArrayList<>());
    review.getRequest().getObject().getSpec().getInitData().getScripts().add(new StackGresClusterScriptEntry());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).setScriptFrom(new StackGresClusterScriptFrom());
    checkErrorCause(StackGresClusterScriptFrom.class, new String[] { "spec.initData.scripts[0].scriptFrom.secretKeyRef", "spec.initData.scripts[0].scriptFrom.configMapKeyRef" }, "isSecretKeySelectorAndConfigMapKeySelectorMutuallyExclusiveAndRequired", review, AssertTrue.class);
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ConstraintValidationTest(io.stackgres.operator.validation.ConstraintValidationTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with StackGresClusterScriptFrom

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

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

the class ScriptsConfigValidator method checkConfigMapsSelectors.

private void checkConfigMapsSelectors(StackGresClusterReview review, List<StackGresClusterScriptEntry> scripts) throws ValidationFailed {
    String clusterNamespace = review.getRequest().getObject().getMetadata().getNamespace();
    List<ConfigMapKeySelector> configMapsSelectors = scripts.stream().map(StackGresClusterScriptEntry::getScriptFrom).filter(Objects::nonNull).map(StackGresClusterScriptFrom::getConfigMapKeyRef).filter(Objects::nonNull).collect(Collectors.toList());
    for (ConfigMapKeySelector configMapRef : configMapsSelectors) {
        Optional<ConfigMap> scriptConfigMap = configMapFinder.findByNameAndNamespace(configMapRef.getName(), clusterNamespace);
        if (!scriptConfigMap.isPresent()) {
            fail(invalidReference, "Referenced ConfigMap " + configMapRef.getName() + " does not exists in namespace " + clusterNamespace);
        } else {
            Set<String> configMapKeys = ImmutableSet.<String>builder().addAll(Optional.ofNullable(scriptConfigMap.get().getData()).map(Map::keySet).orElse(ImmutableSet.of())).addAll(Optional.ofNullable(scriptConfigMap.get().getBinaryData()).map(Map::keySet).orElse(ImmutableSet.of())).build();
            if (!configMapKeys.contains(configMapRef.getKey())) {
                fail(invalidReference, "Key " + configMapRef.getKey() + " does not exists in ConfigMap " + configMapRef.getName());
            }
        }
    }
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) Map(java.util.Map) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapKeySelector(io.fabric8.kubernetes.api.model.ConfigMapKeySelector)

Aggregations

StackGresClusterScriptFrom (io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom)15 StackGresClusterScriptEntry (io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)12 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)11 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)9 ConstraintValidationTest (io.stackgres.operator.validation.ConstraintValidationTest)9 Test (org.junit.jupiter.api.Test)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ConfigMapKeySelector (io.stackgres.common.crd.ConfigMapKeySelector)7 SecretKeySelector (io.stackgres.common.crd.SecretKeySelector)6 ClusterConfiguration (io.stackgres.apiweb.dto.cluster.ClusterConfiguration)2 ClusterInitData (io.stackgres.apiweb.dto.cluster.ClusterInitData)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 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)2 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)2 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)2 StackGresPodPersistentVolume (io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1