-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SAP: Introduce external scheduler filter #502
base: stable/xena-m3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2024 SAP SE or an SAP affiliate company. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
import requests | ||
|
||
import nova.conf | ||
from nova.scheduler import filters | ||
from oslo_log import log as logging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
LOG = logging.getLogger(__name__) | ||
CONF = nova.conf.CONF | ||
|
||
|
||
class ExternalSchedulerFilter(filters.BaseHostFilter): | ||
"""Filter using an out of tree scheduler via a REST API.""" | ||
|
||
RUN_ON_REBUILD = False | ||
fwiesel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__(self): | ||
super().__init__() | ||
self.api_url = CONF.filter_scheduler.external_scheduler_api_url | ||
|
||
def filter_all(self, filter_obj_list, spec_obj): | ||
"""Yield objects that pass the filter.""" | ||
if not self.api_url: | ||
LOG.debug( | ||
"external_scheduler_api_url not configured. skipping filter" | ||
) | ||
return filter_obj_list | ||
|
||
hosts_data = [] | ||
for obj in filter_obj_list: | ||
hosts_data.append({ | ||
"name": obj.host, | ||
"status": obj.status, | ||
"vcpus_total": obj.vcpus_total, | ||
"vcpus_used": obj.vcpus_used, | ||
"memory_mb": obj.memory_mb, | ||
"memory_mb_used": obj.memory_mb_used | ||
}) | ||
|
||
spec_data = { | ||
"vcpus": spec_obj.vcpus, | ||
"memory_mb": spec_obj.memory_mb, | ||
} | ||
|
||
json_data = { | ||
"hosts": hosts_data, | ||
"request_spec": spec_data | ||
} | ||
|
||
try: | ||
response = requests.post( | ||
self.api_url, json=json_data, timeout=10 | ||
) | ||
response.raise_for_status() | ||
response_json = response.json() | ||
valid_hosts = response_json.get('valid_hosts', []) | ||
for obj in filter_obj_list: | ||
if obj.host in valid_hosts: | ||
yield obj | ||
|
||
except requests.RequestException as e: | ||
# Log an error if the request fails | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this comment doesn't add anything helpful as the next line starts with |
||
LOG.error("Failed to query the external API: %s", e) | ||
return filter_obj_list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this does nothing. I tried with this code: def a():
try:
for i in range(7):
if i == 3:
raise Exception
yield i
except Exception:
return list(range(7))
breakpoint() $ python3 /tmp/bla.py
--Return--
> /tmp/bla.py(10)<module>()->None
-> breakpoint()
(Pdb) p a()
<generator object a at 0x7e701c04d4d0>
(Pdb) p list(a())
[0, 1, 2] Since a function containing Any return is broken, also the first one allowing to skip the filter: def a():
return ["early"]
try:
for i in range(7):
if i == 3:
raise Exception
yield i
except Exception:
return list(range(7))
breakpoint() $ python3 /tmp/bla.py
--Return--
> /tmp/bla.py(11)<module>()->None
-> breakpoint()
(Pdb) p list(a())
[]
(Pdb) Am I holding it wrong? Shouldn't the test for "the filter returns everything if it's disabled" have found this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you have all the hosts in a list already and are not reading from the request anymore, you might as well return a list instead of yielding, i.e. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2024 SAP SE or an SAP affiliate company. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
# Tests for external scheduler filter. | ||
|
||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
from unittest.mock import sentinel | ||
|
||
import nova.conf | ||
from nova import objects | ||
from nova.scheduler.filters import external_scheduler_filter | ||
from nova import test | ||
from nova.tests.unit.scheduler import fakes | ||
|
||
|
||
CONF = nova.conf.CONF | ||
|
||
|
||
class ExternalSchedulerFilterTestCase(test.NoDBTestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is missing at least 3 tests: |
||
|
||
def setUp(self): | ||
super(test.NoDBTestCase, self).setUp() | ||
CONF.set_override('external_scheduler_api_url', | ||
'http://127.0.0.1:1234', 'filter_scheduler') | ||
|
||
@patch('requests.post') | ||
def test_filter_all(self, mock_post): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = { | ||
'valid_hosts': ['host1', 'host3'] | ||
} | ||
mock_post.return_value = mock_response | ||
|
||
host_attributes = { | ||
"status": "up", | ||
"vcpus_total": 100, | ||
"vcpus_used": 10, | ||
"memory_mb": 1000, | ||
"memory_mb_used": 10, | ||
} | ||
all_hosts = [ | ||
fakes.FakeHostState('host1', 'node1', host_attributes), | ||
fakes.FakeHostState('host2', 'node2', host_attributes), | ||
fakes.FakeHostState('host3', 'node3', host_attributes) | ||
] | ||
instance_request_spec = objects.RequestSpec( | ||
context=sentinel.ctx, | ||
flavor=objects.Flavor( | ||
name="small", | ||
vcpus=4, | ||
memory_mb=1024 | ||
), | ||
) | ||
|
||
f = external_scheduler_filter.ExternalSchedulerFilter() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other test classes, mainly because they run multiple tests, instantiate their filter in |
||
valid_hosts = f.filter_all(all_hosts, instance_request_spec) | ||
self.assertEqual( | ||
['host1', 'host3'], | ||
[h.host for h in valid_hosts] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afaics, this comment doesn't contain any content. What's the intent?