Skip to content

Commit

Permalink
Refactor handling of inventory backend command line flag
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simu committed Jan 27, 2024
1 parent 94687b6 commit 83c413f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
15 changes: 9 additions & 6 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from kapitan import cached, defaults, setup_logging
from kapitan.initialiser import initialise_skeleton
from kapitan.inputs.jsonnet import jsonnet_file
from kapitan.inventory import AVAILABLE_BACKENDS
from kapitan.lint import start_lint
from kapitan.refs.base import RefController, Revealer
from kapitan.refs.cmd_parser import handle_refs_command
Expand Down Expand Up @@ -103,12 +104,12 @@ def build_parser():
subparser = parser.add_subparsers(help="commands", dest="subparser_name")

inventory_backend_parser = argparse.ArgumentParser(add_help=False)
inventory_backend_group = inventory_backend_parser.add_argument_group("inventory_backend")
inventory_backend_group.add_argument(
"--reclass",
action="store_true",
default=from_dot_kapitan("inventory_backend", "reclass", False),
help="use reclass as inventory backend (default)",
inventory_backend_parser.add_argument(
"--inventory-backend",
action="store",
default=from_dot_kapitan("inventory_backend", "inventory", "reclass"),
choices=AVAILABLE_BACKENDS.keys(),
help="Select the inventory backend to use (default=reclass)",
)

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

if hasattr(args, "verbose") and args.verbose:
setup_logging(level=logging.DEBUG, force=True)
Expand Down
8 changes: 8 additions & 0 deletions kapitan/inventory/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
from typing import Type

from .inv_reclass import ReclassInventory
from .inventory import Inventory

# Dict mapping values for command line flag `--inventory-backend` to the
# associated `Inventory` subclass.
AVAILABLE_BACKENDS: dict[str, Type[Inventory]] = {
"reclass": ReclassInventory,
}
14 changes: 8 additions & 6 deletions kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import kapitan.cached as cached
from kapitan import __file__ as kapitan_install_path
from kapitan.errors import CompileError, InventoryError, KapitanError
from kapitan.inventory import Inventory, ReclassInventory
from kapitan.inventory import Inventory, ReclassInventory, AVAILABLE_BACKENDS
from kapitan.utils import PrettyDumper, deep_get, flatten_dict, render_jinja2_file, sha256_string

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -316,13 +316,15 @@ def get_inventory(inventory_path) -> Inventory:
if cached.inv and cached.inv.targets:
return cached.inv

inventory_backend: Inventory = None

# select inventory backend
if cached.args.get("inventory-backend") == "my-new-inventory":
logger.debug("Using my-new-inventory as inventory backend")
backend_id = cached.args.get("inventory-backend")
backend = AVAILABLE_BACKENDS.get(backend_id)
inventory_backend: Inventory = None
if backend != None:
logger.debug(f"Using {backend_id} as inventory backend")
inventory_backend = backend(inventory_path)
else:
logger.debug("Using reclass as inventory backend")
logger.debug(f"Backend {backend_id} is unknown, falling back to reclass as inventory backend")
inventory_backend = ReclassInventory(inventory_path)

inventory_backend.search_targets()
Expand Down

0 comments on commit 83c413f

Please sign in to comment.