Skip to content

Commit

Permalink
Fix 1210 and improves loading of inventory backends
Browse files Browse the repository at this point in the history
  • Loading branch information
ademariag committed Aug 31, 2024
1 parent 9371795 commit 48506cf
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 84 deletions.
4 changes: 2 additions & 2 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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.inventory import AVAILABLE_BACKENDS, InventoryBackends
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 @@ -108,7 +108,7 @@ def build_parser():
inventory_backend_parser.add_argument(
"--inventory-backend",
action="store",
default=from_dot_kapitan("inventory_backend", "inventory-backend", "reclass"),
default=from_dot_kapitan("inventory_backend", "inventory-backend", InventoryBackends.RECLASS),
choices=AVAILABLE_BACKENDS.keys(),
help="Select the inventory backend to use (default=reclass)",
)
Expand Down
58 changes: 51 additions & 7 deletions kapitan/inventory/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
from .inventory import Inventory

try:
from enum import StrEnum
except ImportError:
from strenum import StrEnum

from typing import Type

from .inv_reclass import ReclassInventory
from .inv_reclass_rs import ReclassRsInventory
from .inv_omegaconf.inv_omegaconf import OmegaConfInventory
from .inventory import Inventory

class InventoryBackends(StrEnum):
"""
Enumeration of available inventory backends.
"""
RECLASS = "reclass"
RECLASS_RS = "reclass-rs"
OMEGACONF = "omegaconf"
DEFAULT = RECLASS


def load_reclass_backend():
"""
Enable the reclass inventory backend.
"""
from .inv_reclass import ReclassInventory
return ReclassInventory


def load_reclass_rs_backend():
"""
Enable the reclass-rs inventory backend.
"""
from .inv_reclass_rs import ReclassRsInventory
return ReclassRsInventory


def load_omegaconf_backend():
"""
Enable the omegaconf inventory backend.
"""
from .inv_omegaconf.inv_omegaconf import OmegaConfInventory
return OmegaConfInventory


# 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,
"omegaconf": OmegaConfInventory,
InventoryBackends.RECLASS: load_reclass_backend,
InventoryBackends.RECLASS_RS: load_reclass_rs_backend,
InventoryBackends.OMEGACONF: load_omegaconf_backend,
}


def get_inventory_backend(backend_name: str) -> Type[Inventory]:
"""
Get the `Inventory` subclass associated with the given `backend_name`.
"""
return AVAILABLE_BACKENDS.get(backend_name, AVAILABLE_BACKENDS[InventoryBackends.DEFAULT])()
9 changes: 4 additions & 5 deletions kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from kapitan import __file__ as kapitan_install_path
from kapitan.inputs.kadet import Dict
from kapitan.errors import CompileError, InventoryError, KapitanError
from kapitan.inventory import Inventory, ReclassInventory, AVAILABLE_BACKENDS
from kapitan.inventory import Inventory, get_inventory_backend
from kapitan.utils import PrettyDumper, deep_get, flatten_dict, render_jinja2_file, sha256_string

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -327,10 +327,9 @@ def get_inventory(inventory_path, ignore_class_not_found: bool = False) -> Inven
# select inventory backend
backend_id = hasattr(cached.args, "inventory_backend") and cached.args.inventory_backend
compose_target_name = hasattr(cached.args, "compose_target_name") and cached.args.compose_target_name
backend = AVAILABLE_BACKENDS.get(backend_id, AVAILABLE_BACKENDS.get("reclass"))
inventory_backend: Inventory = None
backend = get_inventory_backend(backend_id)

logger.debug(f"Using {backend_id} as inventory backend")
logger.debug(f"Using {backend.__name__} as inventory backend")
inventory_backend = backend(inventory_path=inventory_path, compose_target_name=compose_target_name, ignore_class_not_found=ignore_class_not_found)

cached.inv = inventory_backend
Expand All @@ -339,7 +338,7 @@ def get_inventory(inventory_path, ignore_class_not_found: bool = False) -> Inven
# if we use forked processes, we need to load the inventory for kadet once
# and pass it to the children, to avoid re-reading the inventory for each child
# TODO(adenaria): Improve to only do it for kadet
if cached.args.mp_method != "spawn":
if hasattr(cached.args, "mp_method") and cached.args.mp_method != "spawn":
cached.inventory_global_kadet = Dict(cached.global_inv)

# migrate inventory to selected inventory backend
Expand Down
Loading

0 comments on commit 48506cf

Please sign in to comment.