mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
feat(resources): let cpus name a percentage of the host
A container cap had to be an absolute number, and docker refuses one above
the host's core count, so "use this machine" could only be written as a
number that breaks on a smaller machine. cpus now also takes a percentage,
read against the host's raw count: 100% is every CPU and lands exactly on the
ceiling docker accepts, 50% is half of it.
The share is read against that raw count rather than against the fair-share
default, because the two CPUs the resource lookup reserves exist to bound
what a service gets when it declares nothing. A service writing a percentage
overrides that default on purpose, and subtracting the reserve a second time
would count it twice and leave no way to name the whole machine.
A bare cpus: 0 is refused from here on. Docker reads a zero as uncapped
rather than as none, so the value most likely to be typed by mistake was also
the one that removed the limit; the error names the fix. No role declared
one: all 315 cpus values in the tree are plain numbers, the smallest 0.01.
Two lookups carry the resolution so that no caller repeats it or has to know
what a percentage is read against. lookup('cpus', app, service) replaces six
copies of lookup('config', ...) piped through the resolver; those six size
worker pools and thread counts from their own cpus, and left alone a
percentage would have given libretranslate a single thread and handed the
agent broker an uncapped container. lookup('webserver_resource', key)
replaces the two blocks that took the smaller of the proxy's value and the
app's, which were the same eight lines written twice for cpus and for
pids_limit.
resolve_cpus is no longer registered as a jinja filter. Nothing referenced it
from a template once the lookups existed, and keeping it would have left two
public ways to resolve one value.
Measured: 96 tests across the filter, both lookups and the container render
templates, including that a percentage resolves before the smaller of the two
is chosen, and that pids_limit stays an integer because nginx renders it
straight into worker_connections.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c76a096fa4
commit
f7b6f0d84e
18 files changed
+436
-27
No files matched your search
@@ -117,6 +117,64 @@ class TestResourceFilter(unittest.TestCase):
|
||||
|
||||
self.assertEqual(result, 2048)
|
||||
|
||||
def test_a_percentage_resolves_against_the_host(self):
|
||||
"""cpus: 100% in services.yml is every CPU the host has."""
|
||||
self.mock_get.return_value = "100%"
|
||||
|
||||
result = plugin_module.resource_filter(
|
||||
self.applications,
|
||||
self.application_id,
|
||||
self.key,
|
||||
service_name="openresty",
|
||||
hard_default="0.5",
|
||||
host_cpus=20,
|
||||
)
|
||||
|
||||
self.assertEqual(result, 20.0)
|
||||
|
||||
def test_a_percentage_in_the_hard_default_resolves_too(self):
|
||||
"""The share applies to the fallback, not only to an explicit key."""
|
||||
self.mock_get.return_value = plugin_module._UNSET
|
||||
|
||||
result = plugin_module.resource_filter(
|
||||
self.applications,
|
||||
self.application_id,
|
||||
self.key,
|
||||
service_name="openresty",
|
||||
hard_default="25%",
|
||||
host_cpus=20,
|
||||
)
|
||||
|
||||
self.assertEqual(result, 5.0)
|
||||
|
||||
def test_a_plain_value_is_left_alone(self):
|
||||
self.mock_get.return_value = "2"
|
||||
|
||||
result = plugin_module.resource_filter(
|
||||
self.applications,
|
||||
self.application_id,
|
||||
self.key,
|
||||
service_name="openresty",
|
||||
hard_default="0.5",
|
||||
host_cpus=20,
|
||||
)
|
||||
|
||||
self.assertEqual(result, "2")
|
||||
|
||||
def test_other_keys_keep_a_zero(self):
|
||||
"""Keys other than cpus pass no host_cpus and must keep their zero."""
|
||||
self.mock_get.return_value = 0
|
||||
|
||||
result = plugin_module.resource_filter(
|
||||
self.applications,
|
||||
self.application_id,
|
||||
key="pids_limit",
|
||||
service_name="openresty",
|
||||
hard_default=2048,
|
||||
)
|
||||
|
||||
self.assertEqual(result, 0)
|
||||
|
||||
def test_raises_ansible_filter_error_on_config_errors(self):
|
||||
"""Underlying config errors must be wrapped as AnsibleFilterError."""
|
||||
self.mock_get.side_effect = plugin_module.AppConfigKeyError("bad path")
|
||||
@@ -131,5 +189,55 @@ class TestResourceFilter(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class TestResolveCpus(unittest.TestCase):
|
||||
def test_a_share_of_the_host(self):
|
||||
for value, expected in (
|
||||
("100%", 20.0),
|
||||
("50%", 10.0),
|
||||
("25%", 5.0),
|
||||
(" 10% ", 2.0),
|
||||
("2.5%", 0.5),
|
||||
):
|
||||
with self.subTest(value=value):
|
||||
self.assertEqual(plugin_module.resolve_cpus(value, 20), expected)
|
||||
|
||||
def test_a_full_share_lands_on_the_docker_ceiling(self):
|
||||
"""100% must equal the host count exactly, which is docker's maximum."""
|
||||
self.assertEqual(plugin_module.resolve_cpus("100%", 20), 20.0)
|
||||
|
||||
def test_the_host_count_may_arrive_as_a_string(self):
|
||||
"""RESOURCE_HOST_CPUS comes through a lookup and renders as text."""
|
||||
self.assertEqual(plugin_module.resolve_cpus("50%", "20"), 10.0)
|
||||
|
||||
def test_a_share_is_rounded_to_dockers_granularity(self):
|
||||
self.assertEqual(plugin_module.resolve_cpus("33%", 20), 6.6)
|
||||
self.assertEqual(plugin_module.resolve_cpus("1%", 3), 0.03)
|
||||
|
||||
def test_a_share_never_falls_below_the_smallest_docker_takes(self):
|
||||
self.assertEqual(plugin_module.resolve_cpus("0%", 20), 0.01)
|
||||
|
||||
def test_a_plain_value_is_returned_unchanged(self):
|
||||
for value in (2, 0.5, "1.5"):
|
||||
with self.subTest(value=value):
|
||||
self.assertEqual(plugin_module.resolve_cpus(value, 20), value)
|
||||
|
||||
def test_a_bare_zero_is_refused_as_uncapped(self):
|
||||
"""docker reads cpus 0 as unlimited, so it must not pass silently."""
|
||||
with self.assertRaises(plugin_module.AnsibleFilterError):
|
||||
plugin_module.resolve_cpus(0, 20)
|
||||
|
||||
def test_more_than_the_host_has_is_refused(self):
|
||||
with self.assertRaises(plugin_module.AnsibleFilterError):
|
||||
plugin_module.resolve_cpus("120%", 20)
|
||||
|
||||
def test_an_unreadable_percentage_is_refused(self):
|
||||
with self.assertRaises(plugin_module.AnsibleFilterError):
|
||||
plugin_module.resolve_cpus("half%", 20)
|
||||
|
||||
def test_false_is_not_a_zero(self):
|
||||
"""bool is an int in Python, and False here would be a config error."""
|
||||
self.assertIs(plugin_module.resolve_cpus(False, 20), False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,50 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.cpus import LookupModule
|
||||
|
||||
VARS = {"application_id": "web-app-docs"}
|
||||
|
||||
|
||||
def _run(terms, configured, host_cpus=20):
|
||||
with (
|
||||
patch("plugins.lookup.cpus.ConfigLookup") as config,
|
||||
patch("plugins.lookup.cpus.ResourceLookup") as resource,
|
||||
):
|
||||
config.return_value.run.return_value = [configured]
|
||||
resource.return_value.run.return_value = [host_cpus]
|
||||
result = LookupModule().run(terms, variables=VARS)
|
||||
return result, config.return_value.run.call_args
|
||||
|
||||
|
||||
class TestCpusLookup(unittest.TestCase):
|
||||
def test_a_percentage_becomes_a_share_of_the_host(self):
|
||||
result, _ = _run(["web-app-docs", "docs"], "95%")
|
||||
self.assertEqual(result, [19.0])
|
||||
|
||||
def test_a_plain_value_passes_through(self):
|
||||
result, _ = _run(["web-app-docs", "docs"], "2")
|
||||
self.assertEqual(result, ["2"])
|
||||
|
||||
def test_the_service_name_builds_the_config_path(self):
|
||||
_, call = _run(["web-app-hermes", "hermes"], "50%")
|
||||
self.assertEqual(call.args[0], ["web-app-hermes", "services.hermes.cpus"])
|
||||
|
||||
@patch("plugins.lookup.cpus.get_entity_name", return_value="docs")
|
||||
def test_the_entity_name_is_the_default_service(self, _entity):
|
||||
_, call = _run(["web-app-docs"], "50%")
|
||||
self.assertEqual(call.args[0], ["web-app-docs", "services.docs.cpus"])
|
||||
|
||||
def test_no_terms_is_refused(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run([], variables=VARS)
|
||||
|
||||
def test_a_third_term_is_refused(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run(["a", "b", "c"], variables=VARS)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,53 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.webserver_resource import PROXY_ROLE, LookupModule
|
||||
|
||||
RESOURCE = {"cpus": "0.5", "pids_limit": 512, "host_cpus": 20}
|
||||
|
||||
|
||||
def _run(key, proxy, app, application_id="web-app-docs", service_name=""):
|
||||
applications = {
|
||||
PROXY_ROLE: {"services": {"openresty": {key: proxy}}},
|
||||
application_id: {"services": {"docs": {key: app}}},
|
||||
}
|
||||
variables = {"application_id": application_id, "service_name": service_name}
|
||||
with (
|
||||
patch("plugins.lookup.webserver_resource.ApplicationsLookup") as apps,
|
||||
patch("plugins.lookup.webserver_resource.ResourceLookup") as resource,
|
||||
):
|
||||
apps.return_value.run.return_value = [applications]
|
||||
resource.return_value.run.side_effect = lambda terms, **_k: [RESOURCE[terms[0]]]
|
||||
return LookupModule().run([key], variables=variables)
|
||||
|
||||
|
||||
class TestWebserverResource(unittest.TestCase):
|
||||
def test_the_tighter_of_the_two_wins(self):
|
||||
self.assertEqual(_run("cpus", proxy="4", app="2"), [2.0])
|
||||
self.assertEqual(_run("cpus", proxy="1", app="2"), [1.0])
|
||||
|
||||
def test_a_percentage_is_resolved_before_the_comparison(self):
|
||||
"""95% of 20 is 19, so a literal 4 is the tighter of the pair."""
|
||||
self.assertEqual(_run("cpus", proxy="4", app="95%"), [4.0])
|
||||
|
||||
def test_a_percentage_can_be_the_tighter_one(self):
|
||||
self.assertEqual(_run("cpus", proxy="50%", app="16"), [10.0])
|
||||
|
||||
def test_pids_limit_stays_an_integer(self):
|
||||
result = _run("pids_limit", proxy=1024, app=512)
|
||||
self.assertEqual(result, [512])
|
||||
self.assertIsInstance(result[0], int)
|
||||
|
||||
def test_an_unknown_key_is_refused(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run(["mem_limit"], variables={})
|
||||
|
||||
def test_more_than_one_term_is_refused(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run(["cpus", "pids_limit"], variables={})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user