use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig in project stackgres by ongres.
the class BackupConfigValidator method checkIfBackupConfigExists.
private void checkIfBackupConfigExists(StackGresClusterReview review, String onError) throws ValidationFailed {
StackGresCluster cluster = review.getRequest().getObject();
String backupConfig = cluster.getSpec().getConfiguration().getBackupConfig();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
if (backupConfig != null) {
Optional<StackGresBackupConfig> backupConfigOpt = configFinder.findByNameAndNamespace(backupConfig, namespace);
if (!backupConfigOpt.isPresent()) {
fail(onError);
}
}
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig in project stackgres by ongres.
the class BackupCronJob method createCronJob.
private CronJob createCronJob(StackGresClusterContext context, StackGresBackupConfig backupConfig) {
String namespace = context.getSource().getMetadata().getNamespace();
String name = context.getSource().getMetadata().getName();
final StackGresCluster cluster = context.getSource();
Map<String, String> labels = labelFactory.scheduledBackupPodLabels(cluster);
return new CronJobBuilder().withNewMetadata().withNamespace(namespace).withName(backupName(context)).withLabels(labels).endMetadata().withNewSpec().withConcurrencyPolicy("Allow").withFailedJobsHistoryLimit(10).withStartingDeadlineSeconds(5 * 60L).withSchedule(Optional.of(backupConfig).map(StackGresBackupConfig::getSpec).map(StackGresBackupConfigSpec::getBaseBackups).map(StackGresBaseBackupConfig::getCronSchedule).orElse("0 5 * * *")).withJobTemplate(new JobTemplateSpecBuilder().withNewMetadata().withNamespace(namespace).withName(backupName(context)).withLabels(labels).endMetadata().withNewSpec().withBackoffLimit(3).withCompletions(1).withParallelism(1).withNewTemplate().withNewMetadata().withNamespace(namespace).withName(backupName(context)).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("CLUSTER_NAME").withValue(name).build(), new EnvVarBuilder().withName("CRONJOB_NAME").withValue(backupName(context)).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("SCHEDULED_BACKUP_KEY").withValue(StackGresContext.SCHEDULED_BACKUP_KEY).build(), new EnvVarBuilder().withName("RIGHT_VALUE").withValue(StackGresContext.RIGHT_VALUE).build(), new EnvVarBuilder().withName("PATRONI_CLUSTER_LABELS").withValue(labelFactory.patroniClusterLabels(cluster).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).map(StackGresBackupConfig::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" + (BACKUP_LOGGER.isTraceEnabled() ? "x" : ""), ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.path()).withVolumeMounts(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)), ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_SHELL_UTILS_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_SHELL_UTILS_PATH.path()).withReadOnly(true))).build()).withVolumes(new VolumeBuilder(ClusterStatefulSetVolumeConfig.TEMPLATES.volume(context)).editConfigMap().withDefaultMode(// NOPMD
0555).endConfigMap().build()).endSpec().endTemplate().endSpec().build()).endSpec().build();
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig in project stackgres by ongres.
the class BackupConfigTransformer method toCustomResource.
@Override
public StackGresBackupConfig toCustomResource(BackupConfigDto source, StackGresBackupConfig original) {
StackGresBackupConfig transformation = Optional.ofNullable(original).orElseGet(StackGresBackupConfig::new);
transformation.setMetadata(getCustomResourceMetadata(source, original));
transformation.setSpec(getCustomResourceSpec(source.getSpec()));
return transformation;
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig 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;
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig in project stackgres by ongres.
the class BackupRequiredResourcesGenerator method getRequiredResources.
@Override
public List<HasMetadata> getRequiredResources(StackGresBackup config) {
final ObjectMeta metadata = config.getMetadata();
final String dbOpsName = metadata.getName();
final String dbOpsNamespace = metadata.getNamespace();
final StackGresBackupSpec spec = config.getSpec();
final StackGresCluster cluster = clusterFinder.findByNameAndNamespace(spec.getSgCluster(), dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target a non existent SGCluster " + spec.getSgCluster()));
final StackGresBackupConfig backupConfig = Optional.of(cluster.getSpec()).map(StackGresClusterSpec::getConfiguration).map(StackGresClusterConfiguration::getBackupConfig).map(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target SGCluster " + spec.getSgCluster() + " with a non existent SGBackupConfig " + backupConfigName))).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target SGCluster " + spec.getSgCluster() + " without a SGBackupConfig"));
StackGresBackupContext context = ImmutableStackGresBackupContext.builder().source(config).cluster(cluster).backupConfig(backupConfig).build();
final List<ResourceGenerator<StackGresBackupContext>> resourceGenerators = generators.getResourceGenerators(context);
final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
List<Decorator<StackGresBackupContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
decorators.forEach(decorator -> decorator.decorate(context, resources));
return resources;
}
Aggregations