From 3bcf09c448683bbe4b4cf80605892361b94c6278 Mon Sep 17 00:00:00 2001 From: Dragomir Penev Date: Fri, 23 Aug 2024 03:12:21 +0300 Subject: [PATCH 1/2] Socket and config flag --- config.yaml | 6 ++++++ src/config.py | 1 + src/relations/pgbouncer_provider.py | 11 ++++++++--- .../pgbouncer_provider/test_pgbouncer_provider.py | 1 + tests/integration/test_config.py | 1 + 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index 1eb6ba8a9..9d4ebaf10 100644 --- a/config.yaml +++ b/config.yaml @@ -21,6 +21,12 @@ options: Virtual IP to use to front pgbouncer units. Used only in case of external node connection. type: string + local_connection_type: + default: localhost + description: | + Type of local connectivity to provide. Must be either 'localhost' or 'socket'. Only applies to the database interface. + type: string + pool_mode: default: session description: | diff --git a/src/config.py b/src/config.py index 7883de226..d3fd9a6a2 100644 --- a/src/config.py +++ b/src/config.py @@ -19,5 +19,6 @@ class CharmConfig(BaseConfigModel): listen_port: PositiveInt metrics_port: PositiveInt vip: Optional[IPvAnyAddress] + local_connection_type: Literal["localhost", "socket"] pool_mode: Literal["session", "transaction", "statement"] max_db_connections: conint(ge=0) diff --git a/src/relations/pgbouncer_provider.py b/src/relations/pgbouncer_provider.py index 1b68f12bc..06f89386a 100644 --- a/src/relations/pgbouncer_provider.py +++ b/src/relations/pgbouncer_provider.py @@ -60,7 +60,7 @@ RelationDepartedEvent, ) -from constants import CLIENT_RELATION_NAME +from constants import CLIENT_RELATION_NAME, PGB_RUN_DIR logger = logging.getLogger(__name__) @@ -239,8 +239,6 @@ def update_connection_info(self, relation): user = f"relation_id_{relation.id}" password = self.database_provides.fetch_my_relation_field(relation.id, "password") - host = "localhost" - uri_host = host if exposed: if self.charm.config.vip: host = str(self.charm.config.vip) @@ -251,6 +249,13 @@ def update_connection_info(self, relation): self.charm.leader_ip, *[ip for ip in self.charm.peers.units_ips if ip != self.charm.leader_ip], ]) + elif self.charm.config.local_connection_type == "socket": + host = f"{PGB_RUN_DIR}/{self.charm.app.name}/instance_0" + uri_host = host + else: + host = "localhost" + uri_host = host + port = self.charm.config.listen_port initial_status = self.charm.unit.status diff --git a/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py b/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py index 9ec13e67e..e998b53b3 100644 --- a/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py +++ b/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py @@ -61,6 +61,7 @@ async def test_database_relation_with_charm_libraries(ops_test: OpsTest, pgb_cha pgb_charm_jammy, application_name=PGB, num_units=None, + config={"local_connection_type": "socket"}, ), ops_test.model.deploy( PG, diff --git a/tests/integration/test_config.py b/tests/integration/test_config.py index 8488f23e1..d18940b49 100644 --- a/tests/integration/test_config.py +++ b/tests/integration/test_config.py @@ -58,6 +58,7 @@ async def test_config_parameters(ops_test: OpsTest, pgb_charm_jammy) -> None: "listen_port": "0", "metrics_port": "0", "vip": test_string, + "local_connection_type": test_string, "pool_mode": test_string, "max_db_connections": "-1", } From d5de74ae3af79afdc2e13b6004c881e44b4b26c5 Mon Sep 17 00:00:00 2001 From: Dragomir Penev Date: Mon, 9 Sep 2024 15:33:51 +0300 Subject: [PATCH 2/2] Change flag values --- config.yaml | 4 ++-- src/config.py | 2 +- src/relations/pgbouncer_provider.py | 2 +- .../relations/pgbouncer_provider/test_pgbouncer_provider.py | 2 +- tests/integration/test_charm.py | 1 + 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/config.yaml b/config.yaml index 9d4ebaf10..c7bf23a72 100644 --- a/config.yaml +++ b/config.yaml @@ -22,9 +22,9 @@ options: type: string local_connection_type: - default: localhost + default: tcp description: | - Type of local connectivity to provide. Must be either 'localhost' or 'socket'. Only applies to the database interface. + Type of local connectivity to provide. Must be either 'tcp' or 'uds'. Only applies to the database interface. type: string pool_mode: diff --git a/src/config.py b/src/config.py index d3fd9a6a2..e8794e0e9 100644 --- a/src/config.py +++ b/src/config.py @@ -19,6 +19,6 @@ class CharmConfig(BaseConfigModel): listen_port: PositiveInt metrics_port: PositiveInt vip: Optional[IPvAnyAddress] - local_connection_type: Literal["localhost", "socket"] + local_connection_type: Literal["tcp", "uds"] pool_mode: Literal["session", "transaction", "statement"] max_db_connections: conint(ge=0) diff --git a/src/relations/pgbouncer_provider.py b/src/relations/pgbouncer_provider.py index a82e6cd7e..3b39ef8bb 100644 --- a/src/relations/pgbouncer_provider.py +++ b/src/relations/pgbouncer_provider.py @@ -249,7 +249,7 @@ def update_connection_info(self, relation): self.charm.peers.leader_ip, *[ip for ip in self.charm.peers.units_ips if ip != self.charm.peers.leader_ip], ]) - elif self.charm.config.local_connection_type == "socket": + elif self.charm.config.local_connection_type == "uds": host = f"{PGB_RUN_DIR}/{self.charm.app.name}/instance_0" uri_host = host else: diff --git a/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py b/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py index e998b53b3..97bcfd31c 100644 --- a/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py +++ b/tests/integration/relations/pgbouncer_provider/test_pgbouncer_provider.py @@ -61,7 +61,7 @@ async def test_database_relation_with_charm_libraries(ops_test: OpsTest, pgb_cha pgb_charm_jammy, application_name=PGB, num_units=None, - config={"local_connection_type": "socket"}, + config={"local_connection_type": "uds"}, ), ops_test.model.deploy( PG, diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index 3426d9874..093f9000c 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -40,6 +40,7 @@ async def test_build_and_deploy(ops_test: OpsTest, pgb_charm_jammy): pgb_charm_jammy, application_name=PGB, num_units=None, + config={"local_connection_type": "uds"}, ), ops_test.model.deploy( PG,