From 4c3183679872c3a6528b48754c79b32a802f7884 Mon Sep 17 00:00:00 2001 From: Bruno Almeida Date: Fri, 23 May 2025 10:13:17 -0300 Subject: [PATCH] feat: add global_target parameter for selective global generator execution Add support for `global_target` parameter in inventory to allow targets to selectively opt into global generator execution from specific coordinators. **Behavior:** - When `global_target` is not set: target participates in all global generators (backward compatible) - When `global_target` is set: target only participates in global generators from the specified coordinator target - Current executing target always processes its own inventory regardless of global_target settings **Use Case:** Enables fine-grained control over cross-target resource generation, allowing different targets to be coordinated by different "coordinator" targets while maintaining full backward compatibility. **Example:** ```yaml parameters: argocd: app_of_apps: true parameters: global_target: argocd-prod generators: argocd: applications: prod: ... ``` --- lib/kgenlib/__init__.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/kgenlib/__init__.py b/lib/kgenlib/__init__.py index 8e70be4..3f9651a 100644 --- a/lib/kgenlib/__init__.py +++ b/lib/kgenlib/__init__.py @@ -732,13 +732,36 @@ def _run_global_generator(self, func: Callable, params: GeneratorParams) -> None logger.debug( f"Running global generator {func.__name__} with activation path {activation_path}" ) - for _, inventory in self.global_inventory.items(): - self.expand_and_run( - generator_function=func, - generator_params=params, - inventory=inventory, - ) + + # Get the current target name for global_target filtering + current_target_name = current_target.get() + + for target_name, inventory in self.global_inventory.items(): + + # For other targets, check global_target parameter + inventory_global_target = findpath(inventory.parameters, "global_target") + + # Process if: global_target not set/empty (backward compatibility) + # OR global_target matches current target + # OR it's the current target + if (not inventory_global_target + or inventory_global_target == current_target_name + or target_name == current_target_name): + logger.debug( + f"Processing inventory {target_name} " + f"(global_target={inventory_global_target or 'not set'})" + ) + self.expand_and_run( + generator_function=func, + generator_params=params, + inventory=inventory, + ) + else: + logger.debug( + f"Skipping inventory {target_name} (global_target={inventory_global_target}) " + f"- not listening to current target {current_target_name}" + ) else: logger.debug( f"Skipping global generator {func.__name__} with params {params}" - ) + ) \ No newline at end of file