Skip to content

Commit

Permalink
[DPE-2561] POC - Start with provided config option: config-server, sh…
Browse files Browse the repository at this point in the history
…ard (#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
[4bf9d5f](4bf9d5f)
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 <[email protected]>
  • Loading branch information
2 people authored and Ubuntu committed Oct 17, 2023
1 parent 628bcce commit a09f068
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 25 deletions.
23 changes: 2 additions & 21 deletions lib/charms/mongodb/v1/helpers.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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()
1 change: 0 additions & 1 deletion lib/charms/mongodb/v1/mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,4 +1377,4 @@ class EarlyRemovalOfConfigServerError(Exception):


if __name__ == "__main__":
main(MongodbOperatorCharm)
main(MongodbOperatorCharm)
2 changes: 1 addition & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/machine_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit a09f068

Please sign in to comment.