Skip to content

Commit 1312df1

Browse files
committed
Adjust function that provides the log path for cloud backup jobs and add tests.
1 parent 5eaf862 commit 1312df1

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"io"
11+
"path/filepath"
1112
"reflect"
1213
"regexp"
1314
"sort"
@@ -823,7 +824,7 @@ func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCl
823824
}
824825
} else {
825826
mkdirCommand := ""
826-
cloudLogPath := r.reconcileCloudLogPath(ctx, postgresCluster)
827+
cloudLogPath := getCloudLogPath(postgresCluster)
827828
if cloudLogPath != "" {
828829
mkdirCommand += shell.MakeDirectories(cloudLogPath, cloudLogPath) + "; "
829830
}
@@ -892,8 +893,8 @@ func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCl
892893
pgbackrest.AddConfigToCloudBackupJob(postgresCluster, &jobSpec.Template)
893894

894895
// Mount the PVC named in the "pgbackrest-cloud-log-volume" annotation, if any.
895-
if logVolumeName := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolumeName != "" {
896-
util.AddCloudLogVolumeToPod(&jobSpec.Template.Spec, logVolumeName)
896+
if logVolume := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolume != "" {
897+
util.AddCloudLogVolumeToPod(&jobSpec.Template.Spec, logVolume)
897898
}
898899
}
899900

@@ -2082,7 +2083,7 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20822083
repoHostName, configHash, serviceName, serviceNamespace string,
20832084
instanceNames []string) error {
20842085

2085-
cloudLogPath := r.reconcileCloudLogPath(ctx, postgresCluster)
2086+
cloudLogPath := getCloudLogPath(postgresCluster)
20862087

20872088
backrestConfig, err := pgbackrest.CreatePGBackRestConfigMapIntent(ctx, postgresCluster, repoHostName,
20882089
configHash, serviceName, serviceNamespace, cloudLogPath, instanceNames)
@@ -3338,39 +3339,22 @@ func authorizeBackupRemovalAnnotationPresent(postgresCluster *v1beta1.PostgresCl
33383339
return false
33393340
}
33403341

3341-
// reconcileCloudLogPath is responsible for determining the appropriate log path
3342-
// for pgbackrest in cloud backup jobs.
3343-
func (r *Reconciler) reconcileCloudLogPath(ctx context.Context,
3344-
postgresCluster *v1beta1.PostgresCluster) string {
3345-
// If the user has specified a PVC to use as a log volume for cloud backups via the
3346-
// PGBackRestCloudLogVolume annotation, check for the PVC. If we find it, set the cloud
3347-
// log path. If the user has specified a PVC, but we can't find it, create a warning event.
3348-
// If the user has not set the PGBackRestCloudLogVolume annotation, but has set a log
3349-
// path via the spec, use that.
3350-
// TODO: Make sure this is what we want (i.e. annotation to take precedence over spec)
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 {
33513351
cloudLogPath := ""
3352-
if logVolumeName := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolumeName != "" {
3353-
logVolume := &corev1.PersistentVolumeClaim{
3354-
ObjectMeta: metav1.ObjectMeta{
3355-
Name: logVolumeName,
3356-
Namespace: postgresCluster.GetNamespace(),
3357-
},
3358-
}
3359-
err := errors.WithStack(r.Client.Get(ctx,
3360-
client.ObjectKeyFromObject(logVolume), logVolume))
3361-
if err != nil {
3362-
// PVC not retrieved, create warning event
3363-
r.Recorder.Event(postgresCluster, corev1.EventTypeWarning,
3364-
"PGBackRestCloudLogVolumeNotFound", err.Error())
3365-
} else {
3366-
// We successfully found the specified PVC, so we will set the log path
3367-
cloudLogPath = "/volumes/" + logVolumeName
3368-
}
3369-
// TODO: Can we safely assume that backups are enabled?
3352+
if logVolume := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolume != "" {
3353+
cloudLogPath = "/volumes/" + logVolume
33703354
} else if postgresCluster.Spec.Backups.PGBackRest.Jobs != nil &&
33713355
postgresCluster.Spec.Backups.PGBackRest.Jobs.Log != nil &&
33723356
postgresCluster.Spec.Backups.PGBackRest.Jobs.Log.Path != "" {
3373-
cloudLogPath = postgresCluster.Spec.Backups.PGBackRest.Jobs.Log.Path
3357+
cloudLogPath = filepath.Clean(postgresCluster.Spec.Backups.PGBackRest.Jobs.Log.Path)
33743358
}
33753359
return cloudLogPath
33763360
}

internal/controller/postgrescluster/pgbackrest_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4559,3 +4559,51 @@ func TestGetRepoHostVolumeRequests(t *testing.T) {
45594559
})
45604560
}
45614561
}
4562+
4563+
func TestGetCloudLogPath(t *testing.T) {
4564+
t.Run("NoAnnotationNoSpecPath", func(t *testing.T) {
4565+
postgrescluster := &v1beta1.PostgresCluster{}
4566+
assert.Equal(t, getCloudLogPath(postgrescluster), "")
4567+
})
4568+
4569+
t.Run("AnnotationSetNoSpecPath", func(t *testing.T) {
4570+
postgrescluster := &v1beta1.PostgresCluster{}
4571+
postgrescluster.Annotations = map[string]string{}
4572+
postgrescluster.Annotations[naming.PGBackRestCloudLogVolume] = "another-pvc"
4573+
assert.Equal(t, getCloudLogPath(postgrescluster), "/volumes/another-pvc")
4574+
})
4575+
4576+
t.Run("NoAnnotationSpecPathSet", func(t *testing.T) {
4577+
postgrescluster := &v1beta1.PostgresCluster{
4578+
Spec: v1beta1.PostgresClusterSpec{
4579+
Backups: v1beta1.Backups{
4580+
PGBackRest: v1beta1.PGBackRestArchive{
4581+
Jobs: &v1beta1.BackupJobs{
4582+
Log: &v1beta1.LoggingConfiguration{
4583+
Path: "/volumes/test/log/",
4584+
},
4585+
},
4586+
},
4587+
},
4588+
},
4589+
}
4590+
assert.Equal(t, getCloudLogPath(postgrescluster), "/volumes/test/log")
4591+
})
4592+
4593+
t.Run("BothAnnotationAndSpecPathSet", func(t *testing.T) {
4594+
postgrescluster := &v1beta1.PostgresCluster{
4595+
Spec: v1beta1.PostgresClusterSpec{
4596+
Backups: v1beta1.Backups{
4597+
PGBackRest: v1beta1.PGBackRestArchive{
4598+
Log: &v1beta1.LoggingConfiguration{
4599+
Path: "/volumes/test/log",
4600+
},
4601+
},
4602+
},
4603+
},
4604+
}
4605+
postgrescluster.Annotations = map[string]string{}
4606+
postgrescluster.Annotations[naming.PGBackRestCloudLogVolume] = "another-pvc"
4607+
assert.Equal(t, getCloudLogPath(postgrescluster), "/volumes/another-pvc")
4608+
})
4609+
}

0 commit comments

Comments
 (0)