use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig in project stackgres by ongres.
the class DefaultBackupFactory method buildResource.
@Override
StackGresBackupConfig buildResource(String namespace) {
StackGresBackupConfig config = new StackGresBackupConfig();
config.getMetadata().setNamespace(namespace);
config.getMetadata().setName(generateDefaultName());
StackGresBackupConfigSpec spec = buildSpec(StackGresBackupConfigSpec.class);
config.setSpec(spec);
return config;
}
use of io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig 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();
}
Aggregations