Skip to content

Commit ac3d4ad

Browse files
committed
Allow user to set an annotation that will specify an existing PVC to be...
1 parent 2564681 commit ac3d4ad

File tree

8 files changed

+419
-34
lines changed

8 files changed

+419
-34
lines changed

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/crunchydata/postgres-operator/internal/pgbackrest"
4141
"github.com/crunchydata/postgres-operator/internal/pki"
4242
"github.com/crunchydata/postgres-operator/internal/postgres"
43+
"github.com/crunchydata/postgres-operator/internal/util"
4344
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4445
)
4546

@@ -772,7 +773,7 @@ func (r *Reconciler) generateRepoVolumeIntent(postgresCluster *v1beta1.PostgresC
772773
}
773774

774775
// generateBackupJobSpecIntent generates a JobSpec for a pgBackRest backup job
775-
func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.PostgresCluster,
776+
func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.PostgresCluster,
776777
repo v1beta1.PGBackRestRepo, serviceAccountName string,
777778
labels, annotations map[string]string, opts ...string) *batchv1.JobSpec {
778779

@@ -866,6 +867,27 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
866867
// to read certificate files
867868
jobSpec.Template.Spec.SecurityContext = postgres.PodSecurityContext(postgresCluster)
868869
pgbackrest.AddConfigToCloudBackupJob(postgresCluster, &jobSpec.Template)
870+
871+
// If the user has specified a PVC to use as a log volume via the PGBackRestCloudLogVolume
872+
// annotation, check for the PVC. If we find it, mount it to the backup job.
873+
// Otherwise, create a warning event.
874+
if logVolumeName := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolumeName != "" {
875+
logVolume := &corev1.PersistentVolumeClaim{
876+
ObjectMeta: metav1.ObjectMeta{
877+
Name: logVolumeName,
878+
Namespace: postgresCluster.GetNamespace(),
879+
},
880+
}
881+
err := errors.WithStack(r.Client.Get(ctx,
882+
client.ObjectKeyFromObject(logVolume), logVolume))
883+
if err != nil {
884+
// PVC not retrieved, create warning event
885+
r.Recorder.Event(postgresCluster, corev1.EventTypeWarning, "PGBackRestCloudLogVolumeNotFound", err.Error())
886+
} else {
887+
// We successfully found the specified PVC, so we will add it to the backup job
888+
util.AddVolumeAndMountsToPod(&jobSpec.Template.Spec, logVolume)
889+
}
890+
}
869891
}
870892

871893
return jobSpec
@@ -2030,10 +2052,33 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20302052
repoHostName, configHash, serviceName, serviceNamespace string,
20312053
instanceNames []string) error {
20322054

2055+
// If the user has specified a PVC to use as a log volume for cloud backups via the
2056+
// PGBackRestCloudLogVolume annotation, check for the PVC. If we find it, set the cloud
2057+
// log path. If the user has specified a PVC, but we can't find it, create a warning event.
2058+
cloudLogPath := ""
2059+
if logVolumeName := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolumeName != "" {
2060+
logVolume := &corev1.PersistentVolumeClaim{
2061+
ObjectMeta: metav1.ObjectMeta{
2062+
Name: logVolumeName,
2063+
Namespace: postgresCluster.GetNamespace(),
2064+
},
2065+
}
2066+
err := errors.WithStack(r.Client.Get(ctx,
2067+
client.ObjectKeyFromObject(logVolume), logVolume))
2068+
if err != nil {
2069+
// PVC not retrieved, create warning event
2070+
r.Recorder.Event(postgresCluster, corev1.EventTypeWarning,
2071+
"PGBackRestCloudLogVolumeNotFound", err.Error())
2072+
} else {
2073+
// We successfully found the specified PVC, so we will set the log path
2074+
cloudLogPath = "/volumes/" + logVolumeName
2075+
}
2076+
}
2077+
20332078
backrestConfig := pgbackrest.CreatePGBackRestConfigMapIntent(postgresCluster, repoHostName,
2034-
configHash, serviceName, serviceNamespace, instanceNames)
2035-
if err := controllerutil.SetControllerReference(postgresCluster, backrestConfig,
2036-
r.Client.Scheme()); err != nil {
2079+
configHash, serviceName, serviceNamespace, cloudLogPath, instanceNames)
2080+
2081+
if err := r.setControllerReference(postgresCluster, backrestConfig); err != nil {
20372082
return err
20382083
}
20392084
if err := r.apply(ctx, backrestConfig); err != nil {
@@ -2441,7 +2486,7 @@ func (r *Reconciler) reconcileManualBackup(ctx context.Context,
24412486
backupJob.ObjectMeta.Labels = labels
24422487
backupJob.ObjectMeta.Annotations = annotations
24432488

2444-
spec := generateBackupJobSpecIntent(ctx, postgresCluster, repo,
2489+
spec := r.generateBackupJobSpecIntent(ctx, postgresCluster, repo,
24452490
serviceAccount.GetName(), labels, annotations, backupOpts...)
24462491

24472492
backupJob.Spec = *spec
@@ -2619,7 +2664,7 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
26192664
backupJob.ObjectMeta.Labels = labels
26202665
backupJob.ObjectMeta.Annotations = annotations
26212666

2622-
spec := generateBackupJobSpecIntent(ctx, postgresCluster, replicaCreateRepo,
2667+
spec := r.generateBackupJobSpecIntent(ctx, postgresCluster, replicaCreateRepo,
26232668
serviceAccount.GetName(), labels, annotations)
26242669

26252670
backupJob.Spec = *spec
@@ -3046,7 +3091,7 @@ func (r *Reconciler) reconcilePGBackRestCronJob(
30463091
// set backup type (i.e. "full", "diff", "incr")
30473092
backupOpts := []string{"--type=" + backupType}
30483093

3049-
jobSpec := generateBackupJobSpecIntent(ctx, cluster, repo,
3094+
jobSpec := r.generateBackupJobSpecIntent(ctx, cluster, repo,
30503095
serviceAccount.GetName(), labels, annotations, backupOpts...)
30513096

30523097
// Suspend cronjobs when shutdown or read-only. Any jobs that have already

0 commit comments

Comments
 (0)