Skip to content

Commit bf21f0a

Browse files
committed
Another small refactor for sending pgbackrest logs in Repo Host to additional volume.
1 parent 9b65fca commit bf21f0a

File tree

2 files changed

+112
-30
lines changed

2 files changed

+112
-30
lines changed

internal/pgbackrest/config.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,7 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet
134134

135135
if collector.OpenTelemetryLogsOrMetricsEnabled(ctx, postgresCluster) {
136136
// Get pgbackrest log path for repo host pod
137-
var pgBackRestLogPath string
138-
for _, repo := range postgresCluster.Spec.Backups.PGBackRest.Repos {
139-
if repo.Volume != nil {
140-
// If the user has set a log path in the spec, use it.
141-
// Otherwise, default to /pgbackrest/repo#/log
142-
if postgresCluster.Spec.Backups.PGBackRest.RepoHost != nil &&
143-
postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log != nil &&
144-
postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log.Path != "" {
145-
pgBackRestLogPath = postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log.Path
146-
} else {
147-
pgBackRestLogPath = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
148-
}
149-
break
150-
}
151-
}
137+
pgBackRestLogPath := generateRepoHostLogPath(postgresCluster)
152138

153139
err = collector.AddToConfigMap(ctx, collector.NewConfigForPgBackrestRepoHostPod(
154140
ctx,
@@ -190,21 +176,7 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet
190176
func MakePGBackrestLogDir(template *corev1.PodTemplateSpec,
191177
cluster *v1beta1.PostgresCluster) string {
192178

193-
var pgBackRestLogPath string
194-
for _, repo := range cluster.Spec.Backups.PGBackRest.Repos {
195-
if repo.Volume != nil {
196-
// If the user has set a log path in the spec, use it.
197-
// Otherwise, default to /pgbackrest/repo#/log
198-
if cluster.Spec.Backups.PGBackRest.RepoHost != nil &&
199-
cluster.Spec.Backups.PGBackRest.RepoHost.Log != nil &&
200-
cluster.Spec.Backups.PGBackRest.RepoHost.Log.Path != "" {
201-
pgBackRestLogPath = cluster.Spec.Backups.PGBackRest.RepoHost.Log.Path
202-
} else {
203-
pgBackRestLogPath = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
204-
}
205-
break
206-
}
207-
}
179+
pgBackRestLogPath := generateRepoHostLogPath(cluster)
208180

209181
container := corev1.Container{
210182
// TODO(log-rotation): The second argument here should be the path
@@ -795,3 +767,27 @@ func serverConfig(cluster *v1beta1.PostgresCluster) iniSectionSet {
795767
"global:server": server,
796768
}
797769
}
770+
771+
// generateRepoHostLogPath takes a postgrescluster and returns the log path that
772+
// should be used by pgbackrest in the Repo Host Pod based on the repos specified
773+
// and whether the user has specified a log path.
774+
//
775+
// This function assumes that the backups/pgbackrest spec is present in cluster.
776+
func generateRepoHostLogPath(cluster *v1beta1.PostgresCluster) string {
777+
var pgBackRestLogPath string
778+
for _, repo := range cluster.Spec.Backups.PGBackRest.Repos {
779+
if repo.Volume != nil {
780+
// If the user has set a log path in the spec, use it.
781+
// Otherwise, default to /pgbackrest/repo#/log
782+
if cluster.Spec.Backups.PGBackRest.RepoHost != nil &&
783+
cluster.Spec.Backups.PGBackRest.RepoHost.Log != nil &&
784+
cluster.Spec.Backups.PGBackRest.RepoHost.Log.Path != "" {
785+
pgBackRestLogPath = cluster.Spec.Backups.PGBackRest.RepoHost.Log.Path
786+
} else {
787+
pgBackRestLogPath = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
788+
}
789+
break
790+
}
791+
}
792+
return pgBackRestLogPath
793+
}

internal/pgbackrest/config_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,3 +840,89 @@ log-level-stderr = error
840840
log-timestamp = n
841841
`)
842842
}
843+
844+
func TestGenerateRepoHostLogPath(t *testing.T) {
845+
cluster := v1beta1.PostgresCluster{}
846+
cluster.Namespace = "ns1"
847+
cluster.Name = "hippo-dance"
848+
849+
cluster.Spec.Port = initialize.Int32(2345)
850+
cluster.Spec.PostgresVersion = 12
851+
852+
cluster.Spec.Backups = v1beta1.Backups{
853+
PGBackRest: v1beta1.PGBackRestArchive{},
854+
}
855+
856+
t.Run("NoReposNoRepoHost", func(t *testing.T) {
857+
cluster := cluster.DeepCopy()
858+
assert.Equal(t, generateRepoHostLogPath(cluster), "")
859+
})
860+
861+
t.Run("NoVolumeRepo", func(t *testing.T) {
862+
cluster := cluster.DeepCopy()
863+
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
864+
{
865+
Name: "repo1",
866+
GCS: &v1beta1.RepoGCS{},
867+
},
868+
}
869+
assert.Equal(t, generateRepoHostLogPath(cluster), "")
870+
})
871+
872+
t.Run("OneVolumeRepo", func(t *testing.T) {
873+
cluster := cluster.DeepCopy()
874+
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
875+
{
876+
Name: "repo1",
877+
Volume: &v1beta1.RepoPVC{},
878+
},
879+
}
880+
assert.Equal(t, generateRepoHostLogPath(cluster), "/pgbackrest/repo1/log")
881+
})
882+
883+
t.Run("TwoVolumeRepos", func(t *testing.T) {
884+
cluster := cluster.DeepCopy()
885+
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
886+
{
887+
Name: "repo1",
888+
Volume: &v1beta1.RepoPVC{},
889+
},
890+
{
891+
Name: "repo2",
892+
Volume: &v1beta1.RepoPVC{},
893+
},
894+
}
895+
assert.Equal(t, generateRepoHostLogPath(cluster), "/pgbackrest/repo1/log")
896+
})
897+
898+
t.Run("VolumeRepoNotFirst", func(t *testing.T) {
899+
cluster := cluster.DeepCopy()
900+
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
901+
{
902+
Name: "repo1",
903+
GCS: &v1beta1.RepoGCS{},
904+
},
905+
{
906+
Name: "repo2",
907+
Volume: &v1beta1.RepoPVC{},
908+
},
909+
}
910+
assert.Equal(t, generateRepoHostLogPath(cluster), "/pgbackrest/repo2/log")
911+
})
912+
913+
t.Run("LogPathSpecified", func(t *testing.T) {
914+
cluster := cluster.DeepCopy()
915+
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
916+
{
917+
Name: "repo1",
918+
Volume: &v1beta1.RepoPVC{},
919+
},
920+
}
921+
cluster.Spec.Backups.PGBackRest.RepoHost = &v1beta1.PGBackRestRepoHost{
922+
Log: &v1beta1.LoggingConfiguration{
923+
Path: "/some/directory",
924+
},
925+
}
926+
assert.Equal(t, generateRepoHostLogPath(cluster), "/some/directory")
927+
})
928+
}

0 commit comments

Comments
 (0)