|
8 | 8 | "context"
|
9 | 9 | "fmt"
|
10 | 10 | "io"
|
| 11 | + "path/filepath" |
11 | 12 | "reflect"
|
12 | 13 | "regexp"
|
13 | 14 | "sort"
|
@@ -38,6 +39,7 @@ import (
|
38 | 39 | "github.com/crunchydata/postgres-operator/internal/pgbackrest"
|
39 | 40 | "github.com/crunchydata/postgres-operator/internal/pki"
|
40 | 41 | "github.com/crunchydata/postgres-operator/internal/postgres"
|
| 42 | + "github.com/crunchydata/postgres-operator/internal/shell" |
41 | 43 | "github.com/crunchydata/postgres-operator/internal/util"
|
42 | 44 | "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
|
43 | 45 | )
|
@@ -821,7 +823,13 @@ func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCl
|
821 | 823 | {Name: "SELECTOR", Value: naming.PGBackRestDedicatedSelector(postgresCluster.GetName()).String()},
|
822 | 824 | }
|
823 | 825 | } else {
|
824 |
| - container.Command = []string{"/bin/pgbackrest", "backup"} |
| 826 | + mkdirCommand := "" |
| 827 | + cloudLogPath := getCloudLogPath(postgresCluster) |
| 828 | + if cloudLogPath != "" { |
| 829 | + mkdirCommand += shell.MakeDirectories(cloudLogPath, cloudLogPath) + "; " |
| 830 | + } |
| 831 | + |
| 832 | + container.Command = []string{"sh", "-c", "--", mkdirCommand + `exec "$@"`, "--", "/bin/pgbackrest", "backup"} |
825 | 833 | container.Command = append(container.Command, cmdOpts...)
|
826 | 834 | }
|
827 | 835 |
|
@@ -885,8 +893,8 @@ func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCl
|
885 | 893 | pgbackrest.AddConfigToCloudBackupJob(postgresCluster, &jobSpec.Template)
|
886 | 894 |
|
887 | 895 | // Mount the PVC named in the "pgbackrest-cloud-log-volume" annotation, if any.
|
888 |
| - if logVolumeName := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolumeName != "" { |
889 |
| - util.AddCloudLogVolumeToPod(&jobSpec.Template.Spec, logVolumeName) |
| 896 | + if logVolume := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolume != "" { |
| 897 | + util.AddCloudLogVolumeToPod(&jobSpec.Template.Spec, logVolume) |
890 | 898 | }
|
891 | 899 | }
|
892 | 900 |
|
@@ -2075,28 +2083,7 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
|
2075 | 2083 | repoHostName, configHash, serviceName, serviceNamespace string,
|
2076 | 2084 | instanceNames []string) error {
|
2077 | 2085 |
|
2078 |
| - // If the user has specified a PVC to use as a log volume for cloud backups via the |
2079 |
| - // PGBackRestCloudLogVolume annotation, check for the PVC. If we find it, set the cloud |
2080 |
| - // log path. If the user has specified a PVC, but we can't find it, create a warning event. |
2081 |
| - cloudLogPath := "" |
2082 |
| - if logVolumeName := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolumeName != "" { |
2083 |
| - logVolume := &corev1.PersistentVolumeClaim{ |
2084 |
| - ObjectMeta: metav1.ObjectMeta{ |
2085 |
| - Name: logVolumeName, |
2086 |
| - Namespace: postgresCluster.GetNamespace(), |
2087 |
| - }, |
2088 |
| - } |
2089 |
| - err := errors.WithStack(r.Client.Get(ctx, |
2090 |
| - client.ObjectKeyFromObject(logVolume), logVolume)) |
2091 |
| - if err != nil { |
2092 |
| - // PVC not retrieved, create warning event |
2093 |
| - r.Recorder.Event(postgresCluster, corev1.EventTypeWarning, |
2094 |
| - "PGBackRestCloudLogVolumeNotFound", err.Error()) |
2095 |
| - } else { |
2096 |
| - // We successfully found the specified PVC, so we will set the log path |
2097 |
| - cloudLogPath = "/volumes/" + logVolumeName |
2098 |
| - } |
2099 |
| - } |
| 2086 | + cloudLogPath := getCloudLogPath(postgresCluster) |
2100 | 2087 |
|
2101 | 2088 | backrestConfig, err := pgbackrest.CreatePGBackRestConfigMapIntent(ctx, postgresCluster, repoHostName,
|
2102 | 2089 | configHash, serviceName, serviceNamespace, cloudLogPath, instanceNames)
|
@@ -3351,3 +3338,23 @@ func authorizeBackupRemovalAnnotationPresent(postgresCluster *v1beta1.PostgresCl
|
3351 | 3338 | }
|
3352 | 3339 | return false
|
3353 | 3340 | }
|
| 3341 | + |
| 3342 | +// getCloudLogPath is responsible for determining the appropriate log path for pgbackrest |
| 3343 | +// in cloud backup jobs. If the user has specified a PVC to use as a log volume for cloud |
| 3344 | +// backups via the PGBackRestCloudLogVolume annotation, set the cloud log path accordingly. |
| 3345 | +// If the user has not set the PGBackRestCloudLogVolume annotation, but has set a log path |
| 3346 | +// via the spec, use that. |
| 3347 | +// TODO: Make sure this is what we want (i.e. annotation to take precedence over spec) |
| 3348 | +// |
| 3349 | +// This function assumes that the backups/pgbackrest spec is present in postgresCluster. |
| 3350 | +func getCloudLogPath(postgresCluster *v1beta1.PostgresCluster) string { |
| 3351 | + cloudLogPath := "" |
| 3352 | + if logVolume := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolume != "" { |
| 3353 | + cloudLogPath = "/volumes/" + logVolume |
| 3354 | + } else if postgresCluster.Spec.Backups.PGBackRest.Jobs != nil && |
| 3355 | + postgresCluster.Spec.Backups.PGBackRest.Jobs.Log != nil && |
| 3356 | + postgresCluster.Spec.Backups.PGBackRest.Jobs.Log.Path != "" { |
| 3357 | + cloudLogPath = filepath.Clean(postgresCluster.Spec.Backups.PGBackRest.Jobs.Log.Path) |
| 3358 | + } |
| 3359 | + return cloudLogPath |
| 3360 | +} |
0 commit comments