diff --git a/lib/charms/mongodb/v1/mongodb_provider.py b/lib/charms/mongodb/v1/mongodb_provider.py index ec9de773b..4563d8886 100644 --- a/lib/charms/mongodb/v1/mongodb_provider.py +++ b/lib/charms/mongodb/v1/mongodb_provider.py @@ -31,12 +31,13 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 11 +LIBPATCH = 12 logger = logging.getLogger(__name__) REL_NAME = "database" MONGOS_RELATIONS = "cluster" MONGOS_CLIENT_RELATIONS = "mongos_proxy" +EXTERNAL_CONNECTIVITY_TAG = "external-node-connectivity" # We expect the MongoDB container to use the default ports @@ -301,6 +302,10 @@ def _get_config(self, username: str, password: Optional[str]) -> MongoConfigurat } if self.charm.is_role(Config.Role.MONGOS): mongo_args["port"] = Config.MONGOS_PORT + if self.substrate == Config.Substrate.K8S: + external = self.is_external_client(relation.id) + mongo_args["port"] = self.charm.get_mongos_port(external) + mongo_args["hosts"] = self.charm.get_mongos_hosts(external) else: mongo_args["replset"] = self.charm.app.name @@ -396,6 +401,13 @@ def get_relation_name(self): else: return REL_NAME + def is_external_client(self, rel_id) -> bool: + """Returns true if the integrated client requests external connectivity.""" + return ( + self.database_provides.fetch_relation_field(rel_id, EXTERNAL_CONNECTIVITY_TAG) + == "true" + ) + @staticmethod def _get_database_from_relation(relation: Relation) -> Optional[str]: """Return database name from relation.""" diff --git a/src/charm.py b/src/charm.py index 6e6c640f2..29759e2e8 100755 --- a/src/charm.py +++ b/src/charm.py @@ -419,7 +419,7 @@ def _on_config_changed(self, event: ConfigChangedEvent) -> None: # TODO in the future (24.04) support migration of components logger.error( - f"cluster migration currently not supported, cannot change from { self.model.config['role']} to {self.role}" + f"cluster migration currently not supported, cannot change from {self.model.config['role']} to {self.role}" ) raise ShardingMigrationError( f"Migration of sharding components not permitted, revert config role to {self.role}"