From a09f068b43e49cc3a9c608f3e806a1f51e9f4fbc Mon Sep 17 00:00:00 2001 From: Mia Altieri <32723809+MiaAltieri@users.noreply.github.com> Date: Wed, 20 Sep 2023 08:57:32 +0200 Subject: [PATCH] [DPE-2561] POC - Start with provided config option: config-server, shard (#246) This PR enables the user to pass the following config options: `replication`(default), `shard`, and `config-server`. This PR supports this options by starting the charm as a `shard` or a `config-server`. If the config option `config-server` is provided, then the charm starts an internal `mongos` service that runs on 0.0.0.0 and is configured to the provided config server. As a POC there are not tests included in this PR. Testing was performed by hand with: ``` charmcraft pack juju deploy ./*charm --config role="shard" shard-one juju deploy ./*charm --config role="config-server" config-server watch -n1 --color juju status --color juju ssh shard-one/0 systemctl status snap.charmed-mongodb.mongod.service exit juju ssh config-server/0 systemctl status snap.charmed-mongodb.mongod.service systemctl status snap.charmed-mongodb.mongos.service exit ``` New snap revison packages a new version of the PBM tool which updated how errors were handled when querying PBM status, commit [4bf9d5f9608976cf84d813f521826ea25eb9e04b](https://github.com/canonical/mongodb-operator/pull/246/commits/4bf9d5f9608976cf84d813f521826ea25eb9e04b) reflects these necessary changes Follow up PR is to be made immediately after merging of this feature is completed and is a requirement of finishing [DPE-2561](https://warthogs.atlassian.net/browse/DPE-2561). For this PR this will be starting `mongos` with a `--auth` and the same `--keyFile` used to start the`mongod` service Future PR is to be started after the follow up PR has been made. Once `mongos` is started with auth [DPE-2562](https://warthogs.atlassian.net/browse/DPE-2562) will be started. This includes creating a basic shared library between `config-server` and `shard` components. In this PR we will: - implement keyfile sharing across shard and config server components - implement adding shards to cluster (Shard removal is saved for later.) As a POC this PR doesn't include handling edge cases, intelligent status reporting, unit tests, or integration tests. These will be handled later on down the line. Specifically: 1. block config change events for changing role of charm 2. report status of internal `mongos` in `update_status` 3. unit tests and integration tests [DPE-2561]: https://warthogs.atlassian.net/browse/DPE-2561?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Pedro Guimaraes --- lib/charms/mongodb/v1/helpers.py | 23 ++--------------------- lib/charms/mongodb/v1/mongodb_backups.py | 1 - src/charm.py | 2 +- src/config.py | 2 +- src/machine_helpers.py | 2 +- 5 files changed, 5 insertions(+), 25 deletions(-) diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 029ae05ed..9d0e288b6 100644 --- a/lib/charms/mongodb/v1/helpers.py +++ b/lib/charms/mongodb/v1/helpers.py @@ -1,14 +1,13 @@ """Simple functions, which can be used in both K8s and VM charms.""" # Copyright 2023 Canonical Ltd. # See LICENSE file for licensing details. -from ctypes import Union import json import logging import os import secrets import string import subprocess -from typing import List, Optional +from typing import List from charms.mongodb.v0.mongodb import MongoDBConfiguration, MongoDBConnection from ops.model import ( @@ -243,24 +242,6 @@ def copy_licenses_to_unit(): ) -_StrOrBytes = Union[str, bytes] - - -def process_pbm_error(error_string: Optional[_StrOrBytes]) -> str: - """Parses pbm error string and returns a user friendly message.""" - message = "couldn't configure s3 backup option" - if not error_string: - return message - if type(error_string) is bytes: - error_string = error_string.decode("utf-8") - if "status code: 403" in error_string: # type: ignore - message = "s3 credentials are incorrect." - elif "status code: 404" in error_string: # type: ignore - message = "s3 configurations are incompatible." - elif "status code: 301" in error_string: # type: ignore - message = "s3 configurations are incompatible." - return message - def current_pbm_op(pbm_status: str) -> str: """Parses pbm status for the operation that pbm is running.""" pbm_status = json.loads(pbm_status) @@ -285,4 +266,4 @@ def process_pbm_status(pbm_status: str) -> StatusBase: if current_op["type"] == "resync": return WaitingStatus("waiting to sync s3 configurations.") - return ActiveStatus() + return ActiveStatus() \ No newline at end of file diff --git a/lib/charms/mongodb/v1/mongodb_backups.py b/lib/charms/mongodb/v1/mongodb_backups.py index d8280c86f..a71fead3f 100644 --- a/lib/charms/mongodb/v1/mongodb_backups.py +++ b/lib/charms/mongodb/v1/mongodb_backups.py @@ -681,7 +681,6 @@ def process_pbm_error(self, pbm_status: Optional[_StrOrBytes]) -> str: """Returns errors found in PBM status.""" if type(pbm_status) is bytes: pbm_status = pbm_status.decode("utf-8") - try: error_message = self.retrieve_error_message(json.loads(pbm_status)) except json.decoder.JSONDecodeError: diff --git a/src/charm.py b/src/charm.py index 909330b89..a69c1983d 100755 --- a/src/charm.py +++ b/src/charm.py @@ -1377,4 +1377,4 @@ class EarlyRemovalOfConfigServerError(Exception): if __name__ == "__main__": - main(MongodbOperatorCharm) + main(MongodbOperatorCharm) \ No newline at end of file diff --git a/src/config.py b/src/config.py index 073065bee..40af7d0c0 100644 --- a/src/config.py +++ b/src/config.py @@ -86,4 +86,4 @@ class Secrets: SECRET_KEYFILE_NAME = "keyfile" SECRET_INTERNAL_LABEL = "internal-secret" SECRET_DELETED_LABEL = "None" - MAX_PASSWORD_LENGTH = 4096 + MAX_PASSWORD_LENGTH = 4096 \ No newline at end of file diff --git a/src/machine_helpers.py b/src/machine_helpers.py index 2d64ef37a..746fd6474 100644 --- a/src/machine_helpers.py +++ b/src/machine_helpers.py @@ -46,4 +46,4 @@ def add_args_to_env(var: str, args: str): env_vars.append(f"{var}={args}") with open(Config.ENV_VAR_PATH, "w") as service_file: - service_file.writelines(env_vars) + service_file.writelines(env_vars) \ No newline at end of file