use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec in project stackgres by ongres.
the class RestoreSecret method buildSource.
@NotNull
public Optional<HasMetadata> buildSource(StackGresClusterContext context) {
return context.getRestoreBackup().map(restoreBackup -> {
Map<String, String> data = new HashMap<>();
data.put("BACKUP_RESOURCE_VERSION", restoreBackup.getMetadata().getResourceVersion());
String backupNamespace = restoreBackup.getMetadata().getNamespace();
final StackGresBackupConfigSpec backupConfig = restoreBackup.getStatus().getBackupConfig();
data.putAll(envVarFactory.getSecretEnvVar(backupNamespace, backupConfig));
final StackGresCluster cluster = context.getSource();
return new SecretBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context)).withLabels(labelFactory.clusterLabels(cluster)).endMetadata().withType("Opaque").withStringData(StackGresUtil.addMd5Sum(data)).build();
});
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec 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.StackGresBackupConfigSpec in project stackgres by ongres.
the class BackupEnvVarFactoryImpl method getSecretEnvVar.
@Override
public Map<String, String> getSecretEnvVar(StackGresBackupConfig backupConfig) {
String namespace = backupConfig.getMetadata().getNamespace();
final StackGresBackupConfigSpec spec = backupConfig.getSpec();
var secrets = getBackupConfigSecretReferences(namespace, spec);
return getBackupSecrets(spec, secrets);
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec in project stackgres by ongres.
the class BackupConfigDefaultValuesMutatorTest method getEmptyReview.
@Override
protected BackupConfigReview getEmptyReview() {
BackupConfigReview backupConfigReview = JsonUtil.readFromJson("backupconfig_allow_request/create.json", BackupConfigReview.class);
backupConfigReview.getRequest().getObject().setSpec(new StackGresBackupConfigSpec());
return backupConfigReview;
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec in project stackgres by ongres.
the class BackupConfigTransformer method getResourceSpec.
public BackupConfigSpec getResourceSpec(StackGresBackupConfigSpec source) {
if (source == null) {
return null;
}
BackupConfigSpec transformation = new BackupConfigSpec();
Optional.ofNullable(source.getBaseBackups()).ifPresent(sourceBaseBackup -> {
final BaseBackupConfig baseBackup = new BaseBackupConfig();
baseBackup.setCompressionMethod(sourceBaseBackup.getCompression());
baseBackup.setCronSchedule(sourceBaseBackup.getCronSchedule());
baseBackup.setRetention(sourceBaseBackup.getRetention());
transformation.setBaseBackup(baseBackup);
});
Optional.ofNullable(source.getBaseBackups()).map(StackGresBaseBackupConfig::getPerformance).ifPresent(sourcePerformance -> {
final BaseBackupPerformance performance = new BaseBackupPerformance();
performance.setMaxDiskBandwitdh(sourcePerformance.getMaxDiskBandwitdh());
performance.setMaxNetworkBandwitdh(sourcePerformance.getMaxNetworkBandwitdh());
performance.setUploadDiskConcurrency(sourcePerformance.getUploadDiskConcurrency());
transformation.getBaseBackups().setPerformance(performance);
});
transformation.setStorage(getResourceStorage(source.getStorage()));
return transformation;
}
Aggregations