Skip to content

Commit

Permalink
Merge branch '6/edge' into DPE-5089-use-node-port
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Aug 28, 2024
2 parents 7c45892 + 04475f9 commit 8d357a7
Show file tree
Hide file tree
Showing 17 changed files with 4,366 additions and 101 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
jobs:
lint:
name: Lint
uses: canonical/data-platform-workflows/.github/workflows/lint.yaml@v18.0.0
uses: canonical/data-platform-workflows/.github/workflows/lint.yaml@v20.0.2

lib-check:
name: Check libraries
Expand Down Expand Up @@ -55,20 +55,26 @@ jobs:
uses: codecov/codecov-action@v3

build:
name: Build charm
uses: canonical/data-platform-workflows/.github/workflows/[email protected]
name: Build charm | ${{ matrix.path }}
uses: canonical/data-platform-workflows/.github/workflows/[email protected]
strategy:
matrix:
path:
- .
- tests/integration/application
with:
cache: true
path-to-charm-directory: ${{ matrix.path }}

integration-test:
name: Integration test charm | 3.4.4
needs:
- lint
- unit-test
- build
uses: canonical/data-platform-workflows/.github/workflows/integration_test_charm.yaml@v18.0.0
uses: canonical/data-platform-workflows/.github/workflows/integration_test_charm.yaml@v20.0.2
with:
artifact-prefix: ${{ needs.build.outputs.artifact-prefix }}
artifact-prefix: packed-charm-cache-true
cloud: microk8s
microk8s-snap-channel: 1.29-strict/stable
juju-agent-version: 3.4.4 # renovate: juju-agent-pin-minor
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ jobs:

build:
name: Build charm
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v18.0.0
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v20.0.2

release-charm:
name: Release charm
needs:
- ci-tests
- build
uses: canonical/data-platform-workflows/.github/workflows/release_charm.yaml@v18.0.0
uses: canonical/data-platform-workflows/.github/workflows/release_charm.yaml@v20.0.2
with:
channel: 6/edge
artifact-prefix: ${{ needs.build.outputs.artifact-prefix }}
Expand Down
10 changes: 9 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Copyright 2023 Canonical Ltd.
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

options:
expose-external:
description: "String to determine how to expose the mongos router externally from the Kubernetes cluster. Possible values: 'nodeport', 'none'"
type: string
default: "none"

auto-delete:
type: boolean
description: |
When a relation is removed, auto-delete ensures that any relevant databases
associated with the relation are also removed
default: false
4 changes: 4 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ requires:
interface: config-server
limit: 1

provides:
mongos_proxy:
interface: mongos_client

containers:
mongos:
resource: mongodb-image
Expand Down
58 changes: 31 additions & 27 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ juju = "^3.4.0"
pytest = "^8.1.1"
pytest-mock = "^3.14.0"
pytest-operator = "^0.34.0"
pytest-operator-cache = {git = "https://github.com/canonical/data-platform-workflows", tag = "v16.7.0", subdirectory = "python/pytest_plugins/pytest_operator_cache"}
pytest-operator-groups = {git = "https://github.com/canonical/data-platform-workflows", tag = "v16.7.0", subdirectory = "python/pytest_plugins/pytest_operator_groups"}
pytest-github-secrets = {git = "https://github.com/canonical/data-platform-workflows", tag = "v16.7.0", subdirectory = "python/pytest_plugins/github_secrets"}
allure-pytest-collection-report = {git = "https://github.com/canonical/data-platform-workflows", tag = "v16.7.0", subdirectory = "python/pytest_plugins/allure_pytest_collection_report"}
pytest-operator-cache = {git = "https://github.com/canonical/data-platform-workflows", tag = "v20.0.2", subdirectory = "python/pytest_plugins/pytest_operator_cache"}
pytest-operator-groups = {git = "https://github.com/canonical/data-platform-workflows", tag = "v20.0.2", subdirectory = "python/pytest_plugins/pytest_operator_groups"}
pytest-github-secrets = {git = "https://github.com/canonical/data-platform-workflows", tag = "v20.0.2", subdirectory = "python/pytest_plugins/github_secrets"}
allure-pytest-collection-report = {git = "https://github.com/canonical/data-platform-workflows", tag = "v20.0.2", subdirectory = "python/pytest_plugins/allure_pytest_collection_report"}


# Linting tools configuration
Expand Down
71 changes: 64 additions & 7 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
from ops.main import main
import json
from exceptions import MissingSecretError

from ops.pebble import PathError, ProtocolError, Layer
from node_port import NodePortManager

from typing import Set, Optional, Dict
from charms.mongodb.v0.config_server_interface import ClusterRequirer


from pymongo.errors import PyMongoError

from charms.mongodb.v0.mongo import MongoConfiguration, MongoConnection
from charms.mongos.v0.set_status import MongosStatusHandler
from charms.mongodb.v1.mongodb_provider import MongoDBProvider
from charms.mongodb.v0.mongodb_tls import MongoDBTLS
from charms.mongodb.v0.mongodb_secrets import SecretCache
from charms.mongodb.v0.mongodb_secrets import generate_secret_label
from charms.mongodb.v1.mongos import MongoConfiguration, MongoConnection


from charms.mongodb.v1.helpers import get_mongos_args

from config import Config
Expand Down Expand Up @@ -82,6 +84,12 @@ def __init__(self, *args):
self.tls = MongoDBTLS(self, Config.Relations.PEERS, substrate=Config.SUBSTRATE)
self.cluster = ClusterRequirer(self, substrate=Config.SUBSTRATE)

self.client_relations = MongoDBProvider(
self,
substrate=Config.SUBSTRATE,
relation_name=Config.Relations.CLIENT_RELATIONS_NAME,
)

# BEGIN: hook functions
def _on_config_changed(self, event: ConfigChangedEvent) -> None:
"""Listen to changes in the application configuration."""
Expand Down Expand Up @@ -152,9 +160,31 @@ def _on_start(self, event: StartEvent) -> None:
# start hooks are fired before relation hooks and `mongos` requires a config-server in
# order to start. Wait to receive config-server info from the relation event before
# starting `mongos` daemon
self.status.set_and_share_status(
BlockedStatus("Missing relation to config-server.")
)
if not self.is_integrated_to_config_server():
logger.info(
"Missing integration to config-server. mongos cannot run start sequence unless connected to config-server."
)
self.status.set_and_share_status(
BlockedStatus("Missing relation to config-server.")
)
event.defer()
return

if not self.cluster.is_mongos_running():
logger.debug("mongos service is not ready yet.")
event.defer()
return

self.db_initialised = True

try:
self.client_relations.oversee_users(None, None)
except PyMongoError as e:
logger.error(
"Failed to create mongos client users, due to %r. Will defer and try again",
e,
)
event.defer()

def _on_update_status(self, _):
"""Handle the update status event"""
Expand Down Expand Up @@ -547,7 +577,7 @@ def expose_external(self) -> Optional[str]:

@expose_external.setter
def expose_external(self, expose_external: str) -> None:
"""Set the db_initialised flag."""
"""Set the expose_external flag."""
if not self.unit.is_leader():
return

Expand All @@ -556,6 +586,28 @@ def expose_external(self, expose_external: str) -> None:

self.app_peer_data["expose-external"] = expose_external

@property
def db_initialised(self) -> bool:
"""Check if mongos is initialised.
Named `db_initialised` rather than `router_initialised` due to need for parity across DB
charms.
"""
return json.loads(self.app_peer_data.get("db_initialised", "false"))

@db_initialised.setter
def db_initialised(self, value):
"""Set the db_initalised flag."""
if not self.unit.is_leader():
return

if isinstance(value, bool):
self.app_peer_data["db_initialised"] = json.dumps(value)
else:
raise ValueError(
f"'db_initialised' must be a boolean value. Proivded: {value} is of type {type(value)}"
)

@property
def peers_units(self) -> list[Unit]:
"""Get peers units in a safe way."""
Expand Down Expand Up @@ -637,6 +689,11 @@ def extra_user_roles(self) -> Set[str]:
"""
return Config.USER_ROLE_CREATE_USERS

@property
def mongo_config(self) -> MongoConfiguration:
"""Returns a MongoConfiguration object for shared libs with agnostic mongo commands."""
return self.mongos_config

@property
def mongos_config(self) -> MongoConfiguration:
"""Generates a MongoDBConfiguration object for mongos in the deployment of MongoDB."""
Expand Down
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Relations:
UNIT_SCOPE = "unit"
PEERS = "router-peers"
CLUSTER_RELATIONS_NAME = "cluster"
CLIENT_RELATIONS_NAME = "mongos_proxy"
Scopes = Literal[APP_SCOPE, UNIT_SCOPE]

class TLS:
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/application/charmcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

type: charm
bases:
- build-on:
- name: "ubuntu"
channel: "22.04"
run-on:
- name: "ubuntu"
channel: "22.04"
Loading

0 comments on commit 8d357a7

Please sign in to comment.