Skip to content

Commit 5459765

Browse files
authored
Merge pull request #1127 from projectsyn/feat/inventory-backend-flag
Refactor handling of inventory backend command line flag
2 parents 7a73a5b + 0c13d11 commit 5459765

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

docs/pages/commands/kapitan_compile.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,28 @@ The `--embed-refs` flags tells **Kapitan** to embed these references on compile,
148148
```
149149

150150
??? example "click to expand output"
151+
151152
```shell
152-
usage: kapitan compile [-h] [--search-paths JPATH [JPATH ...]]
153-
[--jinja2-filters FPATH] [--verbose] [--prune]
154-
[--quiet] [--output-path PATH] [--fetch]
155-
[--force-fetch] [--force] [--validate]
156-
[--parallelism INT] [--indent INT]
157-
[--refs-path REFS_PATH] [--reveal] [--embed-refs]
158-
[--inventory-path INVENTORY_PATH] [--cache]
159-
[--cache-paths PATH [PATH ...]]
160-
[--ignore-version-check] [--use-go-jsonnet]
161-
[--compose-node-name] [--schemas-path SCHEMAS_PATH]
162-
[--yaml-multiline-string-style STYLE]
163-
[--yaml-dump-null-as-empty]
164-
[--targets TARGET [TARGET ...] | --labels
165-
[key=value ...]]
166-
167-
optional arguments:
153+
usage: kapitan compile [-h] [--inventory-backend {reclass}]
154+
[--search-paths JPATH [JPATH ...]]
155+
[--jinja2-filters FPATH] [--verbose] [--prune]
156+
[--quiet] [--output-path PATH] [--fetch]
157+
[--force-fetch] [--force] [--validate]
158+
[--parallelism INT] [--indent INT]
159+
[--refs-path REFS_PATH] [--reveal] [--embed-refs]
160+
[--inventory-path INVENTORY_PATH] [--cache]
161+
[--cache-paths PATH [PATH ...]]
162+
[--ignore-version-check] [--use-go-jsonnet]
163+
[--compose-node-name] [--schemas-path SCHEMAS_PATH]
164+
[--yaml-multiline-string-style STYLE]
165+
[--yaml-dump-null-as-empty]
166+
[--targets TARGET [TARGET ...] | --labels
167+
[key=value ...]]
168+
169+
options:
168170
-h, --help show this help message and exit
171+
--inventory-backend {reclass}
172+
Select the inventory backend to use (default=reclass)
169173
--search-paths JPATH [JPATH ...], -J JPATH [JPATH ...]
170174
set search paths, default is ["."]
171175
--jinja2-filters FPATH, -J2F FPATH

docs/pages/commands/kapitan_dotfile.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,14 @@ global:
5858
```
5959
6060
which would be equivalent to running any command with `--inventory-path=./some_path`.
61+
62+
Another flag that you may want to set in the `global` section is `inventory-backend` to select a non-default inventory backend implementation.
63+
64+
```yaml
65+
global:
66+
inventory-backend: reclass
67+
```
68+
69+
which would be equivalent to always running **Kapitan** with `--inventory-backend=reclass`.
70+
71+
Please note that the `inventory-backend` flag currently can't be set through the command-specific sections of the **Kapitan** config file.

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-backend", "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)