Skip to content

Commit 18c5a4b

Browse files
committed
Configure Patroni Logs to be stored in a file
This commit updates the Patroni configuration so that the instance Pod logs go to a file on the 'pgdata' volume instead of to stdout. This file is located at /pgdata/patroni/log/patroni.log. This is accomplished by setting 'log.dir' in the Patroni YAML configuration. Setting 'log.file_size' to 0 means that the log file will not rollover. - https://patroni.readthedocs.io/en/latest/yaml_configuration.html#log - https://github.com/patroni/patroni/blob/v3.3.4/patroni/log.py#L431-L432 - https://docs.python.org/3.11/library/logging.handlers.html#rotatingfilehandler Issue: PGO-1701
1 parent 12c8207 commit 18c5a4b

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

internal/naming/names.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ const (
131131
)
132132

133133
const (
134+
// PatroniPGDataLogPath is the Patroni default log path configuration used by the
135+
// PostgreSQL instance.
136+
PatroniPGDataLogPath = "/pgdata/patroni/log"
137+
134138
// PGBackRestRepoContainerName is the name assigned to the container used to run pgBackRest
135139
PGBackRestRepoContainerName = "pgbackrest"
136140

internal/patroni/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ func clusterYAML(
5050
// lifetime.
5151
"scope": naming.PatroniScope(cluster),
5252

53+
// Configure the Patroni log path directory
54+
// - https://patroni.readthedocs.io/en/latest/yaml_configuration.html#log
55+
//
56+
// Setting file_size to 0 means that there will be no rollover.
57+
// - https://github.com/patroni/patroni/blob/v3.3.4/patroni/log.py#L431-L432
58+
// - https://docs.python.org/3.11/library/logging.handlers.html#rotatingfilehandler
59+
"log": map[string]any{
60+
"dir": naming.PatroniPGDataLogPath,
61+
"file_size": 0,
62+
},
63+
5364
// Use Kubernetes Endpoints for the distributed configuration store (DCS).
5465
// These values cannot change during the cluster's lifetime.
5566
//

internal/patroni/config_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ kubernetes:
5858
role_label: postgres-operator.crunchydata.com/role
5959
scope_label: postgres-operator.crunchydata.com/patroni
6060
use_endpoints: true
61+
log:
62+
dir: /pgdata/patroni/log
63+
file_size: 0
6164
postgresql:
6265
authentication:
6366
replication:
@@ -116,6 +119,9 @@ kubernetes:
116119
role_label: postgres-operator.crunchydata.com/role
117120
scope_label: postgres-operator.crunchydata.com/patroni
118121
use_endpoints: true
122+
log:
123+
dir: /pgdata/patroni/log
124+
file_size: 0
119125
postgresql:
120126
authentication:
121127
replication:

internal/postgres/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ chmod +x /tmp/pg_rewind_tde.sh
291291
`
292292
}
293293

294-
args := []string{version, walDir, naming.PGBackRestPGDataLogPath}
294+
args := []string{version, walDir, naming.PGBackRestPGDataLogPath, naming.PatroniPGDataLogPath}
295295
script := strings.Join([]string{
296-
`declare -r expected_major_version="$1" pgwal_directory="$2" pgbrLog_directory="$3"`,
296+
`declare -r expected_major_version="$1" pgwal_directory="$2" pgbrLog_directory="$3" patroniLog_directory="$4"`,
297297

298298
// Function to print the permissions of a file or directory and its parents.
299299
bashPermissions,
@@ -369,6 +369,11 @@ chmod +x /tmp/pg_rewind_tde.sh
369369
`install --directory --mode=0775 "${pgbrLog_directory}" ||`,
370370
`halt "$(permissions "${pgbrLog_directory}" ||:)"`,
371371

372+
// Create the Patroni log directory.
373+
`results 'Patroni log directory' "${patroniLog_directory}"`,
374+
`install --directory --mode=0775 "${patroniLog_directory}" ||`,
375+
`halt "$(permissions "${patroniLog_directory}" ||:)"`,
376+
372377
// Copy replication client certificate files
373378
// from the /pgconf/tls/replication directory to the /tmp/replication directory in order
374379
// to set proper file permissions. This is required because the group permission settings

internal/postgres/reconcile_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ initContainers:
230230
- -ceu
231231
- --
232232
- |-
233-
declare -r expected_major_version="$1" pgwal_directory="$2" pgbrLog_directory="$3"
233+
declare -r expected_major_version="$1" pgwal_directory="$2" pgbrLog_directory="$3" patroniLog_directory="$4"
234234
permissions() { while [[ -n "$1" ]]; do set "${1%/*}" "$@"; done; shift; stat -Lc '%A %4u %4g %n' "$@"; }
235235
halt() { local rc=$?; >&2 echo "$@"; exit "${rc/#0/1}"; }
236236
results() { printf '::postgres-operator: %s::%s\n' "$@"; }
@@ -270,6 +270,9 @@ initContainers:
270270
results 'pgBackRest log directory' "${pgbrLog_directory}"
271271
install --directory --mode=0775 "${pgbrLog_directory}" ||
272272
halt "$(permissions "${pgbrLog_directory}" ||:)"
273+
results 'Patroni log directory' "${patroniLog_directory}"
274+
install --directory --mode=0775 "${patroniLog_directory}" ||
275+
halt "$(permissions "${patroniLog_directory}" ||:)"
273276
install -D --mode=0600 -t "/tmp/replication" "/pgconf/tls/replication"/{tls.crt,tls.key,ca.crt}
274277
275278
@@ -286,6 +289,7 @@ initContainers:
286289
- "11"
287290
- /pgdata/pg11_wal
288291
- /pgdata/pgbackrest/log
292+
- /pgdata/patroni/log
289293
env:
290294
- name: PGDATA
291295
value: /pgdata/pg11
@@ -473,7 +477,7 @@ volumes:
473477

474478
// Startup moves WAL files to data volume.
475479
assert.DeepEqual(t, pod.InitContainers[0].Command[4:],
476-
[]string{"startup", "11", "/pgdata/pg11_wal", "/pgdata/pgbackrest/log"})
480+
[]string{"startup", "11", "/pgdata/pg11_wal", "/pgdata/pgbackrest/log", "/pgdata/patroni/log"})
477481
})
478482

479483
t.Run("WithAdditionalConfigFiles", func(t *testing.T) {
@@ -703,7 +707,7 @@ volumes:
703707

704708
// Startup moves WAL files to WAL volume.
705709
assert.DeepEqual(t, pod.InitContainers[0].Command[4:],
706-
[]string{"startup", "11", "/pgwal/pg11_wal", "/pgdata/pgbackrest/log"})
710+
[]string{"startup", "11", "/pgwal/pg11_wal", "/pgdata/pgbackrest/log", "/pgdata/patroni/log"})
707711
})
708712
}
709713

0 commit comments

Comments
 (0)