From b29c32a7e384c2bb7baafedf84d2945312b62271 Mon Sep 17 00:00:00 2001 From: Gerard Nguyen Date: Thu, 17 Oct 2024 11:29:04 +1100 Subject: [PATCH] add filter for pod list --- pkg/api/server.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/api/server.go b/pkg/api/server.go index 15a3a8c..a5a1ee4 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -1465,6 +1465,15 @@ func filterObjectsByFields(object runtime.Object, selector fields.Selector) runt } } return filtered + case *corev1.PodList: + filtered := &corev1.PodList{} + for _, item := range o.Items { + item := item + if selector.Matches(podToSelectableFields(&item)) { + filtered.Items = append(filtered.Items, *item.DeepCopy()) + } + } + return filtered default: // TODO: do more } @@ -1496,6 +1505,16 @@ func eventToSelectableFields(event *corev1.Event) fields.Set { return generic.MergeFieldsSets(specificFieldsSet, objectMetaFieldsSet) } +// podToSelectableFields extracts fields from a Pod object to be used for selection or filtering +func podToSelectableFields(pod *corev1.Pod) fields.Set { + objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&pod.ObjectMeta, true) + specificFieldsSet := fields.Set{ + "spec.nodeName": pod.Spec.NodeName, + "status.phase": string(pod.Status.Phase), + } + return generic.MergeFieldsSets(specificFieldsSet, objectMetaFieldsSet) +} + func toTable(object runtime.Object, r *http.Request) (runtime.Object, error) { switch o := object.(type) { case *corev1.PodList: