Skip to content

Commit ecb7730

Browse files
author
Matteo Voges
committed
feat: enable multiprocessing on compile object level
1 parent d67e907 commit ecb7730

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
for_testing

examples/kubernetes/inventory/targets/removal.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ parameters:
99
- copy_target
1010
output_path: .
1111
# test removal of a file
12-
- input_type: remove
13-
input_paths:
14-
- compiled/${kapitan:vars:target}/copy_target
15-
output_path: .
12+
# - input_type: remove
13+
# input_paths:
14+
# - compiled/${kapitan:vars:target}/copy_target
15+
# output_path: .

kapitan/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def trigger_compile(args):
9494
verbose=hasattr(args, "verbose") and args.verbose,
9595
use_go_jsonnet=args.use_go_jsonnet,
9696
compose_node_name=args.compose_node_name,
97+
multiprocess_objects=args.multiprocess_objects,
9798
)
9899

99100

@@ -305,6 +306,12 @@ def build_parser():
305306
action="store_true",
306307
help="dumps all none-type entries as empty, default is dumping as 'null'",
307308
)
309+
compile_parser.add_argument(
310+
"--multiprocess-objects",
311+
default=from_dot_kapitan("compile", "multiprocess-objects", False),
312+
action="store_true",
313+
help="compute compile objects in parallel, default is 'false'",
314+
)
308315

309316
compile_selector_parser = compile_parser.add_mutually_exclusive_group()
310317
compile_selector_parser.add_argument(

kapitan/targets.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ def compile_targets(
151151

152152
logger.info("Rendered inventory (%.2fs)", time.time() - rendering_start)
153153

154+
if kwargs.get("multiprocess_objects"):
155+
# append target information to compile objects
156+
new_target_objs = []
157+
for target_obj in target_objs:
158+
compile_objs = target_obj["compile"]
159+
objects_length = len(compile_objs)
160+
161+
for id, compile_obj in enumerate(compile_objs):
162+
generated_target_obj = target_obj.copy()
163+
generated_target_obj["compile"] = [compile_obj]
164+
generated_target_obj["id"] = id
165+
generated_target_obj["max_id"] = objects_length - 1
166+
new_target_objs.append(generated_target_obj)
167+
168+
target_objs = new_target_objs
169+
170+
compile_start = time.time()
171+
154172
worker = partial(
155173
compile_target,
156174
search_paths=search_paths,
@@ -165,6 +183,9 @@ def compile_targets(
165183
# so p is only not None when raising an exception
166184
[p.get() for p in pool.imap_unordered(worker, target_objs) if p]
167185

186+
if kwargs.get("multiprocess_objects"):
187+
logger.info(f"\nCompiled {len(target_objs)} compile targets ({time.time() - compile_start:.2f}s)")
188+
168189
os.makedirs(compile_path, exist_ok=True)
169190

170191
# if '-t' is set on compile or only a few changed, only override selected targets
@@ -500,6 +521,16 @@ def compile_target(target_obj, search_paths, compile_path, ref_controller, globa
500521
input_compiler.make_compile_dirs(target_name, output_path, **kwargs)
501522
input_compiler.compile_obj(comp_obj, ext_vars, **kwargs)
502523

524+
if kwargs.get("multiprocess_objects"):
525+
current_id = target_obj["id"]
526+
max_id = target_obj["max_id"]
527+
# contains only one element
528+
if current_id != max_id:
529+
logger.debug(f"Compiled {target_obj['target_full_path']} - {compile_objs[0]['input_type']} ({current_id} / {max_id})")
530+
else:
531+
logger.info(f"Compiled {target_obj['target_full_path']}")
532+
return
533+
503534
logger.info("Compiled %s (%.2fs)", target_obj["target_full_path"], time.time() - start)
504535

505536

0 commit comments

Comments
 (0)