From 1a63fa2fdff2bf7044992754e9034a2e6df3ee80 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Tue, 25 Apr 2023 16:55:11 +0000 Subject: [PATCH] Add action for applying local settings Add action to apply snap setting specific to the local unit. Change-Id: I6ab63de753046c07934a6f923206ff5179599e39 --- charms/openstack-hypervisor/actions.yaml | 15 ++++++++++ charms/openstack-hypervisor/config.yaml | 3 -- charms/openstack-hypervisor/src/charm.py | 29 +++++++++++++++---- .../openstack-hypervisor/tests/actions.yaml | 1 + .../tests/unit/test_charm.py | 5 ++-- 5 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 charms/openstack-hypervisor/actions.yaml create mode 120000 charms/openstack-hypervisor/tests/actions.yaml diff --git a/charms/openstack-hypervisor/actions.yaml b/charms/openstack-hypervisor/actions.yaml new file mode 100644 index 00000000..98ea1af0 --- /dev/null +++ b/charms/openstack-hypervisor/actions.yaml @@ -0,0 +1,15 @@ +set-hypervisor-local-settings: + description: | + Apply settings specific to this hypervisor unit + params: + external-nic: + type: string + description: NIC that hypervisor will configure for North/South traffic + spice-proxy-address: + type: string + description: IP address to use for configuration of SPICE consoles in instances. + ip-address: + type: string + description: IP address to use for service configuration + additionalProperties: false + diff --git a/charms/openstack-hypervisor/config.yaml b/charms/openstack-hypervisor/config.yaml index 61331630..bb9b4512 100644 --- a/charms/openstack-hypervisor/config.yaml +++ b/charms/openstack-hypervisor/config.yaml @@ -20,9 +20,6 @@ options: external-bridge-address: default: "10.20.20.1/24" type: string - ip-address: - default: - type: string physnet-name: default: "physnet1" type: string diff --git a/charms/openstack-hypervisor/src/charm.py b/charms/openstack-hypervisor/src/charm.py index 4f0fc0f5..c6255920 100755 --- a/charms/openstack-hypervisor/src/charm.py +++ b/charms/openstack-hypervisor/src/charm.py @@ -36,6 +36,7 @@ import ops_sunbeam.guard as sunbeam_guard import ops_sunbeam.ovn.relation_handlers as ovn_relation_handlers import ops_sunbeam.relation_handlers as sunbeam_rhandlers from netifaces import AF_INET, gateways, ifaddresses +from ops.charm import ActionEvent from ops.main import main logger = logging.getLogger(__name__) @@ -69,6 +70,10 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): """Run constructor.""" super().__init__(framework) self._state.set_default(metadata_secret="") + self.framework.observe( + self.on.set_hypervisor_local_settings_action, + self._set_hypervisor_local_settings_action, + ) def get_relation_handlers( self, handlers: List[sunbeam_rhandlers.RelationHandler] = None @@ -86,6 +91,21 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): handlers = super().get_relation_handlers(handlers) return handlers + def _set_hypervisor_local_settings_action(self, event: ActionEvent): + """Run set_hypervisor_local_settings action.""" + local_settings = [ + "network.external-nic", + "compute.spice-proxy-address", + "network.ip-address", + ] + new_snap_settings = {} + for setting in local_settings: + action_param = setting.split(".")[1] + if event.params.get(action_param): + new_snap_settings[setting] = event.params.get(action_param) + if new_snap_settings: + self.set_snap_data(new_snap_settings) + def ensure_services_running(self): """Ensure systemd services running.""" # This should taken care of by the snap @@ -121,11 +141,8 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): self._state.metadata_secret = secret return secret - def set_snap_data(self, snap_data): - """Set snap setting if needed. - - Update the snap with any settings that have changed. - """ + def set_snap_data(self, snap_data: dict): + """Set snap data on local snap.""" cache = snap.SnapCache() hypervisor = cache["openstack-hypervisor"] new_settings = {} @@ -195,7 +212,7 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): ).decode(), "network.ovn-sb-connection": sb_connection_strs[0], "network.physnet-name": config("physnet-name"), - "node.fqdn": config("fqdn") or socket.getfqdn(), + "node.fqdn": socket.getfqdn(), "node.ip-address": config("ip-address") or local_ip, "rabbitmq.url": contexts.amqp.transport_url, } diff --git a/charms/openstack-hypervisor/tests/actions.yaml b/charms/openstack-hypervisor/tests/actions.yaml new file mode 120000 index 00000000..9adaf92e --- /dev/null +++ b/charms/openstack-hypervisor/tests/actions.yaml @@ -0,0 +1 @@ +../actions.yaml \ No newline at end of file diff --git a/charms/openstack-hypervisor/tests/unit/test_charm.py b/charms/openstack-hypervisor/tests/unit/test_charm.py index 46732d50..940536bb 100644 --- a/charms/openstack-hypervisor/tests/unit/test_charm.py +++ b/charms/openstack-hypervisor/tests/unit/test_charm.py @@ -33,7 +33,7 @@ class _HypervisorOperatorCharm(charm.HypervisorOperatorCharm): class TestCharm(test_utils.CharmTestCase): - PATCHES = ["socket", "snap"] + PATCHES = ["socket", "snap", "_get_local_ip_by_default_route"] def setUp(self): """Setup OpenStack Hypervisor tests.""" @@ -50,7 +50,7 @@ class TestCharm(test_utils.CharmTestCase): def initial_setup(self): rel_id = self.harness.add_relation("certificates", "vault") self.harness.add_relation_unit(rel_id, "vault/0") - self.harness.update_config({"snap-channel": "essex/stable", "ip-address": "10.0.0.10"}) + self.harness.update_config({"snap-channel": "essex/stable"}) self.harness.begin_with_initial_hooks() csr = {"certificate_signing_request": test_utils.TEST_CSR} self.harness.update_relation_data( @@ -79,6 +79,7 @@ class TestCharm(test_utils.CharmTestCase): def test_all_relations(self): """Test all the charms relations.""" + self._get_local_ip_by_default_route.return_value = "10.0.0.10" hypervisor_snap_mock = mock.MagicMock() hypervisor_snap_mock.present = False self.snap.SnapState.Latest = "latest"