Skip to content

Commit cba92af

Browse files
committed
feat(config): move patroni template into the lib as one substrate-conditional template
The two charms each carried their own templates/patroni.yml.j2 and the config manager loaded it with a CWD-relative open(), so the lib could not own the render. Merge both copies into a single template the lib ships and loads as package data, so the charms delete their copies in the adoption PR and the template travels with the code that renders it rather than with each charm's working directory. The VM and K8s templates diverge in section ordering, whole blocks (raft, bypass_api_service, pod_ip), path variables, and pg_hba rules, so the merged template branches at the top level on a substrate context var with each substrate's body kept verbatim. That guarantees byte-for-byte identical output per substrate and avoids interleaving conditionals that could silently shift a byte. render_patroni_yml_file now loads the template via importlib.resources and passes the substrate the template branches on. Golden tests render the merged template against byte copies of each charm's original template across the full matrix of conditional dimensions (tls, connectivity, ldap, restore/pitr, slots, peers, watcher, extra replication endpoints, tags) and assert equality, so any divergent byte fails the suite. Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
1 parent f43f4f8 commit cba92af

7 files changed

Lines changed: 1157 additions & 5 deletions

File tree

single_kernel_postgresql/managers/config.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Responsible for managing the configuration of the PostgreSQL instance.
88
"""
99

10+
import importlib.resources
1011
import logging
1112
from typing import Any
1213

@@ -381,11 +382,17 @@ def render_patroni_yml_file(
381382
logger.warning("Passwords are not yet generated by the leader")
382383
return
383384

384-
# Open the template patroni.yml file.
385-
with open("templates/patroni.yml.j2") as file:
386-
template = Template(file.read())
385+
# Load the template shipped as package data, not relative to the CWD.
386+
template_source = (
387+
importlib.resources
388+
.files("single_kernel_postgresql.templates")
389+
.joinpath("patroni.yml.j2")
390+
.read_text()
391+
)
392+
template = Template(template_source)
387393

388394
confs = {
395+
"substrate": self.state.substrate,
389396
"connectivity": connectivity,
390397
"enable_ldap": enable_ldap,
391398
"enable_tls": enable_tls,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2025 Canonical Ltd.
2+
# See LICENSE file for licensing details.
3+
4+
"""Jinja2 templates rendered by the charm managers."""

0 commit comments

Comments
 (0)