Skip to content

Commit

Permalink
Updates status determination so that logic from upstream instead uses
Browse files Browse the repository at this point in the history
thing single method.
  • Loading branch information
manadart committed Jan 8, 2024
1 parent 112dc83 commit 970373c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
42 changes: 23 additions & 19 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, *args):
self.on.dbcluster_relation_changed, self._on_dbcluster_relation_changed)

self.control_socket = controlsocket.Client(
socket_path="/var/lib/juju/control.socket")
socket_path='/var/lib/juju/control.socket')
self.framework.observe(
self.on.metrics_endpoint_relation_created, self._on_metrics_endpoint_relation_created)
self.framework.observe(
Expand All @@ -49,9 +49,11 @@ def _on_collect_status(self, event: CollectStatusEvent):
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'))
try:
self.api_port()
except AgentConfException as e:
event.add_status(ErrorStatus(
f"cannot read controller API port from agent configuration: {e}"))

event.add_status(ActiveStatus())

Expand All @@ -73,9 +75,11 @@ def _on_dashboard_relation_joined(self, event):
def _on_website_relation_joined(self, event):
"""Connect a website relation."""
logger.info('got a new website relation: %r', event)
port = self.api_port()
if port is None:
logger.error('charm does not appear to be running on a controller node')

try:
api_port = self.api_port()
except AgentConfException as e:
logger.error("cannot read controller API port from agent configuration: {}", e)
return

address = None
Expand All @@ -86,7 +90,7 @@ def _on_website_relation_joined(self, event):
event.relation.data[self.unit].update({
'hostname': str(address),
'private-address': str(address),
'port': str(port)
'port': str(api_port)
})

def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent):
Expand All @@ -98,7 +102,7 @@ def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent):
try:
api_port = self.api_port()
except AgentConfException as e:
self.unit.status = ErrorStatus(f"can't read controller API port from agent.conf: {e}")
logger.error("cannot read controller API port from agent configuration: {}", e)
return

metrics_endpoint = MetricsEndpointProvider(
Expand Down Expand Up @@ -143,15 +147,6 @@ def _on_dbcluster_relation_changed(self, event):
self._stored.db_bind_address = ip
event.relation.data[self.unit].update({'db-bind-address': ip})

def _agent_conf(self, key: str):
"""Read a value (by key) from the agent.conf file on disk."""
unit_name = self.unit.name.replace('/', '-')
agent_conf_path = f'/var/lib/juju/agents/unit-{unit_name}/agent.conf'

with open(agent_conf_path) as agent_conf_file:
agent_conf = yaml.safe_load(agent_conf_file)
return agent_conf.get(key)

def api_port(self) -> str:
"""Return the port on which the controller API server is listening."""
api_addresses = self._agent_conf('apiaddresses')
Expand All @@ -162,13 +157,22 @@ def api_port(self) -> str:

parsed_url = urllib.parse.urlsplit('//' + api_addresses[0])
if not parsed_url.port:
raise AgentConfException("api address doesn't include port")
raise AgentConfException("API address does not include port")
return parsed_url.port

def ca_cert(self) -> str:
"""Return the controller's CA certificate."""
return self._agent_conf('cacert')

def _agent_conf(self, key: str):
"""Read a value (by key) from the agent.conf file on disk."""
unit_name = self.unit.name.replace('/', '-')
agent_conf_path = f'/var/lib/juju/agents/unit-{unit_name}/agent.conf'

with open(agent_conf_path) as agent_conf_file:
agent_conf = yaml.safe_load(agent_conf_file)
return agent_conf.get(key)


def metrics_username(relation: Relation) -> str:
"""
Expand Down
8 changes: 3 additions & 5 deletions tests/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import os
import unittest
from charm import JujuControllerCharm, AgentConfException
from ops.model import BlockedStatus, ActiveStatus
from ops import ErrorStatus
from ops.model import BlockedStatus, ActiveStatus, ErrorStatus
from ops.testing import Harness
from unittest.mock import mock_open, patch

Expand Down Expand Up @@ -140,9 +139,8 @@ def test_apiaddresses_missing_status(self, *_):
harness.begin()

harness.add_relation('metrics-endpoint', 'prometheus-k8s')
self.assertEqual(harness.charm.unit.status, ErrorStatus(
"can't read controller API port from agent.conf: agent.conf key 'apiaddresses' missing"
))
harness.evaluate_status()
self.assertIsInstance(harness.charm.unit.status, ErrorStatus)

@patch("builtins.open", new_callable=mock_open, read_data=agent_conf_ipv4)
def test_apiaddresses_ipv4(self, _):
Expand Down

0 comments on commit 970373c

Please sign in to comment.