Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor handling of inventory backend command line flag #1127

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 15 additions & 7 deletions docs/pages/commands/kapitan_dotfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ version: 0.30 # Allows any 0.30.x release to run
...
```

### `compile`
### Command line flags

You can also permanently define all command line flags in the `.kapitan` config file. For example:

Expand All @@ -45,19 +45,27 @@ would be equivalent to running:
kapitan compile --indent 4 --parallelism 8
```

### inventory
For flags which are shared by multiple commands, you can either selectively define them for single commmands in a section with the same name as the command, or you can set any flags in section `global`, in which case they're applied for all commands.
If you set a flag in both the `global` section and a command's section, the value from the command's section takes precedence over the value from the global section.

In some cases, you might want to store the inventory under a different directory. You can configure the `inventory` section of the **Kapitan** dotfile to make sure it's persisted across all **Kapitan** runs.
As an example, you can configure the `inventory-path` in the `global` section of the **Kapitan** dotfile to make sure it's persisted across all **Kapitan** runs.

```yaml
...

inventory:
global:
inventory-path: ./some_path
```

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

```shell
kapitan inventory --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
14 changes: 5 additions & 9 deletions kapitan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,15 @@ def dot_kapitan_config():

def from_dot_kapitan(command, flag, default):
"""
Returns the 'flag' for 'command' from .kapitan file. If failed, returns 'default'
Returns the 'flag' from the '<command>' or from the 'global' section in the .kapitan file. If
neither section proivdes a value for the flag, the value passed in `default` is returned.
"""
kapitan_config = dot_kapitan_config()

try:
if kapitan_config[command]:
flag_value = kapitan_config[command][flag]
if flag_value:
return flag_value
except KeyError:
pass
global_config = kapitan_config.get("global", {})
cmd_config = kapitan_config.get(command, {})

return default
return cmd_config.get(flag, global_config.get(flag, default))


def compare_versions(v1_raw, v2_raw):
Expand Down
53 changes: 53 additions & 0 deletions tests/test_from_dot_kapitan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import shutil
import tempfile
import unittest
import yaml

from kapitan.utils import from_dot_kapitan
from kapitan.cached import reset_cache


class FromDotKapitanTest(unittest.TestCase):
"Test loading flags from .kapitan"

def _setup_dot_kapitan(self, config):
with open(self.work_dir.name + "/.kapitan", "w", encoding="utf-8") as f:
yaml.safe_dump(config, f)

def setUp(self):
self.orig_dir = os.getcwd()
self.work_dir = tempfile.TemporaryDirectory()
os.chdir(self.work_dir.name)

def test_no_file(self):
assert from_dot_kapitan("compile", "inventory-path", "./some/fallback") == "./some/fallback"

def test_no_option(self):
self._setup_dot_kapitan(
{"global": {"inventory-backend": "reclass"}, "compile": {"inventory-path": "./path/to/inv"}}
)
assert from_dot_kapitan("inventory", "inventory-path", "./some/fallback") == "./some/fallback"

def test_cmd_option(self):
self._setup_dot_kapitan(
{"global": {"inventory-backend": "reclass"}, "compile": {"inventory-path": "./path/to/inv"}}
)
assert from_dot_kapitan("compile", "inventory-path", "./some/fallback") == "./path/to/inv"

def test_global_option(self):
self._setup_dot_kapitan(
{"global": {"inventory-path": "./some/path"}, "compile": {"inventory-path": "./path/to/inv"}}
)
assert from_dot_kapitan("inventory", "inventory-path", "./some/fallback") == "./some/path"

def test_command_over_global_option(self):
self._setup_dot_kapitan(
{"global": {"inventory-path": "./some/path"}, "compile": {"inventory-path": "./path/to/inv"}}
)
assert from_dot_kapitan("compile", "inventory-path", "./some/fallback") == "./path/to/inv"

def tearDown(self):
self.work_dir.cleanup()
os.chdir(self.orig_dir)
reset_cache()
Loading