Skip to content

Commit

Permalink
Removes unconditional setting of active status upon start-up.
Browse files Browse the repository at this point in the history
Instead we handle the collect_unit_status event, using our known state
to supply candidate status values.
  • Loading branch information
manadart committed Nov 29, 2023
1 parent 90dfb37 commit 9cf3fcd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import logging
import secrets
import yaml

from charms.prometheus_k8s.v0.prometheus_scrape import MetricsEndpointProvider
from ops.charm import CharmBase
from ops.charm import CharmBase, CollectStatusEvent
from ops.framework import StoredState
from ops.charm import RelationJoinedEvent, RelationDepartedEvent
from ops.main import main
Expand All @@ -22,14 +23,15 @@ class JujuControllerCharm(CharmBase):
def __init__(self, *args):
super().__init__(*args)

self.framework.observe(self.on.collect_unit_status, self._on_collect_status)
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(self.on.start, self._on_start)
self.framework.observe(
self.on.dashboard_relation_joined, self._on_dashboard_relation_joined)
self.framework.observe(
self.on.website_relation_joined, self._on_website_relation_joined)

self._stored.set_default(db_bind_address='')
self._stored.set_default(last_bind_addresses=[])
self.framework.observe(
self.on.dbcluster_relation_changed, self._on_dbcluster_relation_changed)

Expand All @@ -40,8 +42,16 @@ def __init__(self, *args):
self.framework.observe(
self.on.metrics_endpoint_relation_broken, self._on_metrics_endpoint_relation_broken)

def _on_start(self, _):
self.unit.status = ActiveStatus()
def _on_collect_status(self, event: CollectStatusEvent):
if len(self._stored.last_bind_addresses) > 1:
event.add_status(BlockedStatus(
'multiple possible DB bind addresses; set a suitable dbcluster network binding'))

if self.api_port() is None:
event.add_status(BlockedStatus(
'charm does not appear to be running on a controller node'))

event.add_status(ActiveStatus())

def _on_config_changed(self, _):
controller_url = self.config['controller-url']
Expand All @@ -63,8 +73,7 @@ def _on_website_relation_joined(self, event):
logger.info('got a new website relation: %r', event)
port = self.api_port()
if port is None:
logger.error('machine does not appear to be a controller')
self.unit.status = BlockedStatus('machine does not appear to be a controller')
logger.error('charm does not appear to be running on a controller node')
return

address = None
Expand Down Expand Up @@ -112,11 +121,11 @@ def _on_metrics_endpoint_relation_broken(self, event: RelationDepartedEvent):

def _on_dbcluster_relation_changed(self, event):
ips = self.model.get_binding(event.relation).network.ingress_addresses
self._stored.last_bind_addresses = ips

if len(ips) > 1:
logger.error('multiple possible DB bind addresses; set a dbcluster network binding')
self.unit.status = BlockedStatus(
'multiple possible DB bind addresses; set a dbcluster network binding')
logger.error(
'multiple possible DB bind addresses; set a suitable bcluster network binding')
return

ip = str(ips[0])
Expand Down
12 changes: 9 additions & 3 deletions tests/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import unittest
from charm import JujuControllerCharm
from ops.model import BlockedStatus
from ops.model import BlockedStatus, ActiveStatus
from ops.testing import Harness
from unittest.mock import mock_open, patch

Expand Down Expand Up @@ -88,8 +88,9 @@ def test_metrics_endpoint_relation(self, mock_remove_user, mock_add_user,
harness.remove_relation(relation_id)
mock_remove_user.assert_called_once_with(f'juju-metrics-r{relation_id}')

@patch("builtins.open", new_callable=mock_open, read_data=agent_conf)
@patch("ops.model.Model.get_binding")
def test_dbcluster_relation_changed_single_addr(self, binding):
def test_dbcluster_relation_changed_single_addr(self, binding, _):
harness = self.harness
binding.return_value = mockBinding(["192.168.1.17"])

Expand All @@ -102,8 +103,12 @@ def test_dbcluster_relation_changed_single_addr(self, binding):
data = harness.get_relation_data(relation_id, 'juju-controller/0')
self.assertEqual(data["db-bind-address"], "192.168.1.17")

harness.evaluate_status()
self.assertIsInstance(harness.charm.unit.status, ActiveStatus)

@patch("builtins.open", new_callable=mock_open, read_data=agent_conf)
@patch("ops.model.Model.get_binding")
def test_dbcluster_relation_changed_multi_addr_error(self, binding):
def test_dbcluster_relation_changed_multi_addr_error(self, binding, _):
harness = self.harness
binding.return_value = mockBinding(["192.168.1.17", "192.168.1.18"])

Expand All @@ -113,6 +118,7 @@ def test_dbcluster_relation_changed_multi_addr_error(self, binding):
harness.charm._on_dbcluster_relation_changed(
harness.charm.model.get_relation('dbcluster').data[harness.charm.unit])

harness.evaluate_status()
self.assertIsInstance(harness.charm.unit.status, BlockedStatus)


Expand Down

0 comments on commit 9cf3fcd

Please sign in to comment.