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());
}
}
}
}
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());
}
}
}
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);
}
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);
}
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);
}
Aggregations