From 70520b65beb228697996bd0b3c75cd077179e5a4 Mon Sep 17 00:00:00 2001 From: Mia Altieri Date: Thu, 10 Oct 2024 08:02:16 +0000 Subject: [PATCH] update charm to send correct port to clinet --- lib/charms/mongodb/v1/mongodb_provider.py | 3 ++- src/charm.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/charms/mongodb/v1/mongodb_provider.py b/lib/charms/mongodb/v1/mongodb_provider.py index 2fc78b25..41b6fd65 100644 --- a/lib/charms/mongodb/v1/mongodb_provider.py +++ b/lib/charms/mongodb/v1/mongodb_provider.py @@ -31,7 +31,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 13 +LIBPATCH = 14 logger = logging.getLogger(__name__) REL_NAME = "database" @@ -372,6 +372,7 @@ def _get_config( mongo_args["port"] = Config.MONGOS_PORT if self.substrate == Config.Substrate.K8S: mongo_args["hosts"] = self.charm.get_mongos_hosts_for_client() + mongo_args["port"] = self.charm.get_mongos_port() else: mongo_args["replset"] = self.charm.app.name diff --git a/src/charm.py b/src/charm.py index 7d28bc7a..fec653ce 100755 --- a/src/charm.py +++ b/src/charm.py @@ -450,6 +450,15 @@ def get_units(self) -> List[Unit]: units.extend(self.peers_units) return units + def get_mongos_port(self) -> int: + """Returns the port for this unit""" + if self.is_external_client: + self.node_port_manager.get_node_port( + port_to_match=Config.MONGOS_PORT, unit_name=self.unit.name + ) + + return Config.MONGOS_PORT + def get_mongos_host(self) -> str: """Returns the host for mongos as a str. @@ -459,7 +468,7 @@ def get_mongos_host(self) -> str: unit_id = self.unit.name.split("/")[1] return f"{self.app.name}-{unit_id}.{self.app.name}-endpoints" - def get_ext_mongos_hosts(self) -> Set: + def get_ext_mongos_hosts(self, incl_port: bool = True) -> Set: """Returns the ext hosts for mongos. Note: for external connections it is not enough to know the external ip, but also the @@ -467,7 +476,7 @@ def get_ext_mongos_hosts(self) -> Set: """ hosts = set() for unit in self.get_units(): - hosts.add(self.get_ext_mongos_host(unit)) + hosts.add(self.get_ext_mongos_host(unit, incl_port=incl_port)) return hosts @@ -513,7 +522,7 @@ def get_mongos_hosts_for_client(self) -> Set: the app has been configured. """ if self.is_external_client: - return self.get_ext_mongos_hosts() + return self.get_ext_mongos_hosts(incl_port=False) return self.get_k8s_mongos_hosts()