Search in sources :

Example 96 with StackGresCluster

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

the class ExtensionsChannelMutator method mutate.

@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
    if (review.getRequest().getOperation() == Operation.CREATE || review.getRequest().getOperation() == Operation.UPDATE) {
        ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
        Optional.of(review).map(StackGresClusterReview::getRequest).map(AdmissionRequest<StackGresCluster>::getObject).ifPresent(cluster -> {
            Optional.of(cluster).map(StackGresCluster::getSpec).map(StackGresClusterSpec::getPostgres).map(StackGresClusterPostgres::getExtensions).stream().flatMap(extensions -> Seq.seq(extensions).zipWithIndex()).forEach(Unchecked.consumer(extension -> {
                final JsonPointer extensionVersionPointer = CLUSTER_CONFIG_POINTER.append("postgres").append("extensions").append(extension.v2.intValue()).append("version");
                final StackGresExtensionMetadata extensionMetadata;
                try {
                    extensionMetadata = extensionMetadataManager.getExtensionCandidateSameMajorBuild(cluster, extension.v1);
                } catch (Exception ex) {
                    LOGGER.warn("Unable to retrieve candidate with same major build for extension", ex);
                    return;
                }
                final StackGresClusterInstalledExtension installedExtension = ExtensionUtil.getInstalledExtension(extension.v1, extensionMetadata);
                final TextNode extensionVersion = new TextNode(installedExtension.getVersion());
                if (extension.v1.getVersion() == null) {
                    operations.add(new AddOperation(extensionVersionPointer, extensionVersion));
                } else if (!installedExtension.getVersion().equals(extension.v1.getVersion())) {
                    operations.add(new ReplaceOperation(extensionVersionPointer, extensionVersion));
                }
            }));
        });
        return operations.build();
    }
    return ImmutableList.of();
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) Unchecked(org.jooq.lambda.Unchecked) Logger(org.slf4j.Logger) AddOperation(com.github.fge.jsonpatch.AddOperation) Operation(io.stackgres.operatorframework.admissionwebhook.Operation) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) ReplaceOperation(com.github.fge.jsonpatch.ReplaceOperation) Seq(org.jooq.lambda.Seq) TextNode(com.fasterxml.jackson.databind.node.TextNode) Inject(javax.inject.Inject) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresExtensionMetadata(io.stackgres.common.extension.StackGresExtensionMetadata) ClusterExtensionMetadataManager(io.stackgres.operator.mutation.ClusterExtensionMetadataManager) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) ExtensionUtil(io.stackgres.common.extension.ExtensionUtil) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) AdmissionRequest(io.stackgres.operatorframework.admissionwebhook.AdmissionRequest) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) TextNode(com.fasterxml.jackson.databind.node.TextNode) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) StackGresExtensionMetadata(io.stackgres.common.extension.StackGresExtensionMetadata) AddOperation(com.github.fge.jsonpatch.AddOperation) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ReplaceOperation(com.github.fge.jsonpatch.ReplaceOperation)

Example 97 with StackGresCluster

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

the class PoolingConfigValidator method checkIfPoolingConfigExists.

private void checkIfPoolingConfigExists(StackGresClusterReview review, String onError) throws ValidationFailed {
    StackGresCluster cluster = review.getRequest().getObject();
    String poolingConfig = cluster.getSpec().getConfiguration().getConnectionPoolingConfig();
    String namespace = review.getRequest().getObject().getMetadata().getNamespace();
    if (Boolean.FALSE.equals(cluster.getSpec().getPod().getDisableConnectionPooling())) {
        checkIfProvided(poolingConfig, "sgPoolingConfig");
        Optional<StackGresPoolingConfig> poolingConfigOpt = configFinder.findByNameAndNamespace(poolingConfig, namespace);
        if (poolingConfigOpt.isEmpty()) {
            fail(onError);
        }
    }
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig)

Example 98 with StackGresCluster

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

the class PostgresConfigValidator method validate.

@Override
public void validate(StackGresClusterReview review) throws ValidationFailed {
    StackGresCluster cluster = review.getRequest().getObject();
    if (cluster == null) {
        return;
    }
    String givenPgVersion = cluster.getSpec().getPostgres().getVersion();
    String pgConfig = cluster.getSpec().getConfiguration().getPostgresConfig();
    checkIfProvided(givenPgVersion, "postgres version");
    checkIfProvided(pgConfig, "sgPostgresConfig");
    if (givenPgVersion != null && !isPostgresVersionSupported(givenPgVersion)) {
        final String message = "Unsupported postgres version " + givenPgVersion + ".  Supported postgres versions are: " + StackGresComponent.POSTGRESQL.getOrderedVersions().toString(", ");
        fail(errorPostgresMismatchUri, message);
    }
    String givenMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(givenPgVersion);
    String namespace = cluster.getMetadata().getNamespace();
    switch(review.getRequest().getOperation()) {
        case CREATE:
            validateAgainstConfiguration(givenMajorVersion, pgConfig, namespace);
            break;
        case UPDATE:
            StackGresCluster oldCluster = review.getRequest().getOldObject();
            String oldPgConfig = oldCluster.getSpec().getConfiguration().getPostgresConfig();
            if (!oldPgConfig.equals(pgConfig)) {
                validateAgainstConfiguration(givenMajorVersion, pgConfig, namespace);
            }
            long givenMajorVersionIndex = StackGresComponent.POSTGRESQL.getOrderedMajorVersions().zipWithIndex().filter(t -> t.v1.equals(givenMajorVersion)).map(Tuple2::v2).findAny().get();
            String oldPgVersion = oldCluster.getSpec().getPostgres().getVersion();
            String oldMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(oldPgVersion);
            long oldMajorVersionIndex = StackGresComponent.POSTGRESQL.getOrderedMajorVersions().zipWithIndex().filter(t -> t.v1.equals(oldMajorVersion)).map(Tuple2::v2).findAny().get();
            if (givenMajorVersionIndex > oldMajorVersionIndex) {
                fail(errorForbiddenUpdateUri, "postgres version can not be changed to a previous major version");
            }
            break;
        default:
    }
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ValidationType(io.stackgres.operator.validation.ValidationType) Singleton(javax.inject.Singleton) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) StackGresComponent(io.stackgres.common.StackGresComponent) List(java.util.List) Tuple2(org.jooq.lambda.tuple.Tuple2) ErrorType(io.stackgres.common.ErrorType) Optional(java.util.Optional) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) Tuple2(org.jooq.lambda.tuple.Tuple2)

Example 99 with StackGresCluster

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

the class RestoreConfigValidator method validate.

@Override
public void validate(StackGresClusterReview review) throws ValidationFailed {
    StackGresCluster cluster = review.getRequest().getObject();
    Optional<StackGresClusterRestore> restoreOpt = Optional.of(cluster.getSpec()).map(StackGresClusterSpec::getInitData).map(StackGresClusterInitData::getRestore);
    checkRestoreConfig(review, restoreOpt);
    if (restoreOpt.isPresent()) {
        StackGresClusterRestore restoreConfig = restoreOpt.get();
        checkBackup(review, restoreConfig);
    }
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData)

Example 100 with StackGresCluster

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

Aggregations

StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)127 Test (org.junit.jupiter.api.Test)48 Optional (java.util.Optional)32 List (java.util.List)31 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)27 StackGresClusterInstalledExtension (io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)25 ImmutableList (com.google.common.collect.ImmutableList)24 Inject (javax.inject.Inject)22 ArrayList (java.util.ArrayList)19 StackGresClusterStatus (io.stackgres.common.crd.sgcluster.StackGresClusterStatus)18 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)15 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 StackGresComponent (io.stackgres.common.StackGresComponent)15 StackGresClusterPodStatus (io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus)14 ExtensionInstaller (io.stackgres.common.extension.ExtensionManager.ExtensionInstaller)14 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)13 Collectors (java.util.stream.Collectors)13 BeforeEach (org.junit.jupiter.api.BeforeEach)13 ExtensionUninstaller (io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller)12 JsonUtil (io.stackgres.testutil.JsonUtil)12