canonical / kubernetes-charm-libraries

Charm libraries for interacting with Kubernetes.
https://charmhub.io/kubernetes-charm-libraries
Apache License 2.0
0 stars 0 forks source link

Replace custom events with gets/sets #55

Closed gruyaume closed 1 month ago

gruyaume commented 1 month ago

Overview

As we are now using central hook event handlers, custom events don't make as much sense. Here the enhancement request is to replace the custom events with gets/sets.

Rationale

This simplifies the code quite a bit. Right now we have a complicated series of events where the charm library triggers a custom event that it does not know of. Making the change described here also makes it possible for the lib not to be a child of CharmBase.

Proposal

  1. Replace custom events with configure() and is_configured() calls
  2. Replace func type attributes with list of NAD's and list of Network Annotations
  3. Make the library a root class (instead of having ChildBase as a parent). Pass in everything k8s related (statefulset name, namespace, etc.)

Current

class NadConfigChangedEvent(EventBase):
    """Event triggered when an existing network attachment definition is changed."""

class UpfOperatorCharmEvents(CharmEvents):
    """Kubernetes UPF operator charm events."""

    nad_config_changed = EventSource(NadConfigChangedEvent)

class UPFOperatorCharm(CharmBase):
    """Main class to describe juju event handling for the 5G UPF operator for K8s."""

    on = UpfOperatorCharmEvents()  # type: ignore

    def __init__(self, *args):
        super().__init__(*args)

        self._kubernetes_multus = KubernetesMultusCharmLib(
            charm=self,
            container_name=self._bessd_container_name,
            cap_net_admin=True,
            network_annotations_func=self._generate_network_annotations,
            network_attachment_definitions_func=self._network_attachment_definitions_from_config,
            refresh_event=self.on.nad_config_changed,
        )
        self.framework.observe(self.on.config_changed, self._on_config_changed)

    def _on_config_changed(self, event: EventBase):
        """Handle for config changed events."""
        ...
        self.on.nad_config_changed.emit()

Proposed


class UPFOperatorCharm(CharmBase):
    """Main class to describe juju event handling for the 5G UPF operator for K8s."""

    def __init__(self, *args):
        super().__init__(*args)

        self._kubernetes_multus = KubernetesMultusCharmLib(
            namespace=self.model.name,
            statefulset_name=self.model.app.name,
            container_name=self._bessd_container_name,
            cap_net_admin=True,
            network_annotations=self._generate_network_annotations(),
            network_attachment_definitions=self._network_attachment_definitions_from_config(),
            refresh_event=self.on.nad_config_changed,
        )
        self.framework.observe(self.on.config_changed, self._on_config_changed)

    def _on_config_changed(self, event: EventBase):
        """Handle for config changed events."""
        ...
        if not self._kubernetes_multus.is_configured():
            self._kubernetes_multus.configure()