forked from kapicorp/kapitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kapicorp#1059 from projectsyn/feat/reclass-rs
Add `reclass-rs` inventory backend
- Loading branch information
Showing
14 changed files
with
349 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# The reclass-rs inventory backend | ||
|
||
## Overview | ||
|
||
[Reclass-rs](https://github.com/projectsyn/reclass-rs) is a reimplementation of Kapitan's Reclass fork in Rust. | ||
Please note that the Rust implementation doesn't support all the features of Kapitan's Reclass fork yet. | ||
|
||
However, reclass-rs improves rendering time for the inventory significantly, especially if you're making heavy use of parameter references in class includes. | ||
If some of the Reclass features or options that you're using are missing in reclass-rs, don't hesitate to open an issue in the [reclass-rs project](https://github.com/projectsyn/reclass-rs/issues/new?assignees=&labels=enhancement&projects=&template=03_missing_reclass_feature.md). | ||
|
||
## Installation | ||
|
||
The `reclass-rs` Python package is an optional dependency of Kapitan. | ||
You can install it as follows: | ||
|
||
```shell | ||
pip install kapitan[reclass-rs] | ||
``` | ||
|
||
## Usage | ||
|
||
To use the reclass-rs inventory backend, you need to pass `--inventory-backend=reclass-rs` on the command line. | ||
If you want to permanently switch to the reclass-rs inventory backend, you can select the inventory backend in the [.kapitan config file](../commands/kapitan_dotfile.md): | ||
|
||
```yaml | ||
global: | ||
inventory-backend: reclass-rs | ||
``` | ||
## Performance comparison | ||
For the performance comparison, a real Kapitan inventory which makes heavy use of parameter interpolation in class includes was rendered with both Reclass and reclass-rs. | ||
The example inventory that was used for the performance comparison contains 325 classes and 56 targets. | ||
The example inventory renders to a total of 25MB of YAML. | ||
### Reclass | ||
``` | ||
$ time kapitan inventory -v --inventory-backend=reclass > inv.yml | ||
[ ... some output omitted ... ] | ||
kapitan.resources DEBUG Using reclass as inventory backend | ||
kapitan.inventory.inv_reclass DEBUG Inventory reclass: No config file found. Using reclass inventory config defaults | ||
kapitan.inventory.inv_reclass DEBUG Inventory rendering with reclass took 0:01:06.037057 | ||
|
||
real 1m23.840s | ||
user 1m23.520s | ||
sys 0m0.287s | ||
``` | ||
|
||
Reclass takes 1 minute and 6 seconds to render the example inventory. | ||
The rest of the runtime (roughly 18 seconds) is spent in writing the resulting 25MB of YAML to the output file. | ||
|
||
### reclass-rs | ||
|
||
``` | ||
$ time kapitan inventory -v --inventory-backend=reclass-rs > inv-rs.yml | ||
[ ... some output omitted ... ] | ||
kapitan.resources DEBUG Using reclass-rs as inventory backend | ||
kapitan.inventory.inv_reclass DEBUG Inventory reclass: No config file found. Using reclass inventory config defaults | ||
reclass-config.yml entry 'storage_type=yaml_fs' not implemented yet, ignoring... | ||
reclass-config.yml entry 'inventory_base_uri=./inventory' not implemented yet, ignoring... | ||
reclass-config.yml entry 'allow_none_override=true' not implemented yet, ignoring... | ||
kapitan.inventory.inv_reclass_rs DEBUG Inventory rendering with reclass-rs took 0:00:01.717107 | ||
real 0m19.921s | ||
user 0m35.586s | ||
sys 0m1.066s | ||
``` | ||
|
||
reclass-rs takes 1.7 seconds to render the example inventory. | ||
The rest of the runtime (roughly 18 seconds) is spent in writing the resulting 25MB of YAML to the output file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
from typing import Type | ||
|
||
from .inv_reclass import ReclassInventory | ||
from .inv_reclass_rs import ReclassRsInventory | ||
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, | ||
"reclass-rs": ReclassRsInventory, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import logging | ||
import os | ||
import reclass_rs | ||
|
||
from datetime import datetime | ||
|
||
from kapitan.errors import InventoryError | ||
|
||
from .inventory import Inventory, InventoryTarget | ||
from .inv_reclass import get_reclass_config | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ReclassRsInventory(Inventory): | ||
def _make_reclass_rs(self, ignore_class_notfound: bool): | ||
# Get Reclass config options with the same method that's used for `ReclassInventory`, but | ||
# disable the logic to normalise the `nodes_uri` and `classes_uri` options, since reclass-rs | ||
# expects those fields to be relative to the inventory path. | ||
config_dict = get_reclass_config( | ||
self.inventory_path, ignore_class_notfound, self.compose_target_name, False | ||
) | ||
|
||
# Turn on verbose config loading only if Kapitan loglevel is set to at least DEBUG. | ||
config = reclass_rs.Config.from_dict( | ||
self.inventory_path, config_dict, logger.isEnabledFor(logging.DEBUG) | ||
) | ||
return reclass_rs.Reclass.from_config(config) | ||
|
||
def render_targets(self, targets: list = None, ignore_class_notfound: bool = False): | ||
try: | ||
r = self._make_reclass_rs(ignore_class_notfound) | ||
start = datetime.now() | ||
inv = r.inventory() | ||
elapsed = datetime.now() - start | ||
logger.debug(f"Inventory rendering with reclass-rs took {elapsed}") | ||
|
||
for target_name, nodeinfo in inv.nodes.items(): | ||
self.targets[target_name].parameters = nodeinfo.parameters | ||
self.targets[target_name].classes = nodeinfo.classes | ||
|
||
except ValueError as e: | ||
logger.error(f"Reclass-rs error: {e}") | ||
raise InventoryError(f"{e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.