Skip to content

Commit a9c2480

Browse files
[DPE-10417] feat: migrate shared constants to single kernel (#147)
* feat: migrate shared constants to single kernel Consolidate the cross-cutting constants the charms still define locally (database name/port, metrics ports, TLS file names, override maps, mapping labels, tracing and pgBackRest paths) into the library so both substrates read one source instead of drifting copies. The secrets/scopes/password-key constants are intentionally omitted — they were already migrated by the start/install refactor. Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> * feat: include backup user in SYSTEM_USERS Both the VM and K8s charms must treat the backup user as a managed system user (password rotation and internal access-group grants). The VM charm previously compensated by appending BACKUP_USER at individual call sites while K8s already listed it locally. Promoting it into the shared SYSTEM_USERS makes the library the single source of truth and lets both charms drop their divergent local definitions. Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> * feat: add ALL_CLIENT_RELATIONS shared constant The set of client-facing relations was defined locally in the VM charm. Hoisting it into the shared library gives charms a single definition to consume and keeps the client-relation set consistent across substrates as it grows. Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> * feat: migrate operational charm constants to the library Backup ID formats, async-replication relation names, pgBackRest log/error settings, the tracing protocol and the watcher relation constants were defined locally in the charms. Hoisting them into the shared library removes per-charm duplication (the replication relation names were defined in both charms) and lets the charms consume a single definition. Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> --------- Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
1 parent 9ca39cc commit a9c2480

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

single_kernel_postgresql/config/literals.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
REWIND_USER = "rewind"
2222
SNAP_USER = "_daemon_"
2323
USER = "operator"
24-
SYSTEM_USERS = [MONITORING_USER, REPLICATION_USER, REWIND_USER, USER]
24+
SYSTEM_USERS = [BACKUP_USER, MONITORING_USER, REPLICATION_USER, REWIND_USER, USER]
2525

2626
# Paths
2727
## VM Paths
@@ -100,3 +100,64 @@
100100
CONTAINER_NAME = "postgresql"
101101

102102
API_REQUEST_TIMEOUT = 5
103+
104+
105+
# --- Shared constants migrated from the charms ---
106+
107+
# Database
108+
DATABASE = "database"
109+
DATABASE_DEFAULT_NAME = "postgres"
110+
DATABASE_PORT = "5432"
111+
112+
# Client relations
113+
ALL_CLIENT_RELATIONS = [DATABASE]
114+
115+
# Async replication relations
116+
REPLICATION_CONSUMER_RELATION = "replication"
117+
REPLICATION_OFFER_RELATION = "replication-offer"
118+
119+
# TLS files
120+
TLS_KEY_FILE = "key.pem"
121+
TLS_CA_FILE = "ca.pem"
122+
TLS_CERT_FILE = "cert.pem"
123+
124+
# Metrics ports (kept as str to match the K8s charm; VM adapts on flip)
125+
METRICS_PORT = "9187"
126+
PGBACKREST_METRICS_PORT = "9854"
127+
128+
# Secret/database mapping labels
129+
USERNAME_MAPPING_LABEL = "custom-usernames"
130+
DATABASE_MAPPING_LABEL = "prefix-databases"
131+
132+
# Overrides
133+
BACKUP_TYPE_OVERRIDES = {"full": "full", "differential": "diff", "incremental": "incr"}
134+
PLUGIN_OVERRIDES = {"audit": "pgaudit", "uuid_ossp": '"uuid-ossp"'}
135+
136+
# SPI extension modules
137+
SPI_MODULE = ["refint", "autoinc", "insert_username", "moddatetime"]
138+
139+
# Tracing
140+
TRACING_RELATION_NAME = "tracing"
141+
TRACING_PROTOCOL = "otlp_http"
142+
143+
# Patroni
144+
PATRONI_CLUSTER_STATUS_ENDPOINT = "cluster"
145+
146+
# pgBackRest
147+
PGBACKREST_LOGROTATE_FILE = "/etc/logrotate.d/pgbackrest.logrotate"
148+
# pgBackRest emits all error/warning output on stderr (the default is stdout) so error
149+
# extraction stays consistent. Reference: https://pgbackrest.org/configuration.html#section-log
150+
PGBACKREST_LOG_LEVEL_STDERR = "--log-level-stderr=warn"
151+
# Archive timeout: unable to archive WAL files within the configured timeout period.
152+
PGBACKREST_ARCHIVE_TIMEOUT_ERROR_CODE = 82
153+
154+
# Backup ID formats
155+
BACKUP_ID_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
156+
PGBACKREST_BACKUP_ID_FORMAT = "%Y%m%d-%H%M%S"
157+
158+
# Watcher relation (VM)
159+
WATCHER_OFFER_RELATION = "watcher-offer"
160+
WATCHER_RELATION = "watcher"
161+
WATCHER_USER = "watcher"
162+
WATCHER_PASSWORD_KEY = "watcher-password" # noqa: S105
163+
WATCHER_SECRET_LABEL = "watcher-secret" # noqa: S105

tests/unit/test_literals.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2026 Canonical Ltd.
2+
# See LICENSE file for licensing details.
3+
from single_kernel_postgresql.config import literals
4+
5+
6+
def test_shared_string_constants():
7+
assert literals.DATABASE == "database"
8+
assert literals.DATABASE_DEFAULT_NAME == "postgres"
9+
assert literals.DATABASE_PORT == "5432"
10+
assert literals.PATRONI_CLUSTER_STATUS_ENDPOINT == "cluster"
11+
assert literals.TLS_KEY_FILE == "key.pem"
12+
assert literals.TLS_CA_FILE == "ca.pem"
13+
assert literals.TLS_CERT_FILE == "cert.pem"
14+
assert literals.USERNAME_MAPPING_LABEL == "custom-usernames"
15+
assert literals.DATABASE_MAPPING_LABEL == "prefix-databases"
16+
assert literals.TRACING_RELATION_NAME == "tracing"
17+
assert literals.PGBACKREST_LOGROTATE_FILE == "/etc/logrotate.d/pgbackrest.logrotate"
18+
19+
20+
def test_shared_collection_constants():
21+
assert literals.BACKUP_TYPE_OVERRIDES == {
22+
"full": "full",
23+
"differential": "diff",
24+
"incremental": "incr",
25+
}
26+
assert literals.PLUGIN_OVERRIDES == {"audit": "pgaudit", "uuid_ossp": '"uuid-ossp"'}
27+
assert literals.SPI_MODULE == ["refint", "autoinc", "insert_username", "moddatetime"]
28+
29+
30+
def test_metrics_ports_are_str():
31+
assert literals.METRICS_PORT == "9187"
32+
assert literals.PGBACKREST_METRICS_PORT == "9854"
33+
assert isinstance(literals.METRICS_PORT, str)
34+
assert isinstance(literals.PGBACKREST_METRICS_PORT, str)

0 commit comments

Comments
 (0)