From cf7ed168a88729a2815fe93595add4079b3238cf Mon Sep 17 00:00:00 2001 From: Mia Altieri <32723809+MiaAltieri@users.noreply.github.com> Date: Wed, 14 Aug 2024 11:46:03 +0200 Subject: [PATCH] [DPE-5040] - make necessary lib changes for mongos-k8s connections with config-server (#456) ## Issue The k8s router has a slightly different implementation from the VM router. This means that the current lib that supports the router interface does not work with K8s Namely the following has a different implementation on the K8s router: 1. sharing user info to client 2. k8s router communicates via port 3. k8s router updates arguments when changing the pebble layer on restart ## Solution To fix this some cases have been made in the charm lib 1. k8s charm does not provide its admin user/password to the client (instead it will handle this with the mongos_client interface in DPE-5090 2. communicate via port in K8s 3. dont make a call to update mongos args in K8s --- charm_internal_version | 2 +- .../mongodb/v0/config_server_interface.py | 34 ++++++++++++++----- src/config.py | 6 ++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/charm_internal_version b/charm_internal_version index b6a7d89c6..98d9bcb75 100644 --- a/charm_internal_version +++ b/charm_internal_version @@ -1 +1 @@ -16 +17 diff --git a/lib/charms/mongodb/v0/config_server_interface.py b/lib/charms/mongodb/v0/config_server_interface.py index e3209a090..ba515f3e0 100644 --- a/lib/charms/mongodb/v0/config_server_interface.py +++ b/lib/charms/mongodb/v0/config_server_interface.py @@ -42,8 +42,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version - -LIBPATCH = 8 +LIBPATCH = 9 class ClusterProvider(Object): @@ -206,9 +205,13 @@ class ClusterRequirer(Object): """Manage relations between the config server and mongos router on the mongos side.""" def __init__( - self, charm: CharmBase, relation_name: str = Config.Relations.CLUSTER_RELATIONS_NAME + self, + charm: CharmBase, + relation_name: str = Config.Relations.CLUSTER_RELATIONS_NAME, + substrate: str = Config.Substrate.VM, ) -> None: """Constructor for ShardingProvider object.""" + self.substrate = substrate self.relation_name = relation_name self.charm = charm self.database_requires = DatabaseRequires( @@ -254,7 +257,10 @@ def _on_database_created(self, event) -> None: logger.info("Database and user created for mongos application") self.charm.set_secret(Config.Relations.APP_SCOPE, Config.Secrets.USERNAME, event.username) self.charm.set_secret(Config.Relations.APP_SCOPE, Config.Secrets.PASSWORD, event.password) - self.charm.share_connection_info() + + # K8s charm have a 1:Many client scheme and share connection info in a different manner. + if self.substrate == Config.Substrate.VM: + self.charm.share_connection_info() def _on_relation_changed(self, event) -> None: """Starts/restarts monogs with config server information.""" @@ -318,7 +324,10 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None: logger.info("Database and user removed for mongos application") self.charm.remove_secret(Config.Relations.APP_SCOPE, Config.Secrets.USERNAME) self.charm.remove_secret(Config.Relations.APP_SCOPE, Config.Secrets.PASSWORD) - self.charm.remove_connection_info() + + # K8s charm have a 1:Many client scheme and share connection info in a different manner. + if self.substrate == Config.Substrate.VM: + self.charm.remove_connection_info() # BEGIN: helper functions def pass_hook_checks(self, event): @@ -361,8 +370,8 @@ def is_mongos_running(self) -> bool: """Returns true if mongos service is running.""" 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: + # use the mongos port for k8s charms and external connections on VM + if self.charm.is_external_client or self.substrate == Config.K8S_SUBSTRATE: connection_uri = connection_uri + f":{Config.MONGOS_PORT}" with MongosConnection(None, connection_uri) as mongo: @@ -373,7 +382,9 @@ def update_config_server_db(self, config_server_db) -> bool: if self.charm.config_server_db == config_server_db: return False - self.charm.update_mongos_args(config_server_db) + if self.substrate == Config.Substrate.VM: + self.charm.update_mongos_args(config_server_db) + return True def update_keyfile(self, key_file_contents: str) -> bool: @@ -420,6 +431,13 @@ def get_config_server_name(self) -> Optional[str]: # metadata.yaml prevents having multiple config servers return self.model.get_relation(self.relation_name).app.name + def get_config_server_uri(self) -> str: + """Returns the short form URI of the config server.""" + return self.database_requires.fetch_relation_field( + self.model.get_relation(Config.Relations.CLUSTER_RELATIONS_NAME).id, + CONFIG_SERVER_DB_KEY, + ) + def is_ca_compatible(self) -> bool: """Returns true if both the mongos and the config server use the same CA.""" config_server_relation = self.charm.model.get_relation(self.relation_name) diff --git a/src/config.py b/src/config.py index b75535425..fa8473eb1 100644 --- a/src/config.py +++ b/src/config.py @@ -120,6 +120,12 @@ class Status: # TODO Future PR add more status messages here as constants UNHEALTHY_UPGRADE = BlockedStatus("Unhealthy after upgrade.") + class Substrate: + """Substrate related constants.""" + + VM = "vm" + K8S = "k8s" + class Upgrade: """Upgrade related constants."""