From d65c121ee598e1705b3c30ecf8c58867ae666f1e Mon Sep 17 00:00:00 2001 From: Hemanth Nakkina Date: Tue, 11 Feb 2025 10:46:25 +0530 Subject: [PATCH] [openstack-hypervisor] Add support for sev * Add new config parameter reserved-host-memory-mb-for-sev that updates snap config sev.reserved-host-memory-mb * Add an action list-flavors to list the host flavors/ capabilities Change-Id: I2500d1dafc0bb77dafa8a681daf833f7d1f76211 --- charms/openstack-hypervisor/charmcraft.yaml | 10 ++++++++++ charms/openstack-hypervisor/src/charm.py | 19 +++++++++++++++++++ .../tests/unit/test_charm.py | 15 +++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/charms/openstack-hypervisor/charmcraft.yaml b/charms/openstack-hypervisor/charmcraft.yaml index 3590ca04..3b19c88c 100644 --- a/charms/openstack-hypervisor/charmcraft.yaml +++ b/charms/openstack-hypervisor/charmcraft.yaml @@ -42,6 +42,13 @@ config: use-data-binding: default: false type: boolean + reserved-host-memory-mb-for-sev: + type: string + description: | + Memory to be reserved for host for SEV enabled compute + hosts. This memory will be used for instances. The compute + usage report deducts this memory from the available + memory sent to the placement service. actions: set-hypervisor-local-settings: @@ -81,6 +88,9 @@ actions: Only lists guests created by the OpenStack cloud. additionalProperties: false + list-flavors: + description: | + List the flavors or compute host capabilities. requires: amqp: diff --git a/charms/openstack-hypervisor/src/charm.py b/charms/openstack-hypervisor/src/charm.py index d2b84cc7..d57bc1ef 100755 --- a/charms/openstack-hypervisor/src/charm.py +++ b/charms/openstack-hypervisor/src/charm.py @@ -191,6 +191,10 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): self.on.running_guests_action, self._running_guests_action, ) + self.framework.observe( + self.on.list_flavors_action, + self._list_flavors_action, + ) self.framework.observe( self.on.install, self._on_install, @@ -422,6 +426,18 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): # cli returns a json list event.set_results({"result": stdout}) + def _list_flavors_action(self, event: ActionEvent): + """List compute host capabilities.""" + cache = self.get_snap_cache() + hypervisor = cache["openstack-hypervisor"] + try: + flavors = hypervisor.get("compute.flavors", typed=True) + except snap.SnapError as e: + logger.debug(e) + flavors = "" + + event.set_results({"result": flavors}) + def ensure_services_running(self): """Ensure systemd services running.""" # This should taken care of by the snap @@ -575,6 +591,9 @@ class HypervisorOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): "node.ip-address": config("ip-address") or local_ip, "rabbitmq.url": contexts.amqp.transport_url, "monitoring.enable": self.enable_monitoring, + "sev.reserved-host-memory-mb": config( + "reserved-host-memory-mb-for-sev" + ), } except AttributeError as e: raise sunbeam_guard.WaitingExceptionError( diff --git a/charms/openstack-hypervisor/tests/unit/test_charm.py b/charms/openstack-hypervisor/tests/unit/test_charm.py index ce015f22..5a56f65c 100644 --- a/charms/openstack-hypervisor/tests/unit/test_charm.py +++ b/charms/openstack-hypervisor/tests/unit/test_charm.py @@ -174,6 +174,7 @@ class TestCharm(test_utils.CharmTestCase): "telemetry.enable": False, "ca.bundle": None, "masakari.enable": False, + "sev.reserved-host-memory-mb": None, } hypervisor_snap_mock.set.assert_any_call(expect_settings, typed=True) @@ -287,6 +288,7 @@ class TestCharm(test_utils.CharmTestCase): "telemetry.publisher-secret": "FAKE_SECRET", "ca.bundle": None, "masakari.enable": True, + "sev.reserved-host-memory-mb": None, } hypervisor_snap_mock.set.assert_any_call(expect_settings, typed=True) @@ -346,3 +348,16 @@ class TestCharm(test_utils.CharmTestCase): self.subprocess.run = subprocess_run_mock with self.assertRaises(ops.testing.ActionFailed): self.harness.run_action("list-nics") + + def test_list_flavors(self): + """Check action return flavors.""" + flavors = "flavor1,flavor2" + self.harness.begin() + hypervisor_snap_mock = MagicMock() + hypervisor_snap_mock.present = True + self.snap.SnapCache.return_value = { + "openstack-hypervisor": hypervisor_snap_mock + } + hypervisor_snap_mock.get.return_value = flavors + action_output = self.harness.run_action("list-flavors") + assert action_output.results["result"] == flavors