Skip to content

Commit

Permalink
Merge pull request #1127 from projectsyn/feat/inventory-backend-flag
Browse files Browse the repository at this point in the history
Refactor handling of inventory backend command line flag
  • Loading branch information
ademariag committed Feb 2, 2024
2 parents 7a73a5b + 0c13d11 commit 5459765
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
36 changes: 20 additions & 16 deletions docs/pages/commands/kapitan_compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,28 @@ The `--embed-refs` flags tells **Kapitan** to embed these references on compile,
```

??? example "click to expand output"

```shell
usage: kapitan compile [-h] [--search-paths JPATH [JPATH ...]]
[--jinja2-filters FPATH] [--verbose] [--prune]
[--quiet] [--output-path PATH] [--fetch]
[--force-fetch] [--force] [--validate]
[--parallelism INT] [--indent INT]
[--refs-path REFS_PATH] [--reveal] [--embed-refs]
[--inventory-path INVENTORY_PATH] [--cache]
[--cache-paths PATH [PATH ...]]
[--ignore-version-check] [--use-go-jsonnet]
[--compose-node-name] [--schemas-path SCHEMAS_PATH]
[--yaml-multiline-string-style STYLE]
[--yaml-dump-null-as-empty]
[--targets TARGET [TARGET ...] | --labels
[key=value ...]]

optional arguments:
usage: kapitan compile [-h] [--inventory-backend {reclass}]
[--search-paths JPATH [JPATH ...]]
[--jinja2-filters FPATH] [--verbose] [--prune]
[--quiet] [--output-path PATH] [--fetch]
[--force-fetch] [--force] [--validate]
[--parallelism INT] [--indent INT]
[--refs-path REFS_PATH] [--reveal] [--embed-refs]
[--inventory-path INVENTORY_PATH] [--cache]
[--cache-paths PATH [PATH ...]]
[--ignore-version-check] [--use-go-jsonnet]
[--compose-node-name] [--schemas-path SCHEMAS_PATH]
[--yaml-multiline-string-style STYLE]
[--yaml-dump-null-as-empty]
[--targets TARGET [TARGET ...] | --labels
[key=value ...]]

options:
-h, --help show this help message and exit
--inventory-backend {reclass}
Select the inventory backend to use (default=reclass)
--search-paths JPATH [JPATH ...], -J JPATH [JPATH ...]
set search paths, default is ["."]
--jinja2-filters FPATH, -J2F FPATH
Expand Down
11 changes: 11 additions & 0 deletions docs/pages/commands/kapitan_dotfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ global:
```

which would be equivalent to running any command with `--inventory-path=./some_path`.

Another flag that you may want to set in the `global` section is `inventory-backend` to select a non-default inventory backend implementation.

```yaml
global:
inventory-backend: reclass
```

which would be equivalent to always running **Kapitan** with `--inventory-backend=reclass`.

Please note that the `inventory-backend` flag currently can't be set through the command-specific sections of the **Kapitan** config file.
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-backend", "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 5459765

Please sign in to comment.