Skip to content

Commit eff8b68

Browse files
committed
FIXUP: mkdir conditionally
1 parent 56a8263 commit eff8b68

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

internal/postgres/config.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"math"
11+
"path"
1112
"strings"
1213

1314
corev1 "k8s.io/api/core/v1"
@@ -315,9 +316,28 @@ func startupCommand(
315316
parameters *ParameterSet,
316317
) []string {
317318
version := fmt.Sprint(cluster.Spec.PostgresVersion)
318-
logDir := parameters.Value("log_directory")
319+
dataDir := DataDirectory(cluster)
319320
walDir := WALDirectory(cluster, instance)
320321

322+
// TODO: describe this?
323+
mkdirs := make([]string, 0, 6)
324+
mkdir := func(b, p string) {
325+
if path.IsAbs(p) {
326+
p = path.Clean(p)
327+
} else {
328+
p = path.Join(dataDir, p)
329+
}
330+
331+
// Create directories unless they are in the empty Postgres data directory.
332+
mkdirs = append(mkdirs,
333+
`[[ `+shell.QuoteWord(p)+` != "${postgres_data_directory}"* || -f "${postgres_data_directory}/PG_VERSION" ]] &&`,
334+
`{ (`+shell.MakeDirectories(b, p)+`) || halt "$(permissions `+shell.QuoteWord(p)+` ||:)"; }`,
335+
)
336+
}
337+
mkdir(dataMountPath, naming.PGBackRestPGDataLogPath)
338+
mkdir(dataMountPath, naming.PatroniPGDataLogPath)
339+
mkdir(dataDir, parameters.Value("log_directory"))
340+
321341
// If the user requests tablespaces, we want to make sure the directories exist with the
322342
// correct owner and permissions.
323343
tablespaceCmd := ""
@@ -445,14 +465,7 @@ chmod +x /tmp/pg_rewind_tde.sh
445465
`else (halt Permissions!); fi ||`,
446466
`halt "$(permissions "${postgres_data_directory}" ||:)"`,
447467

448-
// Create log directories.
449-
`(` + shell.MakeDirectories(dataMountPath, naming.PGBackRestPGDataLogPath) + `) ||`,
450-
`halt "$(permissions ` + naming.PGBackRestPGDataLogPath + ` ||:)"`,
451-
`(` + shell.MakeDirectories(dataMountPath, naming.PatroniPGDataLogPath) + `) ||`,
452-
`halt "$(permissions ` + naming.PatroniPGDataLogPath + ` ||:)"`,
453-
`(` + shell.MakeDirectories(DataDirectory(cluster), logDir) + `) ||`,
454-
// FIXME: This error prints the wrong directory when logDir is relative (the default).
455-
`halt "$(permissions ` + logDir + ` ||:)"`,
468+
strings.Join(mkdirs, "\n"),
456469

457470
// Copy replication client certificate files
458471
// from the /pgconf/tls/replication directory to the /tmp/replication directory in order

internal/postgres/reconcile_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ initContainers:
270270
recreate "${postgres_data_directory}" '0750'
271271
else (halt Permissions!); fi ||
272272
halt "$(permissions "${postgres_data_directory}" ||:)"
273-
(mkdir -p '/pgdata/pgbackrest/log' && { chmod 0775 '/pgdata/pgbackrest/log' '/pgdata/pgbackrest' || :; }) ||
274-
halt "$(permissions /pgdata/pgbackrest/log ||:)"
275-
(mkdir -p '/pgdata/patroni/log' && { chmod 0775 '/pgdata/patroni/log' '/pgdata/patroni' || :; }) ||
276-
halt "$(permissions /pgdata/patroni/log ||:)"
277-
(mkdir -p '/pgdata/pg11/log' && { chmod 0775 '/pgdata/pg11/log' || :; }) ||
278-
halt "$(permissions log ||:)"
273+
[[ '/pgdata/pgbackrest/log' != "${postgres_data_directory}"* || -f "${postgres_data_directory}/PG_VERSION" ]] &&
274+
{ (mkdir -p '/pgdata/pgbackrest/log' && { chmod 0775 '/pgdata/pgbackrest/log' '/pgdata/pgbackrest' || :; }) || halt "$(permissions '/pgdata/pgbackrest/log' ||:)"; }
275+
[[ '/pgdata/patroni/log' != "${postgres_data_directory}"* || -f "${postgres_data_directory}/PG_VERSION" ]] &&
276+
{ (mkdir -p '/pgdata/patroni/log' && { chmod 0775 '/pgdata/patroni/log' '/pgdata/patroni' || :; }) || halt "$(permissions '/pgdata/patroni/log' ||:)"; }
277+
[[ '/pgdata/pg11/log' != "${postgres_data_directory}"* || -f "${postgres_data_directory}/PG_VERSION" ]] &&
278+
{ (mkdir -p '/pgdata/pg11/log' && { chmod 0775 '/pgdata/pg11/log' || :; }) || halt "$(permissions '/pgdata/pg11/log' ||:)"; }
279279
install -D --mode=0600 -t "/tmp/replication" "/pgconf/tls/replication"/{tls.crt,tls.key,ca.crt}
280280
281281

0 commit comments

Comments
 (0)