From: Vasek Sraier Date: Sun, 27 Mar 2022 17:14:24 +0000 (+0200) Subject: moved `id` in config to the server section, additional refactoring X-Git-Tag: v6.0.0a1~38^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32118941bd3f14cf5a9ac056624b058a6b8385d6;p=thirdparty%2Fknot-resolver.git moved `id` in config to the server section, additional refactoring --- diff --git a/manager/etc/knot-resolver/config.dev.yml b/manager/etc/knot-resolver/config.dev.yml index 9bba35e4e..2adfd9629 100644 --- a/manager/etc/knot-resolver/config.dev.yml +++ b/manager/etc/knot-resolver/config.dev.yml @@ -1,4 +1,3 @@ -id: dev cache: storage: ../cache logging: @@ -9,6 +8,7 @@ network: listen: - interface: 127.0.0.1@5353 server: + id: dev workers: 1 rundir: etc/knot-resolver/runtime management: diff --git a/manager/knot_resolver_manager/constants.py b/manager/knot_resolver_manager/constants.py index 1f2e7f711..b85cf5b9a 100644 --- a/manager/knot_resolver_manager/constants.py +++ b/manager/knot_resolver_manager/constants.py @@ -73,16 +73,16 @@ class _UserConstants: @property def ID(self) -> str: - return self._config_store.get().id + return str(self._config_store.get().server.id) _user_constants: Optional[_UserConstants] = None async def _deny_id_changes(config_old: KresConfig, config_new: KresConfig) -> Result[None, str]: - if config_old.id != config_new.id: + if config_old.server.id != config_new.server.id: return Result.err( - "/id: Based on the groupid, the manager recognizes subprocesses," + "/id: Based on the ID, the manager recognizes subprocesses," " so it is not possible to change it while services are running." ) return Result.ok(None) diff --git a/manager/knot_resolver_manager/datamodel/config_schema.py b/manager/knot_resolver_manager/datamodel/config_schema.py index 0c02232d8..4341326ef 100644 --- a/manager/knot_resolver_manager/datamodel/config_schema.py +++ b/manager/knot_resolver_manager/datamodel/config_schema.py @@ -1,5 +1,4 @@ import os -import re import sys from typing import Dict, Optional, Union @@ -21,7 +20,6 @@ from knot_resolver_manager.datamodel.server_schema import ServerSchema from knot_resolver_manager.datamodel.static_hints_schema import StaticHintsSchema from knot_resolver_manager.datamodel.stub_zone_schema import StubZoneSchema from knot_resolver_manager.datamodel.types import DomainName -from knot_resolver_manager.datamodel.types.base_types import PatternBase from knot_resolver_manager.datamodel.view_schema import ViewSchema from knot_resolver_manager.utils import SchemaNode @@ -55,17 +53,12 @@ def _import_lua_template() -> Template: _MAIN_TEMPLATE = _import_lua_template() -class IDPattern(PatternBase): - _re = re.compile(r"[a-zA-Z0-9]*") - - class KresConfig(SchemaNode): class Raw(SchemaNode): """ Knot Resolver declarative configuration. --- - id: System-wide unique identifier of this manager instance. Used for grouping logs and tagging kresd processes. server: DNS server control and management configuration. options: Fine-tuning global parameters of DNS resolver operation. network: Network connections and protocols configuration. @@ -83,8 +76,7 @@ class KresConfig(SchemaNode): lua: Custom Lua configuration. """ - id: IDPattern - server: ServerSchema = ServerSchema() + server: ServerSchema options: OptionsSchema = OptionsSchema() network: NetworkSchema = NetworkSchema() static_hints: StaticHintsSchema = StaticHintsSchema() @@ -102,7 +94,6 @@ class KresConfig(SchemaNode): _PREVIOUS_SCHEMA = Raw - id: str server: ServerSchema options: OptionsSchema network: NetworkSchema @@ -141,4 +132,4 @@ class KresConfig(SchemaNode): Funtion used just for testing purposes. Creates an instance of KresConfig without requiring any arguments. """ - return KresConfig({"id": "test"}) + return KresConfig({"server": {"id": "test"}}) diff --git a/manager/knot_resolver_manager/datamodel/server_schema.py b/manager/knot_resolver_manager/datamodel/server_schema.py index 9c58961d5..a77e6bf3f 100644 --- a/manager/knot_resolver_manager/datamodel/server_schema.py +++ b/manager/knot_resolver_manager/datamodel/server_schema.py @@ -14,6 +14,7 @@ from knot_resolver_manager.datamodel.types import ( IPAddressPort, UncheckedPath, ) +from knot_resolver_manager.datamodel.types.types import IDPattern from knot_resolver_manager.exceptions import DataException from knot_resolver_manager.utils import SchemaNode @@ -100,6 +101,7 @@ class ServerSchema(SchemaNode): DNS server control and management configuration. --- + id: System-wide unique identifier of this manager instance. Used for grouping logs and tagging kresd processes. hostname: Internal DNS resolver hostname. Default is machine hostname. nsid: Name Server Identifier (RFC 5001) which allows DNS clients to request resolver to send back its NSID along with the reply to a DNS request. workers: The number of running kresd (Knot Resolver daemon) workers. If set to 'auto', it is equal to number of CPUs available. @@ -111,6 +113,7 @@ class ServerSchema(SchemaNode): webmgmt: Configuration of legacy web management endpoint. """ + id: IDPattern hostname: Optional[str] = None nsid: Optional[str] = None workers: Union[Literal["auto"], IntPositive] = IntPositive(1) @@ -123,6 +126,7 @@ class ServerSchema(SchemaNode): _PREVIOUS_SCHEMA = Raw + id: IDPattern hostname: str nsid: Optional[str] workers: IntPositive diff --git a/manager/knot_resolver_manager/datamodel/types/types.py b/manager/knot_resolver_manager/datamodel/types/types.py index 0a884b5a4..da14dcff1 100644 --- a/manager/knot_resolver_manager/datamodel/types/types.py +++ b/manager/knot_resolver_manager/datamodel/types/types.py @@ -68,6 +68,14 @@ class InterfaceName(PatternBase): _re = re.compile(r"^[a-zA-Z0-9]+(?:[-_][a-zA-Z0-9]+)*$") +class IDPattern(PatternBase): + """ + Alphanumerical ID for identifying systemd slice. + """ + + _re = re.compile(r"[a-zA-Z0-9]+") + + class InterfacePort(StrBase): addr: Union[ipaddress.IPv4Address, ipaddress.IPv6Address] if_name: InterfaceName diff --git a/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py b/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py index 5848fa7d4..a5550accc 100644 --- a/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py +++ b/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py @@ -170,14 +170,14 @@ class SystemdSubprocessController(SubprocessController): async def initialize_controller(self, config: KresConfig) -> None: self._controller_config = config try: - await to_thread(start_slice, self._controller_config, self._systemd_type) + await to_thread(start_slice, self._systemd_type) except SubprocessControllerException as e: logger.warning( f"Failed to create systemd slice for our subprocesses: '{e}'. There is/was a manager running with the same ID." ) async def shutdown_controller(self) -> None: - await to_thread(stop_slice, self._controller_config, self._systemd_type) + await to_thread(stop_slice, self._systemd_type) async def create_subprocess(self, subprocess_config: KresConfig, subprocess_type: SubprocessType) -> Subprocess: assert self._controller_config is not None diff --git a/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py b/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py index b12ed6094..7da54f7c7 100644 --- a/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py +++ b/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py @@ -13,7 +13,13 @@ from pydbus.bus import SessionBus # type: ignore[import] from typing_extensions import Literal from knot_resolver_manager.compat.dataclasses import dataclass -from knot_resolver_manager.constants import kres_gc_executable, kresd_cache_dir, kresd_config_file, kresd_executable +from knot_resolver_manager.constants import ( + kres_gc_executable, + kresd_cache_dir, + kresd_config_file, + kresd_executable, + user_constants, +) from knot_resolver_manager.datamodel.config_schema import KresConfig from knot_resolver_manager.exceptions import SubprocessControllerException, SubprocessControllerTimeoutException from knot_resolver_manager.kresd_controller.interface import KresID, SubprocessType @@ -180,8 +186,8 @@ def restart_unit(type_: SystemdType, unit_name: str) -> None: _wait_for_job_completion(systemd, job) -def _slice_name(config: KresConfig) -> str: - return f"kres-{config.id}.slice" +def _slice_name() -> str: + return f"kres-{user_constants().ID}.slice" def _kresd_unit_properties(config: KresConfig, kres_id: KresID) -> List[Tuple[str, str]]: @@ -209,7 +215,7 @@ def _kresd_unit_properties(config: KresConfig, kres_id: KresID) -> List[Tuple[st ("Restart", GLib.Variant("s", "always")), ("LimitNOFILE", GLib.Variant("t", 524288)), ("Environment", GLib.Variant("as", [f"SYSTEMD_INSTANCE={kres_id}"])), - ("Slice", GLib.Variant("s", _slice_name(config))), + ("Slice", GLib.Variant("s", _slice_name())), ] if config.server.watchdog: @@ -245,7 +251,7 @@ def _gc_unit_properties(config: KresConfig) -> Any: ("RestartUSec", GLib.Variant("t", 30000000)), ("StartLimitIntervalUSec", GLib.Variant("t", 400000000)), ("StartLimitBurst", GLib.Variant("u", 10)), - ("Slice", GLib.Variant("s", _slice_name(config))), + ("Slice", GLib.Variant("s", _slice_name())), ] return val @@ -289,16 +295,16 @@ def start_transient_kresd_unit(config: KresConfig, type_: SystemdType, kres_id: _start_transient_unit(type_, name, properties) -def start_slice(config: KresConfig, systemd_type: SystemdType) -> None: - _start_transient_unit(systemd_type, _slice_name(config), _kres_slice_properties()) +def start_slice(systemd_type: SystemdType) -> None: + _start_transient_unit(systemd_type, _slice_name(), _kres_slice_properties()) -def stop_slice(config: KresConfig, systemd_type: SystemdType) -> None: - stop_unit(systemd_type, _slice_name(config)) +def stop_slice(systemd_type: SystemdType) -> None: + stop_unit(systemd_type, _slice_name()) -def list_our_slice_processes(config: KresConfig, systemd_type: SystemdType) -> Set[str]: - return _list_slice_services(systemd_type, _slice_name(config)) +def list_our_slice_processes(systemd_type: SystemdType) -> Set[str]: + return _list_slice_services(systemd_type, _slice_name()) @_wrap_dbus_errors diff --git a/manager/knot_resolver_manager/utils/modelling.py b/manager/knot_resolver_manager/utils/modelling.py index 7276c65bb..6de14933f 100644 --- a/manager/knot_resolver_manager/utils/modelling.py +++ b/manager/knot_resolver_manager/utils/modelling.py @@ -525,7 +525,9 @@ class SchemaNode(Serializable): except SchemaException as e: errs.append(e) - if len(errs) > 0: + if len(errs) == 1: + raise errs[0] + elif len(errs) > 1: raise AggregateSchemaException(object_path, errs) return used_keys @@ -550,9 +552,9 @@ class SchemaNode(Serializable): if source and not isinstance(source, SchemaNode): unused = source.keys() - used_keys if len(unused) > 0: + keys = ", ".join((f"'{u}'" for u in unused)) raise SchemaException( - f"Keys {unused} in your configuration object are not part of the configuration schema." - " Are you using '-' instead of '_'?", + f"unexpected extra key(s) {keys}", object_path, ) diff --git a/manager/tests/integration/config.yml b/manager/tests/integration/config.yml index 4f24a0c04..b05f18b36 100644 --- a/manager/tests/integration/config.yml +++ b/manager/tests/integration/config.yml @@ -1,8 +1,8 @@ -id: integration network: listen: - interface: 127.0.0.1@5353 server: + id: integration-test workers: 1 rundir: tests/integration/run management: diff --git a/manager/tests/unit/datamodel/test_config_schema.py b/manager/tests/unit/datamodel/test_config_schema.py index bf2c7a483..d13a99b67 100644 --- a/manager/tests/unit/datamodel/test_config_schema.py +++ b/manager/tests/unit/datamodel/test_config_schema.py @@ -10,7 +10,7 @@ from knot_resolver_manager.utils.modelling import SchemaNode def test_dns64_true(): - config = KresConfig({"id": "test", "dns64": True}) + config = KresConfig({"server": {"id": "test"}, "dns64": True}) assert config.dns64 assert config.dns64.prefix == IPv6Network96("64:ff9b::/96") @@ -23,7 +23,7 @@ def test_dns64_default_false(): def test_dnssec_false(): - config = KresConfig({"id": "test", "dnssec": False}) + config = KresConfig({"server": {"id": "test"}, "dnssec": False}) assert config.dnssec == False diff --git a/manager/tests/unit/datamodel/test_server_schema.py b/manager/tests/unit/datamodel/test_server_schema.py index 8842cd741..471627f53 100644 --- a/manager/tests/unit/datamodel/test_server_schema.py +++ b/manager/tests/unit/datamodel/test_server_schema.py @@ -5,7 +5,7 @@ from knot_resolver_manager.exceptions import KresManagerException def test_watchdog(): - assert ServerSchema({"watchdog": {"qname": "nic.cz.", "qtype": "A"}}) + assert ServerSchema({"watchdog": {"qname": "nic.cz.", "qtype": "A"}, "id": "test"}) with raises(KresManagerException): ServerSchema({"backend": "supervisord", "watchdog": {"qname": "nic.cz.", "qtype": "A"}})