Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hostname mapping support #142

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 34
LIBPATCH = 35

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -2277,7 +2277,7 @@ def __init__(


################################################################################
# Cross-charm Relatoins Data Handling and Evenets
# Cross-charm Relations Data Handling and Events
################################################################################

# Generic events
Expand All @@ -2300,7 +2300,7 @@ class RelationEventWithSecret(RelationEvent):

@property
def _secrets(self) -> dict:
"""Caching secrets to avoid fetching them each time a field is referrd.
"""Caching secrets to avoid fetching them each time a field is referred.

DON'T USE the encapsulated helper variable outside of this function
"""
Expand All @@ -2309,7 +2309,7 @@ def _secrets(self) -> dict:
return self._cached_secrets

def _get_secret(self, group) -> Optional[Dict[str, str]]:
"""Retrieveing secrets."""
"""Retrieving secrets."""
if not self.app:
return
if not self._secrets.get(group):
Expand Down Expand Up @@ -2498,6 +2498,17 @@ def version(self) -> Optional[str]:

return self.relation.data[self.relation.app].get("version")

@property
def hostname_mapping(self) -> Optional[str]:
"""Returns the hostname mapping.

A list that maps the hostnames to IP address.
"""
if not self.relation.app:
return None

return self.relation.data[self.relation.app].get("hostname-mapping")


class DatabaseCreatedEvent(AuthenticationEvent, DatabaseRequiresEvent):
"""Event emitted when a new database is created for use on this relation."""
Expand Down Expand Up @@ -2602,6 +2613,15 @@ def set_version(self, relation_id: int, version: str) -> None:
"""
self.update_relation_data(relation_id, {"version": version})

def set_hostname_mapping(self, relation_id: int, hostname_mapping: list[dict]) -> None:
"""Set hostname mapping.

Args:
relation_id: the identifier for a particular relation.
hostname_mapping: list of hostname mapping.
"""
self.update_relation_data(relation_id, {"hostname-mapping": json.dumps(hostname_mapping)})


class DatabaseProviderEventHandlers(EventHandlers):
"""Provider-side of the database relation handlers."""
Expand Down Expand Up @@ -3016,7 +3036,7 @@ class KafkaRequiresEvents(CharmEvents):
# Kafka Provides and Requires


class KafkaProvidesData(ProviderData):
class KafkaProviderData(ProviderData):
"""Provider-side of the Kafka relation."""

def __init__(self, model: Model, relation_name: str) -> None:
Expand Down Expand Up @@ -3059,12 +3079,12 @@ def set_zookeeper_uris(self, relation_id: int, zookeeper_uris: str) -> None:
self.update_relation_data(relation_id, {"zookeeper-uris": zookeeper_uris})


class KafkaProvidesEventHandlers(EventHandlers):
class KafkaProviderEventHandlers(EventHandlers):
"""Provider-side of the Kafka relation."""

on = KafkaProvidesEvents() # pyright: ignore [reportAssignmentType]

def __init__(self, charm: CharmBase, relation_data: KafkaProvidesData) -> None:
def __init__(self, charm: CharmBase, relation_data: KafkaProviderData) -> None:
super().__init__(charm, relation_data)
# Just to keep lint quiet, can't resolve inheritance. The same happened in super().__init__() above
self.relation_data = relation_data
Expand All @@ -3086,15 +3106,15 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
)


class KafkaProvides(KafkaProvidesData, KafkaProvidesEventHandlers):
class KafkaProvides(KafkaProviderData, KafkaProviderEventHandlers):
"""Provider-side of the Kafka relation."""

def __init__(self, charm: CharmBase, relation_name: str) -> None:
KafkaProvidesData.__init__(self, charm.model, relation_name)
KafkaProvidesEventHandlers.__init__(self, charm, self)
KafkaProviderData.__init__(self, charm.model, relation_name)
KafkaProviderEventHandlers.__init__(self, charm, self)


class KafkaRequiresData(RequirerData):
class KafkaRequirerData(RequirerData):
"""Requirer-side of the Kafka relation."""

def __init__(
Expand Down Expand Up @@ -3124,12 +3144,12 @@ def topic(self, value):
self._topic = value


class KafkaRequiresEventHandlers(RequirerEventHandlers):
class KafkaRequirerEventHandlers(RequirerEventHandlers):
"""Requires-side of the Kafka relation."""

on = KafkaRequiresEvents() # pyright: ignore [reportAssignmentType]

def __init__(self, charm: CharmBase, relation_data: KafkaRequiresData) -> None:
def __init__(self, charm: CharmBase, relation_data: KafkaRequirerData) -> None:
super().__init__(charm, relation_data)
# Just to keep lint quiet, can't resolve inheritance. The same happened in super().__init__() above
self.relation_data = relation_data
Expand Down Expand Up @@ -3188,7 +3208,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
return


class KafkaRequires(KafkaRequiresData, KafkaRequiresEventHandlers):
class KafkaRequires(KafkaRequirerData, KafkaRequirerEventHandlers):
"""Provider-side of the Kafka relation."""

def __init__(
Expand All @@ -3200,7 +3220,7 @@ def __init__(
consumer_group_prefix: Optional[str] = None,
additional_secret_fields: Optional[List[str]] = [],
) -> None:
KafkaRequiresData.__init__(
KafkaRequirerData.__init__(
self,
charm.model,
relation_name,
Expand All @@ -3209,7 +3229,7 @@ def __init__(
consumer_group_prefix,
additional_secret_fields,
)
KafkaRequiresEventHandlers.__init__(self, charm, self)
KafkaRequirerEventHandlers.__init__(self, charm, self)


# Opensearch related events
Expand Down
33 changes: 12 additions & 21 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tenacity = "^8.2.3"
poetry-core = "^1.7.0"
jinja2 = "^3.1.2"
requests = "^2.31.0"
python-hosts = "*"

[tool.poetry.group.charm-libs.dependencies]
# data_platform_libs/v0/data_interfaces.py
Expand Down
8 changes: 8 additions & 0 deletions src/abstract_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def _exposed_read_write_endpoint(self) -> str:
def _exposed_read_only_endpoint(self) -> str:
"""The exposed read-only endpoint"""

@property
@abc.abstractmethod
def _hostname_mapping(self):
"""Machine hostname mapping"""

@abc.abstractmethod
def is_externally_accessible(self, *, event) -> typing.Optional[bool]:
"""Whether endpoints should be externally accessible.
Expand Down Expand Up @@ -327,6 +332,9 @@ def reconcile(self, event=None) -> None: # noqa: C901
):
self._reconcile_ports(event=event)

if connection_info := self._database_requires.get_connection_info(event=event):
self._hostname_mapping.update_etc_hosts(connection_info)

# 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
5 changes: 5 additions & 0 deletions src/machine_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tenacity

import abstract_charm
import machine_hostname_mapping
import machine_logrotate
import machine_upgrade
import machine_workload
Expand Down Expand Up @@ -85,6 +86,10 @@ def _exposed_read_write_endpoint(self) -> str:
def _exposed_read_only_endpoint(self) -> str:
return f"{self.host_address}:{self._READ_ONLY_PORT}"

@property
def _hostname_mapping(self) -> machine_hostname_mapping.MachineHostnameMapping:
return machine_hostname_mapping.MachineHostnameMapping()

def is_externally_accessible(self, *, event) -> typing.Optional[bool]:
return self._database_provides.external_connectivity(event)

Expand Down
41 changes: 41 additions & 0 deletions src/machine_hostname_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

"""hostname mapping configuration"""

import logging

from python_hosts import Hosts, HostsEntry

from relations.database_requires import CompleteConnectionInformation

logger = logging.getLogger(__name__)

COMMENT = "Managed by mysql-router charm"


class MachineHostnameMapping:
"""Machine hostname mapping configuration"""

def __init__(self) -> None:
self._hosts = Hosts()

def update_etc_hosts(self, connection_info: CompleteConnectionInformation) -> None:
"""Add a host to the hosts file.

Args:
connection_info: The relation CompleteConnectionInformation
"""
if connection_info.hostname_mapping is None:
logger.debug("No hostname mapping to update")
return

logger.debug("Updating /etc/hosts")
host_entries = [
HostsEntry(entry_type="ipv4", comment=COMMENT, **entry)
for entry in connection_info.hostname_mapping
]

self._hosts.remove_all_matching(comment=COMMENT)
self._hosts.add(host_entries, force=True, allow_address_duplication=True, merge_names=True)
self._hosts.write()
2 changes: 2 additions & 0 deletions src/relations/database_requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Relation to MySQL charm"""

import json
import logging
import typing

Expand Down Expand Up @@ -87,6 +88,7 @@ def __init__(self, *, interface: data_interfaces.DatabaseRequires, event) -> Non
self.port = endpoint.split(":")[1]
self.username = databag["username"]
self.password = databag["password"]
self.hostname_mapping = json.loads(databag["hostname-mapping"])

@property
def redacted(self):
Expand Down
Loading