Skip to content

Commit ff07962

Browse files
committed
refactor: optimize getPodsWithIntersectionLabels to use a single label selector for intersection matching
Signed-off-by: XploY04 <[email protected]>
1 parent f6ba5cb commit ff07962

File tree

1 file changed

+14
-49
lines changed

1 file changed

+14
-49
lines changed

pkg/utils/common/pods.go

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -355,65 +355,30 @@ func getPodsWithIntersectionLabels(target types.AppDetails, clients clients.Clie
355355
}
356356
}
357357

358-
// Start with pods matching first label
359-
pods, err := clients.KubeClient.CoreV1().Pods(target.Namespace).List(context.Background(), v1.ListOptions{LabelSelector: target.Labels[0]})
358+
// Build comma-separated label selector for intersection (AND logic)
359+
// e.g., "app=nginx,env=prod,role=primary"
360+
labelSelector := strings.Join(target.Labels, ",")
361+
362+
pods, err := clients.KubeClient.CoreV1().Pods(target.Namespace).List(context.Background(), v1.ListOptions{
363+
LabelSelector: labelSelector,
364+
})
360365
if err != nil {
361366
return nil, cerrors.Error{
362367
ErrorCode: cerrors.ErrorTypeTargetSelection,
363-
Target: fmt.Sprintf("{podLabel: %s, namespace: %s}", target.Labels[0], target.Namespace),
368+
Target: fmt.Sprintf("{labels: %v, namespace: %s}", target.Labels, target.Namespace),
364369
Reason: err.Error(),
365370
}
366371
}
367372

368-
// If only one label, return the pods
369-
if len(target.Labels) == 1 {
370-
return pods.Items, nil
371-
}
372-
373-
// Create map for fast lookup using pod name as key
374-
podMap := make(map[string]core_v1.Pod)
375-
for _, pod := range pods.Items {
376-
podMap[pod.Name] = pod
377-
}
378-
379-
// Intersect with each subsequent label
380-
for i := 1; i < len(target.Labels); i++ {
381-
label := target.Labels[i]
382-
pods, err := clients.KubeClient.CoreV1().Pods(target.Namespace).List(context.Background(), v1.ListOptions{LabelSelector: label})
383-
if err != nil {
384-
return nil, cerrors.Error{
385-
ErrorCode: cerrors.ErrorTypeTargetSelection,
386-
Target: fmt.Sprintf("{podLabel: %s, namespace: %s}", label, target.Namespace),
387-
Reason: err.Error(),
388-
}
389-
}
390-
391-
// Keep only pods that exist in both the current map and the new query result
392-
newPodMap := make(map[string]core_v1.Pod)
393-
for _, pod := range pods.Items {
394-
if _, exists := podMap[pod.Name]; exists {
395-
newPodMap[pod.Name] = pod
396-
}
397-
}
398-
podMap = newPodMap
399-
400-
// Early exit if no intersection found
401-
if len(podMap) == 0 {
402-
return nil, cerrors.Error{
403-
ErrorCode: cerrors.ErrorTypeTargetSelection,
404-
Target: fmt.Sprintf("{labels: %v, namespace: %s}", target.Labels, target.Namespace),
405-
Reason: fmt.Sprintf("no pods found matching all labels after checking: %s", label),
406-
}
373+
if len(pods.Items) == 0 {
374+
return nil, cerrors.Error{
375+
ErrorCode: cerrors.ErrorTypeTargetSelection,
376+
Target: fmt.Sprintf("{labels: %v, namespace: %s}", target.Labels, target.Namespace),
377+
Reason: "no pods found matching all labels",
407378
}
408379
}
409380

410-
// Convert map back to slice
411-
var result []core_v1.Pod
412-
for _, pod := range podMap {
413-
result = append(result, pod)
414-
}
415-
416-
return result, nil
381+
return pods.Items, nil
417382
}
418383

419384
func filterPodsByOwnerKind(pods []core_v1.Pod, target types.AppDetails, clients clients.ClientSets) ([]core_v1.Pod, error) {

0 commit comments

Comments
 (0)