Search in sources :

Example 6 with StackGresClusterScriptEntry

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

Example 7 with StackGresClusterScriptEntry

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

the class ScriptsConfigValidator method checkSecretKeySelectors.

private void checkSecretKeySelectors(StackGresClusterReview review, List<StackGresClusterScriptEntry> scripts) throws ValidationFailed {
    String clusterNamespace = review.getRequest().getObject().getMetadata().getNamespace();
    List<SecretKeySelector> secretKeySelectors = scripts.stream().map(StackGresClusterScriptEntry::getScriptFrom).filter(Objects::nonNull).map(StackGresClusterScriptFrom::getSecretKeyRef).filter(Objects::nonNull).collect(Collectors.toList());
    for (SecretKeySelector secretRef : secretKeySelectors) {
        Optional<Secret> scriptSecret = secretFinder.findByNameAndNamespace(secretRef.getName(), clusterNamespace);
        if (!scriptSecret.isPresent()) {
            fail(invalidReference, "Referenced Secret " + secretRef.getName() + " does not exists in namespace " + clusterNamespace);
        } else if (!scriptSecret.get().getData().containsKey(secretRef.getKey())) {
            fail(invalidReference, "Key " + secretRef.getKey() + " does not exists in Secret " + secretRef.getName());
        }
    }
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) SecretKeySelector(io.fabric8.kubernetes.api.model.SecretKeySelector)

Example 8 with StackGresClusterScriptEntry

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

the class ClusterConstraintValidatorTest method validScriptFromConfigMapAndSecret_shouldFail.

@Test
void validScriptFromConfigMapAndSecret_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("test");
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().setKey("test");
    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");
    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) SecretKeySelector(io.stackgres.common.crd.SecretKeySelector) 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 9 with StackGresClusterScriptEntry

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

the class ClusterConstraintValidatorTest method scriptFromSecretWithEmptyKey_shouldFail.

@Test
void scriptFromSecretWithEmptyKey_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().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("");
    checkErrorCause(SecretKeySelector.class, "spec.initData.scripts[0].scriptFrom.secretKeyRef.key", "isKeyNotEmpty", 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) 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 10 with StackGresClusterScriptEntry

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

the class ClusterConstraintValidatorTest method validScriptFromConfigMap_shouldPass.

@Test
void validScriptFromConfigMap_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().setConfigMapKeyRef(new ConfigMapKeySelector());
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().setName("test");
    review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().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) ConfigMapKeySelector(io.stackgres.common.crd.ConfigMapKeySelector) ConstraintValidationTest(io.stackgres.operator.validation.ConstraintValidationTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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