Search in sources :

Example 1 with AdmissionRequest

use of io.stackgres.operatorframework.admissionwebhook.AdmissionRequest 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)

Aggregations

TextNode (com.fasterxml.jackson.databind.node.TextNode)1 JsonPointer (com.github.fge.jackson.jsonpointer.JsonPointer)1 AddOperation (com.github.fge.jsonpatch.AddOperation)1 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)1 ReplaceOperation (com.github.fge.jsonpatch.ReplaceOperation)1 ImmutableList (com.google.common.collect.ImmutableList)1 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)1 StackGresClusterInstalledExtension (io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)1 StackGresClusterPostgres (io.stackgres.common.crd.sgcluster.StackGresClusterPostgres)1 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)1 ExtensionUtil (io.stackgres.common.extension.ExtensionUtil)1 StackGresExtensionMetadata (io.stackgres.common.extension.StackGresExtensionMetadata)1 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)1 ClusterExtensionMetadataManager (io.stackgres.operator.mutation.ClusterExtensionMetadataManager)1 AdmissionRequest (io.stackgres.operatorframework.admissionwebhook.AdmissionRequest)1 Operation (io.stackgres.operatorframework.admissionwebhook.Operation)1 List (java.util.List)1 Optional (java.util.Optional)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1