From d68cc9795507ddc24cc7b35cf83662481838bcfa Mon Sep 17 00:00:00 2001 From: Matteo Voges <98756476+MatteoVoges@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:14:19 +0200 Subject: [PATCH] fix: namespace error with flag migrate --- kapitan/omegaconf_inv.py | 44 ++++++++++++++++++++++++++++++++++++---- kapitan/resources.py | 7 +++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/kapitan/omegaconf_inv.py b/kapitan/omegaconf_inv.py index f413a1120..2dd482f4e 100644 --- a/kapitan/omegaconf_inv.py +++ b/kapitan/omegaconf_inv.py @@ -68,7 +68,7 @@ def inventory_omegaconf( name, config = load_target(target, classes_searchpath, ignore_class_notfound) inv["nodes"][name] = config except Exception as e: - logger.error(f"{target['name']}: {e}") + raise e # logger.error(f"{target['name']}: {e}") return inv @@ -132,8 +132,8 @@ def load_target(target: dict, classes_searchpath: str, ignore_class_notfound: bo "name": { "full": target_name, "parts": target_name.split("."), - "path": target_name, - "short": target_name, + "path": target_name.replace(".", "/"), + "short": target_name.split(".")[-1], } } @@ -144,7 +144,7 @@ def load_target(target: dict, classes_searchpath: str, ignore_class_notfound: bo target_config = OmegaConf.to_object(target_config) # target_config = OmegaConf.to_object(OmegaConf.create(OmegaConf.to_object(target_config))) except errors.OmegaConfBaseException as e: - raise InventoryError(e.__context__) + raise e # InventoryError(e.__context__) # obtain target name to insert in inv dict try: @@ -175,6 +175,11 @@ def merge(*args): return merge +def merge_replace(*args): + merge = OmegaConf.merge(*args, list_merge_mode=ListMergeMode.REPLACE) + return merge + + def to_dict(input): if not (isinstance(input, list) or OmegaConf.is_list(input)): @@ -206,6 +211,34 @@ def copy(component: str, new_name): return "${merge:${parameters.components." + component + "},${parameters." + new_name + "}}" +def helm_dep(name: str, source: str): + + return { + "type": "helm", + "output_path": f"components/charts/${{parameters.{name}.chart_name}}/${{parameters.{name}.chart_version}}/${{parameters.{name}.application_version}}", + "source": source, + "version": f"${{parameters.{name}.chart_version}}", + "chart_name": f"${{parameters.{name}.chart_name}}", + } + + +def helm_input(name: str): + + return { + "input_type": "helm", + "input_paths": [ + f"components/charts/${{parameters.{name}.chart_name}}/${{parameters.{name}.chart_version}}/${{parameters.{name}.application_version}}" + ], + "output_path": f"k8s/${{parameters.{name}.namespace}}", + "helm_params": { + "namespace": f"${{parameters.{name}.namespace}}", + "name": f"${{parameters.{name}.chart_name}}", + "output_file": f"{name}.yml", + }, + "helm_values": f"${{parameters.{name}.helm_values}}", + } + + def register_resolvers(): # utils OmegaConf.register_new_resolver("key", key) @@ -214,11 +247,14 @@ def register_resolvers(): # kapitan helpers OmegaConf.register_new_resolver("merge", merge) + OmegaConf.register_new_resolver("merge_replace", merge_replace) OmegaConf.register_new_resolver("dict", to_dict) OmegaConf.register_new_resolver("list", to_list) OmegaConf.register_new_resolver("namespace", namespace) OmegaConf.register_new_resolver("deployment", deployment) OmegaConf.register_new_resolver("copy", copy) + OmegaConf.register_new_resolver("helm_dep", helm_dep) + OmegaConf.register_new_resolver("helm_input", helm_input) # def deployment(component: str): diff --git a/kapitan/resources.py b/kapitan/resources.py index c6bbc320f..1ac92e492 100644 --- a/kapitan/resources.py +++ b/kapitan/resources.py @@ -320,7 +320,10 @@ def get_inventory(inventory_path, ignore_class_notfound=False, targets=[]): # get parsed args from cached.py args = list(cached.args.values())[0] use_omegaconf = args.omegaconf - migrate_omegaconf = args.migrate + try: + migrate_omegaconf = args.migrate + except: + ... if use_omegaconf: # show warning @@ -337,7 +340,7 @@ def get_inventory(inventory_path, ignore_class_notfound=False, targets=[]): try: inv = inventory_omegaconf(inventory_path, ignore_class_notfound, targets) except errors.OmegaConfBaseException as e: - raise InventoryError(e) + raise e # InventoryError(e) else: logger.debug("Using reclass as inventory backend") inv = inventory_reclass(inventory_path, ignore_class_notfound)