Skip to content

Commit

Permalink
Attempt to standardize code after merge of external-expose PR in the …
Browse files Browse the repository at this point in the history
…K8s charm
  • Loading branch information
shayancanonical committed Dec 4, 2024
1 parent 25121e4 commit ddb6349
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 87 deletions.
53 changes: 20 additions & 33 deletions src/abstract_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,25 @@ def _logrotate(self) -> logrotate.LogRotate:

@property
@abc.abstractmethod
def _read_write_endpoint(self) -> str:
def _read_write_endpoints(self) -> str:
"""MySQL Router read-write endpoint"""

@property
@abc.abstractmethod
def _read_only_endpoint(self) -> str:
def _read_only_endpoints(self) -> str:
"""MySQL Router read-only endpoint"""

@property
@abc.abstractmethod
def _exposed_read_write_endpoint(self) -> typing.Optional[str]:
def _exposed_read_write_endpoints(self) -> typing.Optional[str]:
"""The exposed read-write endpoint.
Only defined in vm charm.
"""

@property
@abc.abstractmethod
def _exposed_read_only_endpoint(self) -> typing.Optional[str]:
def _exposed_read_only_endpoints(self) -> typing.Optional[str]:
"""The exposed read-only endpoint.
Only defined in vm charm.
Expand All @@ -131,6 +131,11 @@ def is_externally_accessible(self, *, event) -> typing.Optional[bool]:
Only defined in vm charm to return True/False. In k8s charm, returns None.
"""

@property
@abc.abstractmethod
def _status(self) -> ops.StatusBase:
"""Status of the charm."""

@property
def _tls_certificate_saved(self) -> bool:
"""Whether a TLS certificate is available to use"""
Expand Down Expand Up @@ -208,6 +213,8 @@ def _determine_app_status(self, *, event) -> ops.StatusBase:
# (Relations should not be modified during upgrade.)
return upgrade_status
statuses = []
if self._status:
statuses.append(self._status)
for endpoint in (self._database_requires, self._database_provides):
if status := endpoint.get_status(event):
statuses.append(status)
Expand Down Expand Up @@ -248,17 +255,17 @@ def _reconcile_service(self) -> None:
Only applies to Kubernetes charm
"""

@abc.abstractmethod
def _wait_until_service_reconciled(self) -> None:
"""Waits until the service is reconciled (connectable with a socket)"""

@abc.abstractmethod
def _reconcile_ports(self, *, event) -> None:
"""Reconcile exposed ports.
Only applies to Machine charm
"""

@abc.abstractmethod
def _update_endpoints(self) -> None:
"""Update the endpoints in the provider relation if necessary."""

# =======================
# Handlers
# =======================
Expand Down Expand Up @@ -345,12 +352,13 @@ def reconcile(self, event=None) -> None: # noqa: C901
self._reconcile_service()
self._database_provides.reconcile_users(
event=event,
router_read_write_endpoint=self._read_write_endpoint,
router_read_only_endpoint=self._read_only_endpoint,
exposed_read_write_endpoint=self._exposed_read_write_endpoint,
exposed_read_only_endpoint=self._exposed_read_only_endpoint,
router_read_write_endpoints=self._read_write_endpoints,
router_read_only_endpoints=self._read_only_endpoints,
exposed_read_write_endpoints=self._exposed_read_write_endpoints,
exposed_read_only_endpoints=self._exposed_read_only_endpoints,
shell=workload_.shell,
)
self._update_endpoints()

if workload_.container_ready:
workload_.reconcile(
Expand All @@ -367,27 +375,6 @@ def reconcile(self, event=None) -> None: # noqa: C901
):
self._reconcile_ports(event=event)

if (
self._unit_lifecycle.authorized_leader
and not self._upgrade.in_progress
and isinstance(workload_, workload.AuthenticatedWorkload)
and workload_.container_ready
):
# _ha_cluster only assigned a value in machine charms
if self._ha_cluster:
self._database_provides.update_endpoints(
router_read_write_endpoint=self._read_write_endpoint,
router_read_only_endpoint=self._read_only_endpoint,
exposed_read_write_endpoint=self._exposed_read_write_endpoint,
exposed_read_only_endpoint=self._exposed_read_only_endpoint,
)
else:
self._wait_until_service_reconciled()
self._database_provides.update_endpoints(
router_read_write_endpoint=self._read_write_endpoint,
router_read_only_endpoint=self._read_only_endpoint,
)

# Empty waiting status means we're waiting for database requires relation before
# starting workload
if not workload_.status or workload_.status == ops.WaitingStatus():
Expand Down
20 changes: 16 additions & 4 deletions src/machine_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def _upgrade(self) -> typing.Optional[machine_upgrade.Upgrade]:
except upgrade.PeerRelationNotReady:
pass

@property
def _status(self) -> ops.StatusBase:
pass

@property
def _logrotate(self) -> machine_logrotate.LogRotate:
return machine_logrotate.LogRotate(container_=self._container)
Expand All @@ -95,19 +99,19 @@ def host_address(self) -> str:
return str(self.model.get_binding("juju-info").network.bind_address)

@property
def _read_write_endpoint(self) -> str:
def _read_write_endpoints(self) -> str:
return f'file://{self._container.path("/run/mysqlrouter/mysql.sock")}'

@property
def _read_only_endpoint(self) -> str:
def _read_only_endpoints(self) -> str:
return f'file://{self._container.path("/run/mysqlrouter/mysqlro.sock")}'

@property
def _exposed_read_write_endpoint(self) -> typing.Optional[str]:
def _exposed_read_write_endpoints(self) -> typing.Optional[str]:
return f"{self.host_address}:{self._READ_WRITE_PORT}"

@property
def _exposed_read_only_endpoint(self) -> typing.Optional[str]:
def _exposed_read_only_endpoints(self) -> typing.Optional[str]:
return f"{self.host_address}:{self._READ_ONLY_PORT}"

def is_externally_accessible(self, *, event) -> typing.Optional[bool]:
Expand All @@ -124,6 +128,14 @@ def _reconcile_ports(self, *, event) -> None:
ports = []
self.unit.set_ports(*ports)

def _update_endpoints(self) -> None:
self._database_provides.update_endpoints(
router_read_write_endpoints=self._read_write_endpoints,
router_read_only_endpoints=self._read_only_endpoints,
exposed_read_write_endpoints=self._exposed_read_write_endpoints,
exposed_read_only_endpoints=self._exposed_read_only_endpoints,
)

def _wait_until_service_reconciled(self) -> None:
"""Only applies to Kubernetes charm, so no-op."""
pass
Expand Down
32 changes: 16 additions & 16 deletions src/relations/database_providers_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ def external_connectivity(self, event) -> bool:
def update_endpoints(
self,
*,
router_read_write_endpoint: str,
router_read_only_endpoint: str,
exposed_read_write_endpoint: str,
exposed_read_only_endpoint: str,
router_read_write_endpoints: str,
router_read_only_endpoints: str,
exposed_read_write_endpoints: str,
exposed_read_only_endpoints: str,
) -> None:
"""Update the endpoints in the provides relationship databags."""
self._database_provides.update_endpoints(
router_read_write_endpoint=router_read_write_endpoint,
router_read_only_endpoint=router_read_only_endpoint,
exposed_read_write_endpoint=exposed_read_write_endpoint,
exposed_read_only_endpoint=exposed_read_only_endpoint,
router_read_write_endpoints=router_read_write_endpoints,
router_read_only_endpoints=router_read_only_endpoints,
exposed_read_write_endpoints=exposed_read_write_endpoints,
exposed_read_only_endpoints=exposed_read_only_endpoints,
)

def reconcile_users(
self,
*,
event,
router_read_write_endpoint: str,
router_read_only_endpoint: str,
exposed_read_write_endpoint: str,
exposed_read_only_endpoint: str,
router_read_write_endpoints: str,
router_read_only_endpoints: str,
exposed_read_write_endpoints: str,
exposed_read_only_endpoints: str,
shell: mysql_shell.Shell,
) -> None:
"""Create requested users and delete inactive users.
Expand All @@ -76,10 +76,10 @@ def reconcile_users(
"""
self._database_provides.reconcile_users(
event=event,
router_read_write_endpoint=router_read_write_endpoint,
router_read_only_endpoint=router_read_only_endpoint,
exposed_read_write_endpoint=exposed_read_write_endpoint,
exposed_read_only_endpoint=exposed_read_only_endpoint,
router_read_write_endpoints=router_read_write_endpoints,
router_read_only_endpoints=router_read_only_endpoints,
exposed_read_write_endpoints=exposed_read_write_endpoints,
exposed_read_only_endpoints=exposed_read_only_endpoints,
shell=shell,
)
self._deprecated_shared_db.reconcile_users(event=event, shell=shell)
Expand Down
68 changes: 34 additions & 34 deletions src/relations/database_provides.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def _set_databag(
def create_database_and_user(
self,
*,
router_read_write_endpoint: str,
router_read_only_endpoint: str,
exposed_read_write_endpoint: str,
exposed_read_only_endpoint: str,
router_read_write_endpoints: str,
router_read_only_endpoints: str,
exposed_read_write_endpoints: str,
exposed_read_only_endpoints: str,
shell: mysql_shell.Shell,
) -> None:
"""Create database & user and update databag."""
Expand All @@ -128,12 +128,12 @@ def create_database_and_user(
)

rw_endpoint = (
exposed_read_write_endpoint
exposed_read_write_endpoints
if self.external_connectivity
else router_read_write_endpoint
else router_read_write_endpoints
)
ro_endpoint = (
exposed_read_only_endpoint if self.external_connectivity else router_read_only_endpoint
exposed_read_only_endpoints if self.external_connectivity else router_read_only_endpoints
)

self._set_databag(
Expand Down Expand Up @@ -164,28 +164,28 @@ def __init__(
def update_endpoints(
self,
*,
router_read_write_endpoint: str,
router_read_only_endpoint: str,
exposed_read_write_endpoint: str,
exposed_read_only_endpoint: str,
router_read_write_endpoints: str,
router_read_only_endpoints: str,
exposed_read_write_endpoints: str,
exposed_read_only_endpoints: str,
) -> None:
"""Update the endpoints in the databag."""
logger.debug(
f"Updating endpoints {self._id} {router_read_write_endpoint=}, {router_read_only_endpoint=} {exposed_read_write_endpoint=} {exposed_read_only_endpoint=}"
f"Updating endpoints {self._id} {router_read_write_endpoints=}, {router_read_only_endpoints=} {exposed_read_write_endpoints=} {exposed_read_only_endpoints=}"
)
rw_endpoint = (
exposed_read_write_endpoint
exposed_read_write_endpoints
if self.external_connectivity
else router_read_write_endpoint
else router_read_write_endpoints
)
ro_endpoint = (
exposed_read_only_endpoint if self.external_connectivity else router_read_only_endpoint
exposed_read_only_endpoints if self.external_connectivity else router_read_only_endpoints
)

self._interface.set_endpoints(self._id, rw_endpoint)
self._interface.set_read_only_endpoints(self._id, ro_endpoint)
logger.debug(
f"Updated endpoints {self._id} {router_read_write_endpoint=}, {router_read_only_endpoint=} {exposed_read_write_endpoint=} {exposed_read_only_endpoint=}"
f"Updated endpoints {self._id} {router_read_write_endpoints=}, {router_read_only_endpoints=} {exposed_read_write_endpoints=} {exposed_read_only_endpoints=}"
)

def delete_databag(self) -> None:
Expand Down Expand Up @@ -249,28 +249,28 @@ def external_connectivity(self, event) -> bool:
def update_endpoints(
self,
*,
router_read_write_endpoint: str,
router_read_only_endpoint: str,
exposed_read_write_endpoint: str,
exposed_read_only_endpoint: str,
router_read_write_endpoints: str,
router_read_only_endpoints: str,
exposed_read_write_endpoints: str,
exposed_read_only_endpoints: str,
) -> None:
"""Update endpoints in the databags."""
for relation in self._shared_users:
relation.update_endpoints(
router_read_write_endpoint=router_read_write_endpoint,
router_read_only_endpoint=router_read_only_endpoint,
exposed_read_write_endpoint=exposed_read_write_endpoint,
exposed_read_only_endpoint=exposed_read_only_endpoint,
router_read_write_endpoints=router_read_write_endpoints,
router_read_only_endpoints=router_read_only_endpoints,
exposed_read_write_endpoints=exposed_read_write_endpoints,
exposed_read_only_endpoints=exposed_read_only_endpoints,
)

def reconcile_users(
self,
*,
event,
router_read_write_endpoint: str,
router_read_only_endpoint: str,
exposed_read_write_endpoint: str,
exposed_read_only_endpoint: str,
router_read_write_endpoints: str,
router_read_only_endpoints: str,
exposed_read_write_endpoints: str,
exposed_read_only_endpoints: str,
shell: mysql_shell.Shell,
) -> None:
"""Create requested users and delete inactive users.
Expand All @@ -280,7 +280,7 @@ def reconcile_users(
relation is broken.
"""
logger.debug(
f"Reconciling users {event=}, {router_read_write_endpoint=}, {router_read_only_endpoint=}"
f"Reconciling users {event=}, {router_read_write_endpoints=}, {router_read_only_endpoints=}"
)
requested_users = []
for relation in self._interface.relations:
Expand All @@ -300,17 +300,17 @@ def reconcile_users(
for relation in requested_users:
if relation not in self._shared_users:
relation.create_database_and_user(
router_read_write_endpoint=router_read_write_endpoint,
router_read_only_endpoint=router_read_only_endpoint,
exposed_read_write_endpoint=exposed_read_write_endpoint,
exposed_read_only_endpoint=exposed_read_only_endpoint,
router_read_write_endpoints=router_read_write_endpoints,
router_read_only_endpoints=router_read_only_endpoints,
exposed_read_write_endpoints=exposed_read_write_endpoints,
exposed_read_only_endpoints=exposed_read_only_endpoints,
shell=shell,
)
for relation in self._shared_users:
if relation not in requested_users:
relation.delete_user(shell=shell)
logger.debug(
f"Reconciled users {event=}, {router_read_write_endpoint=}, {router_read_only_endpoint=}"
f"Reconciled users {event=}, {router_read_write_endpoints=}, {router_read_only_endpoints=}"
)

def delete_all_databags(self) -> None:
Expand Down

0 comments on commit ddb6349

Please sign in to comment.