Skip to content

Commit

Permalink
Merge pull request #42 from canonical/DPE-3911-update-charm-to-send-c…
Browse files Browse the repository at this point in the history
…orrect-host-to-client

[DPE-3911] update charm to send correct port to client
  • Loading branch information
MiaAltieri authored Oct 10, 2024
2 parents 9b0e8a1 + 70520b6 commit cca0602
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/charms/mongodb/v1/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -459,15 +468,15 @@ 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
port that is associated with the client.
"""
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

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit cca0602

Please sign in to comment.