Skip to content

Commit

Permalink
Merge pull request #1168 from kapicorp/test/continue-on-error
Browse files Browse the repository at this point in the history
Continue on compile error
  • Loading branch information
ademariag authored Apr 11, 2024
2 parents 3d31f5f + 46a496f commit 17d998f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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"},
Expand Down
11 changes: 11 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
14 changes: 14 additions & 0 deletions tests/test_resources/inventory/targets/fail-compile.yml
Original file line number Diff line number Diff line change
@@ -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



5 changes: 5 additions & 0 deletions tests/test_resources/kadet_functions/fail_compile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from kapitan.inputs import kadet


def main(input_params):
raise ValueError("This function will fail to compile")

0 comments on commit 17d998f

Please sign in to comment.