From 4c2278596d5b72a0c6e6c7e688f445fce59fc7de Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 24 Feb 2025 16:17:28 -0500 Subject: [PATCH] cephadm: add daemon type predicate func to daemons_matching Add a predicate function [1] argument, in the style of itertools, to the daemons_matching function to support filtering within the "fast path" of the daemon listing on daemon type before additional information about a daemon/service is done. [1] - https://en.wikipedia.org/wiki/Boolean-valued_function Signed-off-by: John Mulligan --- src/cephadm/cephadmlib/listing.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/cephadm/cephadmlib/listing.py b/src/cephadm/cephadmlib/listing.py index 44004b572af..7ece41b713d 100644 --- a/src/cephadm/cephadmlib/listing.py +++ b/src/cephadm/cephadmlib/listing.py @@ -50,7 +50,17 @@ import os import logging -from typing import TypedDict, Union, Optional, Iterator, List, Any, Dict, cast +from typing import ( + Any, + Callable, + Dict, + Iterator, + List, + Optional, + TypedDict, + Union, + cast, +) from .context import CephadmContext from .daemon_identity import DaemonIdentity @@ -191,6 +201,7 @@ def daemons_matching( daemon_name: Optional[str] = None, daemon_type: Optional[str] = None, fsid: Optional[str] = None, + daemon_type_predicate: Optional[Callable[[str], bool]] = None, ) -> Iterator[Union[LegacyDaemonEntry, DaemonEntry]]: """Iterate over the daemons configured on the current node, matching daemon name or daemon type if supplied. @@ -201,6 +212,11 @@ def daemons_matching( continue if daemon_type is not None and daemon_type != entry.daemon_type: continue + if ( + daemon_type_predicate is not None + and not daemon_type_predicate(entry.daemon_type) + ): + continue elif isinstance(entry, DaemonEntry): if fsid is not None and fsid != entry.identity.fsid: continue @@ -214,6 +230,11 @@ def daemons_matching( and daemon_type != entry.identity.daemon_type ): continue + if ( + daemon_type_predicate is not None + and not daemon_type_predicate(entry.identity.daemon_type) + ): + continue else: raise ValueError(f'unexpected entry type: {entry}') yield entry -- 2.47.3