Skip to content

Commit a94d38a

Browse files
committed
'cache-clear' remade to 'cache/clear'
1 parent ac5506c commit a94d38a

File tree

6 files changed

+50
-35
lines changed

6 files changed

+50
-35
lines changed

doc/config-cache.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ There are two specifics to purging cache records matching specified criteria:
6666

6767
.. code-block:: none
6868
69-
$ kresctl cache-clear example.com.
69+
$ kresctl cache clear example.com.
7070
7171
.. [#] This is a consequence of DNSSEC negative cache which relies on proofs of non-existence on various owner nodes. It is impossible to efficiently flush part of DNS zones signed with NSEC3.
7272

doc/manager-api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ List of API endpoints
8888
- ``GET /metrics`` provides Prometheus metrics
8989
- ``GET /`` static response that could be used to determine, whether the Manager is running
9090
- ``POST /stop`` gracefully stops the Manager, empty request body
91-
- ``POST /cache-clear`` purges cache records matching the specified criteria, see :ref:`cache clearing <config-cache-clear>`
91+
- ``POST /cache/clear`` purges cache records matching the specified criteria, see :ref:`cache clearing <config-cache-clear>`
9292
- ``{GET,PUT,DELETE,PATCH} /v1/config`` allows reading and modifying current configuration
9393

9494

doc/manager-client.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Only one of these arguments can be selected during the execution of a single ``k
158158
159159
$ kresctl metrics ./metrics/data.txt
160160
161-
.. option:: cache-clear
161+
.. option:: cache clear
162162

163163
Purge cache records matching the specified criteria.
164164

@@ -187,9 +187,9 @@ Only one of these arguments can be selected during the execution of a single ``k
187187

188188
.. code-block:: bash
189189
190-
$ kresctl cache-clear
191-
$ kresctl cache-clear example.com.
192-
$ kresctl cache-clear --exact-name example.com.
190+
$ kresctl cache clear
191+
$ kresctl cache clear example.com.
192+
$ kresctl cache clear --exact-name example.com.
193193
194194
195195
.. option:: schema

manager/knot_resolver_manager/cli/cmd/cache_clear.py manager/knot_resolver_manager/cli/cmd/cache.py

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import sys
3-
from typing import Any, Dict, List, Tuple, Type
3+
from enum import Enum
4+
from typing import Any, Dict, List, Optional, Tuple, Type
45

56
from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
67
from knot_resolver_manager.datamodel.cache_schema import CacheClearRPCSchema
@@ -9,69 +10,86 @@
910
from knot_resolver_manager.utils.requests import request
1011

1112

13+
class CacheOperations(Enum):
14+
CLEAR = 0
15+
16+
1217
@register_command
13-
class CacheClearCommand(Command):
18+
class CacheCommand(Command):
1419
def __init__(self, namespace: argparse.Namespace) -> None:
1520
super().__init__(namespace)
21+
self.operation: Optional[CacheOperations] = namespace.operation if hasattr(namespace, "operation") else None
1622

17-
config_dict: Dict[str, Any] = {"exact-name": namespace.exact_name}
18-
23+
# CLEAR operation
24+
self.clear_dict: Dict[str, Any] = {}
25+
if hasattr(namespace, "exact_name"):
26+
self.clear_dict["exact-name"] = namespace.exact_name
1927
if hasattr(namespace, "name"):
20-
config_dict["name"] = namespace.name
28+
self.clear_dict["name"] = namespace.name
2129
if hasattr(namespace, "rr_type"):
22-
config_dict["rr-type"] = namespace.rr_type
30+
self.clear_dict["rr-type"] = namespace.rr_type
2331
if hasattr(namespace, "chunk_size"):
24-
config_dict["chunk-size"] = namespace.chunk_size
25-
26-
try:
27-
self.config = CacheClearRPCSchema(config_dict)
28-
except (AggregateDataValidationError, DataValidationError) as e:
29-
print(e, file=sys.stderr)
30-
sys.exit(1)
32+
self.clear_dict["chunk-size"] = namespace.chunk_size
3133

3234
@staticmethod
3335
def register_args_subparser(
3436
subparser: "argparse._SubParsersAction[argparse.ArgumentParser]",
3537
) -> Tuple[argparse.ArgumentParser, "Type[Command]"]:
36-
cache_clear = subparser.add_parser("cache-clear", help="Purge cache records matching specified criteria.")
37-
cache_clear.set_defaults(exact_name=False)
38-
cache_clear.add_argument(
38+
cache_parser = subparser.add_parser("cache", help="Performs operations on the running resolver's cache.")
39+
40+
config_subparsers = cache_parser.add_subparsers(help="operation type")
41+
42+
# CLEAR operation
43+
clear_subparser = config_subparsers.add_parser("clear", help="Purge cache records matching specified criteria.")
44+
clear_subparser.set_defaults(operation=CacheOperations.CLEAR, exact_name=False)
45+
clear_subparser.add_argument(
3946
"--exact-name",
4047
help="If set, only records with the same name are removed.",
4148
action="store_true",
4249
dest="exact_name",
4350
)
44-
cache_clear.add_argument(
51+
clear_subparser.add_argument(
4552
"--rr-type",
4653
help="Optional, you may additionally specify the type to remove, but that is only supported with '--exact-name' flag set.",
4754
action="store",
4855
type=str,
4956
)
50-
cache_clear.add_argument(
57+
clear_subparser.add_argument(
5158
"--chunk-size",
5259
help="Optional, the number of records to remove in one round; default: 100."
5360
" The purpose is not to block the resolver for long. The resolver repeats the command after one millisecond until all matching data are cleared.",
5461
action="store",
5562
type=int,
5663
default=100,
5764
)
58-
cache_clear.add_argument(
65+
clear_subparser.add_argument(
5966
"name",
6067
type=str,
6168
nargs="?",
6269
help="Optional, subtree to purge; if the name isn't provided, whole cache is purged (and any other parameters are disregarded).",
6370
default=None,
6471
)
6572

66-
return cache_clear, CacheClearCommand
73+
return cache_parser, CacheCommand
6774

6875
@staticmethod
6976
def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords:
7077
return {}
7178

7279
def run(self, args: CommandArgs) -> None:
73-
body: str = DataFormat.JSON.dict_dump(self.config.get_unparsed_data())
74-
response = request(args.socket, "POST", "cache-clear", body)
80+
if not self.operation:
81+
args.subparser.print_help()
82+
sys.exit()
83+
84+
if self.operation == CacheOperations.CLEAR:
85+
try:
86+
validated = CacheClearRPCSchema(self.clear_dict)
87+
except (AggregateDataValidationError, DataValidationError) as e:
88+
print(e, file=sys.stderr)
89+
sys.exit(1)
90+
91+
body: str = DataFormat.JSON.dict_dump(validated.get_unparsed_data())
92+
response = request(args.socket, "POST", "cache/clear", body)
7593

7694
if response.status != 200:
7795
print(response, file=sys.stderr)

manager/knot_resolver_manager/server.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
from knot_resolver_manager.constants import DEFAULT_MANAGER_CONFIG_FILE, PID_FILE_NAME, init_user_constants
2626
from knot_resolver_manager.datamodel.cache_schema import CacheClearRPCSchema
2727
from knot_resolver_manager.datamodel.config_schema import KresConfig, get_rundir_without_validation
28-
from knot_resolver_manager.datamodel.globals import (
29-
Context,
30-
set_global_validation_context,
31-
)
28+
from knot_resolver_manager.datamodel.globals import Context, set_global_validation_context
3229
from knot_resolver_manager.datamodel.management_schema import ManagementSchema
3330
from knot_resolver_manager.exceptions import CancelStartupExecInsteadException, KresManagerException
3431
from knot_resolver_manager.kresd_controller import get_best_controller_implementation
@@ -333,7 +330,7 @@ def _setup_routes(self) -> None:
333330
web.get("/schema", self._handler_schema),
334331
web.get("/schema/ui", self._handle_view_schema),
335332
web.get("/metrics", self._handler_metrics),
336-
web.post("/cache-clear", self._handler_cache_clear),
333+
web.post("/cache/clear", self._handler_cache_clear),
337334
]
338335
)
339336

manager/tests/packaging/interactive/cahe-clear.sh manager/tests/packaging/interactive/cache-clear.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

33
# clear full cache
4-
kresctl cache-clear
4+
kresctl cache clear
55
if [ "$?" -ne "0" ]; then
66
echo "Could not clear full cache"
77
exit 1
88
fi
99

1010
# clear just example.com. AAAA record
11-
kresctl cache-clear --exact-name --rr-type AAAA example.com.
11+
kresctl cache clear --exact-name --rr-type AAAA example.com.
1212
if [ "$?" -ne "0" ]; then
1313
echo "Could not clear example.com. AAAA record"
1414
exit 1

0 commit comments

Comments
 (0)