Search in sources :

Example 6 with StackGresBackup

use of io.stackgres.common.crd.sgbackup.StackGresBackup in project stackgres by ongres.

the class BackupDeployedResourceScanner method getDeployedResources.

@Override
public List<HasMetadata> getDeployedResources(StackGresBackup config) {
    try (KubernetesClient client = clientFactory.create()) {
        final Map<String, String> genericLabels = labelFactory.genericLabels(config);
        Stream<HasMetadata> inNamespace = IN_NAMESPACE_RESOURCE_OPERATIONS.values().stream().flatMap(resourceOperationGetter -> resourceOperationGetter.apply(client).inNamespace(config.getMetadata().getNamespace()).withLabels(genericLabels).list().getItems().stream());
        List<HasMetadata> deployedResources = inNamespace.filter(resource -> resource.getMetadata().getOwnerReferences().stream().anyMatch(ownerReference -> ownerReference.getKind().equals(StackGresBackup.KIND) && ownerReference.getName().equals(config.getMetadata().getName()) && ownerReference.getUid().equals(config.getMetadata().getUid()))).collect(Collectors.toUnmodifiableList());
        deployedResources.forEach(resource -> {
            Instance<DeployedResourceDecorator> decorator = decorators.select(new ReconciliationScopeLiteral(StackGresBackup.class, resource.getKind()));
            if (decorator.isResolvable()) {
                decorator.get().decorate(resource);
            }
        });
        return deployedResources;
    }
}
Also used : ReconciliationScopeLiteral(io.stackgres.operator.conciliation.ReconciliationScopeLiteral) KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) LabelFactoryForBackup(io.stackgres.common.LabelFactoryForBackup) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) ReconciliationOperations(io.stackgres.operator.conciliation.ReconciliationOperations) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Inject(javax.inject.Inject) DeployedResourcesScanner(io.stackgres.operator.conciliation.DeployedResourcesScanner) List(java.util.List) Resource(io.fabric8.kubernetes.client.dsl.Resource) Stream(java.util.stream.Stream) DeployedResourceDecorator(io.stackgres.operator.conciliation.DeployedResourceDecorator) Map(java.util.Map) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) KubernetesClientFactory(io.stackgres.common.KubernetesClientFactory) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Any(javax.enterprise.inject.Any) Job(io.fabric8.kubernetes.api.model.batch.v1.Job) Instance(javax.enterprise.inject.Instance) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReconciliationScopeLiteral(io.stackgres.operator.conciliation.ReconciliationScopeLiteral) DeployedResourceDecorator(io.stackgres.operator.conciliation.DeployedResourceDecorator)

Example 7 with StackGresBackup

use of io.stackgres.common.crd.sgbackup.StackGresBackup 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 8 with StackGresBackup

use of io.stackgres.common.crd.sgbackup.StackGresBackup in project stackgres by ongres.

the class BackupJob method createBackupJob.

private HasMetadata createBackupJob(StackGresBackupContext context) {
    StackGresBackup backup = context.getSource();
    StackGresBackupConfig backupConfig = context.getBackupConfig();
    String namespace = backup.getMetadata().getNamespace();
    String name = backup.getMetadata().getName();
    String cluster = backup.getSpec().getSgCluster();
    Map<String, String> labels = labelFactory.backupPodLabels(context.getSource());
    final VolumeMount utilsVolumeMount = ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_SHELL_UTILS_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_SHELL_UTILS_PATH.path()).withReadOnly(true));
    final VolumeMount backupVolumeMount = ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.path()).withReadOnly(true));
    return new JobBuilder().withNewMetadata().withNamespace(namespace).withName(backupJobName(backup)).withLabels(labels).endMetadata().withNewSpec().withBackoffLimit(3).withCompletions(1).withParallelism(1).withNewTemplate().withNewMetadata().withNamespace(namespace).withName(backupJobName(backup)).withLabels(labels).endMetadata().withNewSpec().withSecurityContext(podSecurityFactory.createResource(context)).withRestartPolicy("OnFailure").withServiceAccountName(PatroniRoleGenerator.roleName(context)).withContainers(new ContainerBuilder().withName("create-backup").withImage(StackGresComponent.KUBECTL.findLatestImageName()).withImagePullPolicy("IfNotPresent").withEnv(ImmutableList.<EnvVar>builder().addAll(getClusterEnvVars(context)).add(new EnvVarBuilder().withName("CLUSTER_NAMESPACE").withValue(namespace).build(), new EnvVarBuilder().withName("BACKUP_NAME").withValue(name).build(), new EnvVarBuilder().withName("CLUSTER_NAME").withValue(cluster).build(), new EnvVarBuilder().withName("CRONJOB_NAME").withValue(cluster + StackGresUtil.BACKUP_SUFFIX).build(), new EnvVarBuilder().withName("BACKUP_IS_PERMANENT").withValue(Optional.ofNullable(backup.getSpec().getManagedLifecycle()).map(managedLifecycle -> !managedLifecycle).map(String::valueOf).orElse("true")).build(), new EnvVarBuilder().withName("BACKUP_CONFIG_CRD_NAME").withValue(CustomResource.getCRDName(StackGresBackupConfig.class)).build(), new EnvVarBuilder().withName("BACKUP_CONFIG").withValue(backupConfig.getMetadata().getName()).build(), new EnvVarBuilder().withName("BACKUP_CRD_KIND").withValue(HasMetadata.getKind(StackGresBackup.class)).build(), new EnvVarBuilder().withName("BACKUP_CRD_NAME").withValue(CustomResource.getCRDName(StackGresBackup.class)).build(), new EnvVarBuilder().withName("BACKUP_CRD_APIVERSION").withValue(HasMetadata.getApiVersion(StackGresBackup.class)).build(), new EnvVarBuilder().withName("BACKUP_PHASE_RUNNING").withValue(BackupPhase.RUNNING.label()).build(), new EnvVarBuilder().withName("BACKUP_PHASE_COMPLETED").withValue(BackupPhase.COMPLETED.label()).build(), new EnvVarBuilder().withName("BACKUP_PHASE_FAILED").withValue(BackupPhase.FAILED.label()).build(), new EnvVarBuilder().withName("PATRONI_ROLE_KEY").withValue(StackGresContext.ROLE_KEY).build(), new EnvVarBuilder().withName("PATRONI_PRIMARY_ROLE").withValue(StackGresContext.PRIMARY_ROLE).build(), new EnvVarBuilder().withName("PATRONI_REPLICA_ROLE").withValue(StackGresContext.REPLICA_ROLE).build(), new EnvVarBuilder().withName("PATRONI_CLUSTER_LABELS").withValue(labelFactoryForCluster.patroniClusterLabels(context.getCluster()).entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","))).build(), new EnvVarBuilder().withName("PATRONI_CONTAINER_NAME").withValue(StackgresClusterContainers.PATRONI).build(), new EnvVarBuilder().withName("SERVICE_ACCOUNT").withValueFrom(new EnvVarSourceBuilder().withFieldRef(new ObjectFieldSelectorBuilder().withFieldPath("spec.serviceAccountName").build()).build()).build(), new EnvVarBuilder().withName("POD_NAME").withValueFrom(new EnvVarSourceBuilder().withFieldRef(new ObjectFieldSelectorBuilder().withFieldPath("metadata.name").build()).build()).build(), new EnvVarBuilder().withName("RETAIN").withValue(Optional.of(backupConfig.getSpec()).map(StackGresBackupConfigSpec::getBaseBackups).map(StackGresBaseBackupConfig::getRetention).map(String::valueOf).orElse("5")).build(), new EnvVarBuilder().withName("WINDOW").withValue("3600").build()).build()).withCommand("/bin/bash", "-e" + (LOGGER.isTraceEnabled() ? "x" : ""), ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.path()).withVolumeMounts(backupVolumeMount, utilsVolumeMount).build()).withVolumes(new VolumeBuilder(ClusterStatefulSetVolumeConfig.TEMPLATES.volume(context)).editConfigMap().withDefaultMode(// NOPMD
    0555).endConfigMap().build()).endSpec().endTemplate().endSpec().build();
}
Also used : VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) StackGresContext(io.stackgres.common.StackGresContext) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) LoggerFactory(org.slf4j.LoggerFactory) StackgresClusterContainers(io.stackgres.common.StackgresClusterContainers) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) StackGresBaseBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBaseBackupConfig) ClusterContext(io.stackgres.common.ClusterContext) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) PatroniRoleGenerator(io.stackgres.operator.conciliation.factory.cluster.patroni.PatroniRoleGenerator) StackGresUtil(io.stackgres.common.StackGresUtil) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) ResourceUtil(io.stackgres.operatorframework.resource.ResourceUtil) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) BackupPhase(io.stackgres.common.crd.sgbackup.BackupPhase) List(java.util.List) Stream(java.util.stream.Stream) Tuple(org.jooq.lambda.tuple.Tuple) ClusterEnvironmentVariablesFactoryDiscoverer(io.stackgres.operator.conciliation.factory.cluster.patroni.ClusterEnvironmentVariablesFactoryDiscoverer) Optional(java.util.Optional) ClusterEnvironmentVariablesFactory(io.stackgres.operator.conciliation.factory.cluster.patroni.ClusterEnvironmentVariablesFactory) CustomResource(io.fabric8.kubernetes.client.CustomResource) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) LabelFactoryForBackup(io.stackgres.common.LabelFactoryForBackup) StackGresBackupConfigSpec(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Seq(org.jooq.lambda.Seq) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ClusterStatefulSetPath(io.stackgres.common.ClusterStatefulSetPath) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) ObjectFieldSelectorBuilder(io.fabric8.kubernetes.api.model.ObjectFieldSelectorBuilder) StackGresBackupStatus(io.stackgres.common.crd.sgbackup.StackGresBackupStatus) ResourceFactory(io.stackgres.operator.conciliation.factory.ResourceFactory) StackGresBackupContext(io.stackgres.operator.conciliation.backup.StackGresBackupContext) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder) Logger(org.slf4j.Logger) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) StackGresComponent(io.stackgres.common.StackGresComponent) ClusterStatefulSetVolumeConfig(io.stackgres.operator.conciliation.factory.cluster.patroni.ClusterStatefulSetVolumeConfig) StackGresBackupProcess(io.stackgres.common.crd.sgbackup.StackGresBackupProcess) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder) ObjectFieldSelectorBuilder(io.fabric8.kubernetes.api.model.ObjectFieldSelectorBuilder) StackGresBaseBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBaseBackupConfig) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount)

Aggregations

StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)4 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)3 ResourceGenerator (io.stackgres.operator.conciliation.ResourceGenerator)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 Inject (javax.inject.Inject)3 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 LabelFactoryForBackup (io.stackgres.common.LabelFactoryForBackup)2 BackupPhase (io.stackgres.common.crd.sgbackup.BackupPhase)2 StackGresBackupProcess (io.stackgres.common.crd.sgbackup.StackGresBackupProcess)2 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)2 StackGresClusterRestore (io.stackgres.common.crd.sgcluster.StackGresClusterRestore)2 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)2 CustomResourceFinder (io.stackgres.common.resource.CustomResourceFinder)2 RequiredResourceGenerator (io.stackgres.operator.conciliation.RequiredResourceGenerator)2 ResourceGenerationDiscoverer (io.stackgres.operator.conciliation.ResourceGenerationDiscoverer)2 Decorator (io.stackgres.operator.conciliation.factory.Decorator)2