Skip to content

Commit 50d809e

Browse files
committed
fixed grouping
1 parent 0ee1c88 commit 50d809e

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

robusta_krr/core/integrations/kubernetes/__init__.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -506,54 +506,40 @@ async def _list_all_groupedjobs(self) -> list[K8sObjectData]:
506506
namespaced_request=self.batch.list_namespaced_job,
507507
)
508508

509-
# Group jobs by individual grouping label values AND namespace (OR logic)
510509
grouped_jobs = defaultdict(list)
511510
for job in all_jobs:
512511
if (job.metadata.labels and
513512
not any(owner.kind == "CronJob" for owner in job.metadata.owner_references or [])):
514513

515-
# Check if job has any of the grouping labels
516514
for label_name in settings.job_grouping_labels:
517515
if label_name in job.metadata.labels:
518516
label_value = job.metadata.labels[label_name]
519-
group_key = f"{job.metadata.namespace}/{label_name}={label_value}"
517+
group_key = f"{label_name}={label_value}"
520518
grouped_jobs[group_key].append(job)
521519

522-
# Create GroupedJob objects
523520
result = []
524521
for group_name, jobs in grouped_jobs.items():
525-
# Use the first job as the template for the group
526-
template_job = jobs[0]
522+
jobs_by_namespace = defaultdict(list)
523+
for job in jobs:
524+
jobs_by_namespace[job.metadata.namespace].append(job)
527525

528-
# Create a virtual container that represents the group
529-
# We'll use the first job's container as the template
530-
template_container = template_job.spec.template.spec.containers[0]
531-
532-
# Create the GroupedJob object
533-
grouped_job = self.__build_scannable_object(
534-
item=template_job,
535-
container=template_container,
536-
kind="GroupedJob"
537-
)
538-
539-
# Override the name to be the group name
540-
grouped_job.name = group_name
541-
grouped_job.namespace = template_job.metadata.namespace
542-
543-
# Store all jobs in the group for later pod listing
544-
grouped_job._api_resource._grouped_jobs = jobs
545-
546-
# Store the label+value filter for pod listing
547-
# Extract the label+value pair from the group name
548-
grouped_job._api_resource._label_filters = []
549-
# The group name is in format "namespace/label_name=label_value"
550-
# Extract just the label=value part for the selector
551-
if "/" in group_name and "=" in group_name:
552-
# Split by "/" and take everything after the first "/"
553-
namespace_part, label_part = group_name.split("/", 1)
554-
grouped_job._api_resource._label_filters.append(label_part)
555-
556-
result.append(grouped_job)
526+
for namespace, namespace_jobs in jobs_by_namespace.items():
527+
template_job = namespace_jobs[0]
528+
template_container = template_job.spec.template.spec.containers[0]
529+
530+
grouped_job = self.__build_scannable_object(
531+
item=template_job,
532+
container=template_container,
533+
kind="GroupedJob"
534+
)
535+
536+
grouped_job.name = group_name
537+
grouped_job.namespace = namespace
538+
grouped_job._api_resource._grouped_jobs = namespace_jobs
539+
grouped_job._api_resource._label_filters = []
540+
grouped_job._api_resource._label_filters.append(group_name)
541+
542+
result.append(grouped_job)
557543

558544
logger.debug(f"Found {len(result)} GroupedJob groups")
559545
return result

0 commit comments

Comments
 (0)