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

Add support for global section in .kapitan #1131

Merged
merged 1 commit 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
15 changes: 6 additions & 9 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,16 @@ 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:

```shell
kapitan inventory --inventory-path=./some_path
```
which would be equivalent to running any command with `--inventory-path=./some_path`.
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