diff --git a/kapitan/targets.py b/kapitan/targets.py index 8e1388029..c06485f36 100644 --- a/kapitan/targets.py +++ b/kapitan/targets.py @@ -445,6 +445,7 @@ def compile_target(target_obj, search_paths, compile_path, ref_controller, globa input_type = comp_obj["input_type"] output_path = comp_obj["output_path"] input_params = comp_obj.setdefault("input_params", {}) + continue_on_compile_error = comp_obj.get("continue_on_compile_error", False) if input_type == "jinja2": input_compiler = Jinja2(compile_path, search_paths, ref_controller, comp_obj) @@ -470,7 +471,14 @@ def compile_target(target_obj, search_paths, compile_path, ref_controller, globa raise CompileError(err_msg.format(input_type)) input_compiler.make_compile_dirs(target_name, output_path, **kwargs) - input_compiler.compile_obj(comp_obj, ext_vars, **kwargs) + try: + input_compiler.compile_obj(comp_obj, ext_vars, **kwargs) + except Exception as e: + if continue_on_compile_error: + logger.error("Error compiling %s: %s", target_name, e) + continue + else: + raise CompileError(f"Error compiling {target_name}: {e}") logger.info("Compiled %s (%.2fs)", target_obj["target_full_path"], time.time() - start) @@ -559,6 +567,7 @@ def valid_target_obj(target_obj, require_compile=True): "name": {"type": "string"}, "input_paths": {"type": "array"}, "input_type": {"type": "string"}, + "continue_on_compile_error": {"type": "boolean"}, "output_path": {"type": "string"}, "output_type": {"type": "string"}, "helm_values": {"type": "object"}, diff --git a/tests/test_compile.py b/tests/test_compile.py index 4d8975e74..11bb5e716 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -74,7 +74,18 @@ def tearDown(self): os.chdir(os.getcwd() + "/../../") reset_cache() +class FailCompileTestResourcesTestKadet(unittest.TestCase): + def setUp(self): + os.chdir(os.getcwd() + "/tests/test_resources/") + + def test_compile(self): + sys.argv = ["kapitan", "compile", "-t", "fail-compile"] + main() + def tearDown(self): + os.chdir(os.getcwd() + "/../../") + reset_cache() + class CompileTestResourcesTestJinja2InputParams(unittest.TestCase): def setUp(self): os.chdir(os.getcwd() + "/tests/test_resources/") diff --git a/tests/test_resources/inventory/targets/fail-compile.yml b/tests/test_resources/inventory/targets/fail-compile.yml new file mode 100644 index 000000000..0b41d4777 --- /dev/null +++ b/tests/test_resources/inventory/targets/fail-compile.yml @@ -0,0 +1,14 @@ +parameters: + kapitan: + vars: + target: fail-compile + compile: + - name: fail_compile + input_type: kadet + output_path: file_compile + continue_on_compile_error: True + input_paths: + - kadet_functions/fail_compile + + + diff --git a/tests/test_resources/kadet_functions/fail_compile/__init__.py b/tests/test_resources/kadet_functions/fail_compile/__init__.py new file mode 100644 index 000000000..726d1561a --- /dev/null +++ b/tests/test_resources/kadet_functions/fail_compile/__init__.py @@ -0,0 +1,5 @@ +from kapitan.inputs import kadet + + +def main(input_params): + raise ValueError("This function will fail to compile")