Skip to content

Commit

Permalink
Merge pull request #1164 from kapicorp/test/reclass-fix
Browse files Browse the repository at this point in the history
Fix inventory not handling compose-target-name
  • Loading branch information
ademariag committed Apr 11, 2024
2 parents c7d440b + 7c92151 commit 3d31f5f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 51 deletions.
4 changes: 2 additions & 2 deletions docs/pages/commands/kapitan_compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ The `--embed-refs` flags tells **Kapitan** to embed these references on compile,
[--inventory-path INVENTORY_PATH] [--cache]
[--cache-paths PATH [PATH ...]]
[--ignore-version-check] [--use-go-jsonnet]
[--compose-node-name] [--schemas-path SCHEMAS_PATH]
[--compose-target-name] [--schemas-path SCHEMAS_PATH]
[--yaml-multiline-string-style STYLE]
[--yaml-dump-null-as-empty]
[--targets TARGET [TARGET ...] | --labels
Expand Down Expand Up @@ -203,7 +203,7 @@ The `--embed-refs` flags tells **Kapitan** to embed these references on compile,
--ignore-version-check
ignore the version from .kapitan
--use-go-jsonnet use go-jsonnet
--compose-node-name Create same subfolder structure from inventory/targets
--compose-target-name Create same subfolder structure from inventory/targets
inside compiled folder
--schemas-path SCHEMAS_PATH
set schema cache path, default is "./schemas"
Expand Down
26 changes: 13 additions & 13 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def trigger_compile(args):
args.targets,
args.labels,
ref_controller,
prune=(args.prune),
prune=args.prune,
indent=args.indent,
reveal=args.reveal,
cache=args.cache,
Expand All @@ -94,7 +94,7 @@ def trigger_compile(args):
jinja2_filters=args.jinja2_filters,
verbose=hasattr(args, "verbose") and args.verbose,
use_go_jsonnet=args.use_go_jsonnet,
compose_node_name=args.compose_node_name,
compose_target_name=args.compose_target_name,
)


Expand All @@ -112,6 +112,13 @@ def build_parser():
help="Select the inventory backend to use (default=reclass)",
)

inventory_backend_parser.add_argument(
"--compose-target-name", "--compose-target-name",
help="Create same subfolder structure from inventory/targets inside compiled folder",
action="store_true",
default=from_dot_kapitan("global", "compose-target-name", from_dot_kapitan("compile", "compose-node-name", False)),
)

eval_parser = subparser.add_parser("eval", aliases=["e"], help="evaluate jsonnet file")
eval_parser.add_argument("jsonnet_file", type=str)
eval_parser.set_defaults(func=trigger_eval, name="eval")
Expand Down Expand Up @@ -283,17 +290,6 @@ def build_parser():
default=from_dot_kapitan("compile", "use-go-jsonnet", False),
)

# compose-node-name should be used in conjunction with reclass
# config "compose_node_name: true". This allows us to make the same subfolder
# structure in the inventory folder inside the compiled folder
# https://github.com/kapicorp/kapitan/issues/932
compile_parser.add_argument(
"--compose-node-name",
help="Create same subfolder structure from inventory/targets inside compiled folder",
action="store_true",
default=from_dot_kapitan("compile", "compose-node-name", False),
)

compile_parser.add_argument(
"--schemas-path",
default=from_dot_kapitan("validate", "schemas-path", "./schemas"),
Expand Down Expand Up @@ -666,6 +662,10 @@ def main():
cached.args[args.name] = args
if "inventory_backend" in args:
cached.args["inventory-backend"] = args.inventory_backend
cached.args.setdefault("global", {}).setdefault("inventory-backend", args.inventory_backend)

if "compose_target_name" in args:
cached.args.setdefault("global", {}).setdefault("compose_target_name", args.compose_target_name)

if hasattr(args, "verbose") and args.verbose:
setup_logging(level=logging.DEBUG, force=True)
Expand Down
4 changes: 2 additions & 2 deletions kapitan/inputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def compile_input_path(self, input_path, comp_obj, ext_vars, **kwargs):

logger.debug("Compiling %s", input_path)
try:
if kwargs.get("compose_node_name", False):
if kwargs.get("compose_target_name", False):
_compile_path = os.path.join(self.compile_path, target_name.replace(".", "/"), output_path)
else:
_compile_path = os.path.join(self.compile_path, target_name, output_path)
Expand All @@ -87,7 +87,7 @@ def compile_input_path(self, input_path, comp_obj, ext_vars, **kwargs):
def make_compile_dirs(self, target_name, output_path, **kwargs):
"""make compile dirs, skips if dirs exist"""
_compile_path = os.path.join(self.compile_path, target_name, output_path)
if kwargs.get("compose_node_name", False):
if kwargs.get("compose_target_name", False):
os.makedirs(_compile_path.replace(".", "/"), exist_ok=True)
else:
os.makedirs(_compile_path, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions kapitan/inventory/inv_reclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def render_targets(self, targets: list = None, ignore_class_notfound: bool = Fal
def get_reclass_config(
inventory_path: str,
ignore_class_notfound: bool = False,
compose_node_name: bool = False,
compose_target_name: bool = False,
normalise_nodes_classes: bool = True,
) -> dict:
# set default values initially
Expand All @@ -71,7 +71,7 @@ def get_reclass_config(
"inventory_base_uri": inventory_path,
"nodes_uri": "targets",
"classes_uri": "classes",
"compose_node_name": compose_node_name,
"compose_node_name": compose_target_name,
"allow_none_override": True,
"ignore_class_notfound": ignore_class_notfound,
}
Expand Down
5 changes: 3 additions & 2 deletions kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,15 @@ def get_inventory(inventory_path) -> Inventory:

# select inventory backend
backend_id = cached.args.get("inventory-backend")
compose_target_name = cached.args["global"].get("compose_target_name")
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)
inventory_backend = backend(inventory_path, compose_target_name)
else:
logger.debug(f"Backend {backend_id} is unknown, falling back to reclass as inventory backend")
inventory_backend = ReclassInventory(inventory_path)
inventory_backend = ReclassInventory(inventory_path, compose_target_name)

inventory_backend.search_targets()

Expand Down
61 changes: 31 additions & 30 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d31f5f

Please sign in to comment.