]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
moved `id` in config to the server section, additional refactoring
authorVasek Sraier <git@vakabus.cz>
Sun, 27 Mar 2022 17:14:24 +0000 (19:14 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:54 +0000 (16:17 +0200)
manager/etc/knot-resolver/config.dev.yml
manager/knot_resolver_manager/constants.py
manager/knot_resolver_manager/datamodel/config_schema.py
manager/knot_resolver_manager/datamodel/server_schema.py
manager/knot_resolver_manager/datamodel/types/types.py
manager/knot_resolver_manager/kresd_controller/systemd/__init__.py
manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py
manager/knot_resolver_manager/utils/modelling.py
manager/tests/integration/config.yml
manager/tests/unit/datamodel/test_config_schema.py
manager/tests/unit/datamodel/test_server_schema.py

index 9bba35e4ef41c30733d5a1404ffdf28eab94939e..2adfd962949515f1553cfa462e4ab2f5dde1329e 100644 (file)
@@ -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:
index 1f2e7f71134ede7cf7551d0ee24f366b0b73dae3..b85cf5b9abfcb487cd1425c4a9b94a821d7cf312 100644 (file)
@@ -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)
index 0c02232d8b9763f32a2ab198e67f4e34ac3e207f..4341326ef27ba7f75510cfcc888d78dcb83c5584 100644 (file)
@@ -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"}})
index 9c58961d5124d633d8fce109ba7fbf74549701e5..a77e6bf3ffbc64080857c7e663cd0a4e09dc951c 100644 (file)
@@ -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
index 0a884b5a40046080fa560a0233af2c2fbafa8baa..da14dcff17ac6d3ab22ecd2c874f37e291f13389 100644 (file)
@@ -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
index 5848fa7d413c5887c1c545faa8d32c1b6b399a81..a5550accc7fe5ebc6bb49b28d1d0be163d5a2e5d 100644 (file)
@@ -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
index b12ed609449130afda70a3f43ae0b5da98054303..7da54f7c7f7a4d410a5d1bac88003875fd83cc3a 100644 (file)
@@ -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
index 7276c65bb153ae590830082ad2c7210dafb9e8b2..6de14933fa686e6e7f5c881b57c596bee45b0905 100644 (file)
@@ -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,
                 )
 
index 4f24a0c04a4997853e12621b505eb17fd263ecb0..b05f18b36b97a1dd2820705d15c28fec8eae4100 100644 (file)
@@ -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:
index bf2c7a4839332a2fc01ac36064ae72a1c224e2b7..d13a99b67f6f076ceb0d47665841f5c96a35d99d 100644 (file)
@@ -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
 
index 8842cd74107e7d0d5239f00a1a7a5286ed8ad9b8..471627f53d9d220b9919eb375836ecb6b4d66099 100644 (file)
@@ -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"}})