From 3d98182b70c0b12e52668187b237112f476c142e Mon Sep 17 00:00:00 2001 From: Mia Altieri <32723809+MiaAltieri@users.noreply.github.com> Date: Fri, 16 Feb 2024 08:53:31 +0100 Subject: [PATCH] [DPE-3293] Changes external mongos (#350) ## Issue Mongos charm does not support external connections ## Solution Enable mongos to provide an external connection when it is requested by the host charm ## Note There are changes required in the shared MongoDB libs - but the bulk of this work will be done in the Mongos Charm --- lib/charms/mongodb/v0/config_server_interface.py | 15 ++++++++++++--- lib/charms/mongodb/v1/helpers.py | 9 +++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/charms/mongodb/v0/config_server_interface.py b/lib/charms/mongodb/v0/config_server_interface.py index f0a9b20fe..9e05e6a1f 100644 --- a/lib/charms/mongodb/v0/config_server_interface.py +++ b/lib/charms/mongodb/v0/config_server_interface.py @@ -35,7 +35,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 6 +LIBPATCH = 7 class ClusterProvider(Object): @@ -275,7 +275,13 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None: def is_mongos_running(self) -> bool: """Returns true if mongos service is running.""" - with MongosConnection(None, f"mongodb://{MONGOS_SOCKET_URI_FMT}") as mongo: + connection_uri = f"mongodb://{self.charm.get_mongos_host()}" + + # when running internally, connections through Unix Domain sockets do not need port. + if self.charm.is_external_client: + connection_uri = connection_uri + f":{Config.MONGOS_PORT}" + + with MongosConnection(None, connection_uri) as mongo: return mongo.is_ready def update_config_server_db(self, config_server_db) -> bool: @@ -285,7 +291,10 @@ def update_config_server_db(self, config_server_db) -> bool: mongos_config = self.charm.mongos_config mongos_start_args = get_mongos_args( - mongos_config, snap_install=True, config_server_db=config_server_db + mongos_config, + snap_install=True, + config_server_db=config_server_db, + external_connectivity=self.charm.is_external_client, ) add_args_to_env("MONGOS_ARGS", mongos_start_args) return True diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 69459b971..9038198d1 100644 --- a/lib/charms/mongodb/v1/helpers.py +++ b/lib/charms/mongodb/v1/helpers.py @@ -30,7 +30,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 3 +LIBPATCH = 4 # path to store mongodb ketFile KEY_FILE = "keyFile" @@ -96,6 +96,7 @@ def get_mongos_args( config, snap_install: bool = False, config_server_db: str = None, + external_connectivity: bool = True, ) -> str: """Returns the arguments used for starting mongos on a config-server side application. @@ -104,9 +105,9 @@ def get_mongos_args( """ # suborinate charm which provides its own config_server_db, should only use unix domain socket binding_ips = ( - f"--bind_ip {MONGODB_COMMON_DIR}/var/mongodb-27018.sock" - if config_server_db - else "--bind_ip_all" + "--bind_ip_all" + if external_connectivity + else f"--bind_ip {MONGODB_COMMON_DIR}/var/mongodb-27018.sock" ) # mongos running on the config server communicates through localhost