canonical / cos-configuration-k8s-operator

This charmed operator for Kubernetes enables you to provide configurations to various components of the Canonical Observability Stack (COS) bundle.
https://charmhub.io/cos-configuration-k8s
Apache License 2.0
4 stars 6 forks source link

The `_common_exit_hook` challenge! #12

Open sed-i opened 2 years ago

sed-i commented 2 years ago

Challenge: get rid of the _common_exit_hook pattern, with minimal changes to tests.

Top score: for whoever introduces a repeatable pattern. Negative score: for transforming into explosive spaghetti.

Please link you PR to this issue.

Optional constraints

Feel free to use all/some/none:

Past attempts

Deferring decorators

    @defer_unless(network_ready, WaitingStatus("Waiting for ip address"))
    def _on_alertmanager_pebble_ready(self, event: ops.charm.PebbleReadyEvent):
        # ...

    @defer_unless(network_ready, WaitingStatus("Waiting for ip address"))
    @skip_unless(config_valid, BlockedStatus("PagerDuty service key missing"))
    def _on_config_changed(self, event: ops.charm.ConfigChangedEvent):
        # ...

The underlying assumptions were:

  1. Deferral/skip is decideable via a boolean callable (e.g. network_ready) at event entry
  2. The boolean callables are always of the form check(charm, event)
  3. The @defer_unless/@skip_unless decorator usage can be limited to event hooks only

Rely on update-status

In all event hooks, use defer if a precondition is not met. This means that the charm may end up in stagnation until the next update-status. If the default update-status hook interval of 5min is kept by the user, then relying on update-status is not too bad, but if the user creates a controller or model with a default of, say, 60 min, then it may be too long.