Skip to content

Commit

Permalink
[DPE 5235] update lib for mongos k8s ext connections (#474)
Browse files Browse the repository at this point in the history
## Issue
mongos k8s charm handles ext connections by providing a separate port +
ip address

## Solution
update the lib to support this
  • Loading branch information
MiaAltieri committed Aug 30, 2024
1 parent f5782b7 commit c26af1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/charms/mongodb/v1/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit c26af1b

Please sign in to comment.