From f98f9f6690deee3413c9d9d11ddd1c8693b71243 Mon Sep 17 00:00:00 2001 From: James Page Date: Tue, 11 Oct 2022 14:28:47 +0100 Subject: [PATCH] Make charm accessible and introspectable By default, expose this charm as a LoadBalancer service to allow external access from outside of K8S which is its principle use case. Add action to allow introspection of the deployed service for the URL to connect to the Southbound DB relay. Change-Id: Ie234ca70d9090be463b5c971a8f01302958c91f5 --- actions.yaml | 4 ++-- src/charm.py | 25 +++++++++++++++++++++++-- tests/actions.yaml | 1 + tests/unit/test_ovn_relay_charm.py | 6 ++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 120000 tests/actions.yaml diff --git a/actions.yaml b/actions.yaml index 88e6195..5756cbf 100644 --- a/actions.yaml +++ b/actions.yaml @@ -1,2 +1,2 @@ -# NOTE: no actions yet! -{ } +get-southbound-db-url: + description: Southbound DB Connection URL for access to OVN via the relay. diff --git a/src/charm.py b/src/charm.py index b362ca7..93a231f 100755 --- a/src/charm.py +++ b/src/charm.py @@ -25,7 +25,9 @@ develop a new k8s charm using the Operator Framework: """ import logging -from typing import List, Mapping + +from ipaddress import IPv4Address, IPv6Address +from typing import List, Mapping, Union from ops.framework import StoredState from ops.main import main @@ -87,8 +89,27 @@ class OVNRelayOperatorCharm(ovn_charm.OSBaseOVNOperatorCharm): self, [ ('southbound', 6642), - ] + ], + service_type="LoadBalancer" ) + self.framework.observe( + self.on.get_southbound_db_url_action, + self._get_southbound_db_url_action + ) + + def _get_southbound_db_url_action(self, event): + event.set_results({"url": self.southbound_db_url}) + + @property + def ingress_address(self) -> Union[IPv4Address, IPv6Address]: + """Network IP address for access to the OVN relay service.""" + return self.model.get_binding( + 'ovsdb-cms-relay').network.ingress_addresses[0] + + @property + def southbound_db_url(self) -> str: + """Full connection URL for Southbound DB relay.""" + return f"ssl:{self.ingress_address}:6442" def get_pebble_handlers(self): pebble_handlers = [ diff --git a/tests/actions.yaml b/tests/actions.yaml new file mode 120000 index 0000000..9adaf92 --- /dev/null +++ b/tests/actions.yaml @@ -0,0 +1 @@ +../actions.yaml \ No newline at end of file diff --git a/tests/unit/test_ovn_relay_charm.py b/tests/unit/test_ovn_relay_charm.py index d310015..c8c7a74 100644 --- a/tests/unit/test_ovn_relay_charm.py +++ b/tests/unit/test_ovn_relay_charm.py @@ -63,3 +63,9 @@ class TestOVNRelayXenaOperatorCharm(test_utils.CharmTestCase): '/root/ovn-relay-wrapper.sh'] for f in ovsdb_config_files: self.check_file('ovsdb-server', f) + + def test_southbound_db_url(self): + self.assertEqual( + 'ssl:10.0.0.10:6442', + self.harness.charm.southbound_db_url + )