Skip to content

Commit b88be78

Browse files
committed
Refactor handling of inventory backend command line flag
This commit switches the inventory backend command line flag to the form `--inventory-backend=<backend name>`. Additionally, we restrict the allowed values for the flag to the known backends. To ensure the allowed values for the command line flag, and the known implementations are in sync, we introduce a dict holding the mapping from flag values to `Inventory` subclasses. Finally, we ensure that the selected backend is cached as a string in `cached.args["inventory-backend"]. Note that this is not how `cached.args` is used otherwise, but since it's unlikely that there ever will be a real command `inventory-backend`, this should be fine.
1 parent 94687b6 commit b88be78

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

kapitan/cli.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from kapitan import cached, defaults, setup_logging
2222
from kapitan.initialiser import initialise_skeleton
2323
from kapitan.inputs.jsonnet import jsonnet_file
24+
from kapitan.inventory import AVAILABLE_BACKENDS
2425
from kapitan.lint import start_lint
2526
from kapitan.refs.base import RefController, Revealer
2627
from kapitan.refs.cmd_parser import handle_refs_command
@@ -103,12 +104,12 @@ def build_parser():
103104
subparser = parser.add_subparsers(help="commands", dest="subparser_name")
104105

105106
inventory_backend_parser = argparse.ArgumentParser(add_help=False)
106-
inventory_backend_group = inventory_backend_parser.add_argument_group("inventory_backend")
107-
inventory_backend_group.add_argument(
108-
"--reclass",
109-
action="store_true",
110-
default=from_dot_kapitan("inventory_backend", "reclass", False),
111-
help="use reclass as inventory backend (default)",
107+
inventory_backend_parser.add_argument(
108+
"--inventory-backend",
109+
action="store",
110+
default=from_dot_kapitan("inventory_backend", "inventory", "reclass"),
111+
choices=AVAILABLE_BACKENDS.keys(),
112+
help="Select the inventory backend to use (default=reclass)",
112113
)
113114

114115
eval_parser = subparser.add_parser("eval", aliases=["e"], help="evaluate jsonnet file")
@@ -663,6 +664,8 @@ def main():
663664
# cache args where key is subcommand
664665
assert "name" in args, "All cli commands must have provided default name"
665666
cached.args[args.name] = args
667+
if "inventory_backend" in args:
668+
cached.args["inventory-backend"] = args.inventory_backend
666669

667670
if hasattr(args, "verbose") and args.verbose:
668671
setup_logging(level=logging.DEBUG, force=True)

kapitan/inventory/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
from typing import Type
2+
13
from .inv_reclass import ReclassInventory
24
from .inventory import Inventory
5+
6+
# Dict mapping values for command line flag `--inventory-backend` to the
7+
# associated `Inventory` subclass.
8+
AVAILABLE_BACKENDS: dict[str, Type[Inventory]] = {
9+
"reclass": ReclassInventory,
10+
}

kapitan/resources.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import kapitan.cached as cached
2323
from kapitan import __file__ as kapitan_install_path
2424
from kapitan.errors import CompileError, InventoryError, KapitanError
25-
from kapitan.inventory import Inventory, ReclassInventory
25+
from kapitan.inventory import Inventory, ReclassInventory, AVAILABLE_BACKENDS
2626
from kapitan.utils import PrettyDumper, deep_get, flatten_dict, render_jinja2_file, sha256_string
2727

2828
logger = logging.getLogger(__name__)
@@ -316,13 +316,15 @@ def get_inventory(inventory_path) -> Inventory:
316316
if cached.inv and cached.inv.targets:
317317
return cached.inv
318318

319-
inventory_backend: Inventory = None
320-
321319
# select inventory backend
322-
if cached.args.get("inventory-backend") == "my-new-inventory":
323-
logger.debug("Using my-new-inventory as inventory backend")
320+
backend_id = cached.args.get("inventory-backend")
321+
backend = AVAILABLE_BACKENDS.get(backend_id)
322+
inventory_backend: Inventory = None
323+
if backend != None:
324+
logger.debug(f"Using {backend_id} as inventory backend")
325+
inventory_backend = backend(inventory_path)
324326
else:
325-
logger.debug("Using reclass as inventory backend")
327+
logger.debug(f"Backend {backend_id} is unknown, falling back to reclass as inventory backend")
326328
inventory_backend = ReclassInventory(inventory_path)
327329

328330
inventory_backend.search_targets()

0 commit comments

Comments
 (0)