Skip to content

Commit acabe60

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

File tree

2 files changed

+63
-32
lines changed

2 files changed

+63
-32
lines changed

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCl
823823
}
824824
} else {
825825
mkdirCommand := ""
826-
cloudLogPath := r.reconcileCloudLogPath(ctx, postgresCluster)
826+
cloudLogPath := getCloudLogPath(postgresCluster)
827827
if cloudLogPath != "" {
828828
mkdirCommand += shell.MakeDirectories(cloudLogPath, cloudLogPath) + "; "
829829
}
@@ -892,8 +892,8 @@ func (r *Reconciler) generateBackupJobSpecIntent(ctx context.Context, postgresCl
892892
pgbackrest.AddConfigToCloudBackupJob(postgresCluster, &jobSpec.Template)
893893

894894
// 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)
895+
if logVolume := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolume != "" {
896+
util.AddCloudLogVolumeToPod(&jobSpec.Template.Spec, logVolume)
897897
}
898898
}
899899

@@ -2082,7 +2082,7 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20822082
repoHostName, configHash, serviceName, serviceNamespace string,
20832083
instanceNames []string) error {
20842084

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

20872087
backrestConfig, err := pgbackrest.CreatePGBackRestConfigMapIntent(ctx, postgresCluster, repoHostName,
20882088
configHash, serviceName, serviceNamespace, cloudLogPath, instanceNames)
@@ -3338,35 +3338,18 @@ func authorizeBackupRemovalAnnotationPresent(postgresCluster *v1beta1.PostgresCl
33383338
return false
33393339
}
33403340

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)
3341+
// getCloudLogPath is responsible for determining the appropriate log path for pgbackrest
3342+
// in cloud backup jobs. If the user has specified a PVC to use as a log volume for cloud
3343+
// backups via the PGBackRestCloudLogVolume annotation, set the cloud log path accordingly.
3344+
// If the user has not set the PGBackRestCloudLogVolume annotation, but has set a log path
3345+
// via the spec, use that.
3346+
// TODO: Make sure this is what we want (i.e. annotation to take precedence over spec)
3347+
//
3348+
// This function assumes that the backups/pgbackrest spec is present in postgresCluster.
3349+
func getCloudLogPath(postgresCluster *v1beta1.PostgresCluster) string {
33513350
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?
3351+
if logVolume := postgresCluster.Annotations[naming.PGBackRestCloudLogVolume]; logVolume != "" {
3352+
cloudLogPath = "/volumes/" + logVolume
33703353
} else if postgresCluster.Spec.Backups.PGBackRest.Jobs != nil &&
33713354
postgresCluster.Spec.Backups.PGBackRest.Jobs.Log != nil &&
33723355
postgresCluster.Spec.Backups.PGBackRest.Jobs.Log.Path != "" {

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)