Skip to content

Commit 4104fa1

Browse files
authored
action to list pods by name and namespace filters (#1867)
1 parent ef8df81 commit 4104fa1

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

helm/robusta/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ lightActions:
9191
- holmes_issue_chat
9292
- holmes_chat
9393
- holmes_workload_chat
94+
- list_pods
9495

9596
# install prometheus, alert-manager, and grafana along with Robusta?
9697
enablePrometheusStack: false

playbooks/robusta_playbooks/k8s_resource_enrichments.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,62 @@ def status_enricher(event: KubernetesResourceEvent, params: StatusEnricherParams
380380
),
381381
]
382382
)
383+
384+
385+
class ListPodsParams(ActionParams):
386+
"""
387+
:var name: Filter pods with name that contains this paramater
388+
:var namespace: Filter pods in this namespace. If ommitted, pods from all namespaces are returned
389+
:var limit: Max number of pods to return
390+
"""
391+
392+
name: str
393+
namespace: Optional[str] = None
394+
limit: int = 50
395+
396+
397+
@action
398+
def list_pods(event: ExecutionBaseEvent, params: ListPodsParams):
399+
"""
400+
List pods by name, and potentially namespace
401+
"""
402+
cluster = event.get_context().cluster_name
403+
filtered_pods = []
404+
continue_token = None
405+
batch_size = 300 # Load pods in batches
406+
407+
# Keep fetching until we have enough matching pods or no more pods
408+
while len(filtered_pods) < params.limit:
409+
if params.namespace:
410+
pod_list = client.CoreV1Api().list_namespaced_pod(
411+
namespace=params.namespace,
412+
limit=batch_size,
413+
_continue=continue_token
414+
)
415+
else:
416+
pod_list = client.CoreV1Api().list_pod_for_all_namespaces(
417+
limit=batch_size,
418+
_continue=continue_token
419+
)
420+
# Filter pods by name from current batch
421+
batch_filtered = [
422+
pod for pod in pod_list.items
423+
if params.name.lower() in pod.metadata.name.lower()
424+
]
425+
filtered_pods.extend(batch_filtered)
426+
427+
# Check if we have more pods to fetch
428+
continue_token = getattr(pod_list.metadata, 'continue', None)
429+
if not continue_token:
430+
break
431+
432+
# Apply final limit
433+
limited_pods = filtered_pods[:params.limit]
434+
435+
# Convert to RelatedPod format (same as related_pods action)
436+
pod_objects = [to_pod_obj(pod, cluster, include_raw_data=False) for pod in limited_pods]
437+
438+
# Return as JSON
439+
event.add_enrichment([
440+
JsonBlock(json.dumps([pod.dict() for pod in pod_objects], default=str))
441+
])

0 commit comments

Comments
 (0)