Search in sources :

Example 6 with StackGresClusterRestore

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

the class ClusterRequiredResourcesGenerator method getRequiredResources.

@Override
public List<HasMetadata> getRequiredResources(StackGresCluster config) {
    final ObjectMeta metadata = config.getMetadata();
    final String clusterName = metadata.getName();
    final String clusterNamespace = metadata.getNamespace();
    final StackGresClusterSpec spec = config.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = spec.getConfiguration();
    final StackGresPostgresConfig clusterPgConfig = postgresConfigFinder.findByNameAndNamespace(clusterConfiguration.getPostgresConfig(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent SGPostgresConfig postgresconf"));
    final StackGresProfile clusterProfile = profileFinder.findByNameAndNamespace(spec.getResourceProfile(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent " + StackGresProfile.KIND + " " + spec.getResourceProfile()));
    final Optional<StackGresBackupConfig> backupConfig = Optional.ofNullable(clusterConfiguration.getBackupConfig()).flatMap(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, clusterNamespace));
    final Optional<StackGresPoolingConfig> clusterPooling = Optional.ofNullable(clusterConfiguration.getConnectionPoolingConfig()).flatMap(poolingConfigName -> poolingConfigFinder.findByNameAndNamespace(poolingConfigName, clusterNamespace));
    Optional<StackGresClusterRestore> restoreConfig = Optional.ofNullable(config.getSpec().getInitData()).map(StackGresClusterInitData::getRestore);
    final Optional<StackGresBackup> restoreBackup;
    if (restoreConfig.isEmpty()) {
        restoreBackup = Optional.empty();
    } else {
        restoreBackup = restoreConfig.map(restore -> {
            final List<StackGresBackup> backups = backupScanner.getResources();
            return backups.stream().filter(backup -> backup.getMetadata().getUid().equals(restore.getFromBackup().getUid())).peek(backup -> {
                Preconditions.checkNotNull(backup.getStatus(), "Backup is " + BackupPhase.RUNNING.label());
                Preconditions.checkNotNull(backup.getStatus().getProcess(), "Backup is " + BackupPhase.RUNNING.label());
                Preconditions.checkArgument(backup.getStatus().getProcess().getStatus().equals(BackupPhase.COMPLETED.label()), "Backup is " + backup.getStatus().getProcess().getStatus());
            }).findFirst().orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have an invalid restore backup Uid"));
        });
    }
    StackGresClusterContext context = ImmutableStackGresClusterContext.builder().source(config).postgresConfig(clusterPgConfig).stackGresProfile(clusterProfile).backupConfig(backupConfig).poolingConfig(clusterPooling).restoreBackup(restoreBackup).prometheus(getPrometheus(config)).internalScripts(List.of(getPostgresExporterInitScript())).databaseCredentials(secretFinder.findByNameAndNamespace(clusterName, clusterNamespace)).build();
    final List<ResourceGenerator<StackGresClusterContext>> resourceGenerators = generators.getResourceGenerators(context);
    final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
    List<Decorator<StackGresClusterContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
    decorators.forEach(decorator -> decorator.decorate(context, resources));
    return resources;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) Inject(javax.inject.Inject) OperatorPropertyContext(io.stackgres.operator.configuration.OperatorPropertyContext) ResourceGenerationDiscoverer(io.stackgres.operator.conciliation.ResourceGenerationDiscoverer) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) Map(java.util.Map) Prometheus(io.stackgres.operator.common.Prometheus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) CustomResourceScanner(io.stackgres.common.resource.CustomResourceScanner) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) Unchecked(org.jooq.lambda.Unchecked) Logger(org.slf4j.Logger) Resources(com.google.common.io.Resources) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PrometheusConfigSpec(io.stackgres.operator.customresource.prometheus.PrometheusConfigSpec) StandardCharsets(java.nio.charset.StandardCharsets) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) BackupPhase(io.stackgres.common.crd.sgbackup.BackupPhase) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) PrometheusInstallation(io.stackgres.operator.customresource.prometheus.PrometheusInstallation) DecoratorDiscoverer(io.stackgres.operator.conciliation.factory.DecoratorDiscoverer) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) Optional(java.util.Optional) Secret(io.fabric8.kubernetes.api.model.Secret) ResourceFinder(io.stackgres.common.resource.ResourceFinder) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Preconditions(com.google.common.base.Preconditions) OperatorProperty(io.stackgres.common.OperatorProperty) PrometheusConfig(io.stackgres.operator.customresource.prometheus.PrometheusConfig) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) List(java.util.List) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig)

Example 7 with StackGresClusterRestore

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

the class DefaultRestoreMutator method mutate.

@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
    final StackGresClusterInitData initData = review.getRequest().getObject().getSpec().getInitData();
    if (review.getRequest().getOperation() == Operation.CREATE && initData != null) {
        StackGresClusterRestore restore = initData.getRestore();
        if (restore != null) {
            JsonNode target = mapper.valueToTree(restore);
            ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
            operations.addAll(applyDefaults(restorePointer, defaultNode, target));
            return operations.build();
        }
    }
    return ImmutableList.of();
}
Also used : StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation)

Example 8 with StackGresClusterRestore

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

the class DefaultRestoreMutator method init.

@PostConstruct
public void init() throws NoSuchFieldException {
    String initDataJson = ClusterMutator.getJsonMappingField("initData", StackGresClusterSpec.class);
    String restoreJsonField = ClusterMutator.getJsonMappingField("restore", StackGresClusterInitData.class);
    restorePointer = ClusterMutator.CLUSTER_CONFIG_POINTER.append(initDataJson).append(restoreJsonField);
    StackGresClusterRestore defaultRestore = defaultRestoreFactory.buildResource();
    defaultNode = mapper.valueToTree(defaultRestore);
}
Also used : StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) PostConstruct(javax.annotation.PostConstruct)

Example 9 with StackGresClusterRestore

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

the class RestoreConfigValidator method checkBackup.

private void checkBackup(StackGresClusterReview review, StackGresClusterRestore restoreConfig) throws ValidationFailed {
    String backupUid = restoreConfig.getFromBackup().getUid();
    switch(review.getRequest().getOperation()) {
        case CREATE:
            Optional<StackGresBackup> config = findBackup(backupUid);
            if (config.isEmpty()) {
                final String message = "Backup uid " + backupUid + " not found";
                fail(errorCrReferencerUri, message);
            }
            StackGresBackup backup = config.get();
            final StackGresBackupProcess process = backup.getStatus().getProcess();
            if (backup.getStatus() == null || !process.getStatus().equals("Completed")) {
                final String message = "Cannot restore from backup " + backupUid + " because it's not ready";
                fail(errorCrReferencerUri, message);
            }
            String backupMajorVersion = RestoreConfigValidator.getMajorVersion(backup);
            String givenPgVersion = review.getRequest().getObject().getSpec().getPostgres().getVersion();
            String givenMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(givenPgVersion);
            if (!backupMajorVersion.equals(givenMajorVersion)) {
                final String message = "Cannot restore from backup " + backupUid + " because it comes from an incompatible postgres version";
                fail(errorPostgresMismatch, message);
            }
            break;
        case UPDATE:
            StackGresClusterRestore oldRestoreConfig = review.getRequest().getOldObject().getSpec().getInitData().getRestore();
            String oldBackupUid = oldRestoreConfig.getFromBackup().getUid();
            final String message = "Cannot update cluster's restore configuration";
            if (backupUid == null && oldBackupUid != null || backupUid != null && oldBackupUid == null) {
                fail(errorCrReferencerUri, message);
            }
            if (backupUid != null && !backupUid.equals(oldBackupUid)) {
                fail(errorCrReferencerUri, message);
            }
            break;
        default:
    }
}
Also used : StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) StackGresBackupProcess(io.stackgres.common.crd.sgbackup.StackGresBackupProcess)

Example 10 with StackGresClusterRestore

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

Aggregations

StackGresClusterRestore (io.stackgres.common.crd.sgcluster.StackGresClusterRestore)13 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)5 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)4 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)4 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)3 StackGresClusterScriptEntry (io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 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 ClusterRestore (io.stackgres.apiweb.dto.cluster.ClusterRestore)2 ClusterScriptEntry (io.stackgres.apiweb.dto.cluster.ClusterScriptEntry)2 ClusterScriptFrom (io.stackgres.apiweb.dto.cluster.ClusterScriptFrom)2 ClusterSpec (io.stackgres.apiweb.dto.cluster.ClusterSpec)2 ClusterSpecLabels (io.stackgres.apiweb.dto.cluster.ClusterSpecLabels)2 ClusterSpecMetadata (io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata)2 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)2