k8s_get_pods
Retrieve and monitor Kubernetes pods by listing their status, restart counts, and age. Filter results using namespace, label selectors, or field selectors to manage container workloads.
Instructions
List pods in a Kubernetes namespace with their status, restarts, and age
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| namespace | No | Kubernetes namespace (default: 'default') | |
| label_selector | No | Label selector to filter pods (e.g., 'app=nginx') | |
| field_selector | No | Field selector (e.g., 'status.phase=Running') |
Implementation Reference
- src/tools/kubernetes/pods.ts:4-45 (handler)The actual logic that calls the Kubernetes API to get pods.
export async function getPods(args: Record<string, unknown>): Promise<string> { const api = getCoreV1Api(); const namespace = (args.namespace as string) || "default"; const labelSelector = args.label_selector as string | undefined; const fieldSelector = args.field_selector as string | undefined; const response = await api.listNamespacedPod( namespace, undefined, undefined, undefined, fieldSelector, labelSelector ); const pods = response.body.items; if (pods.length === 0) { return `No pods found in namespace '${namespace}'${labelSelector ? ` with selector '${labelSelector}'` : ""}.`; } const headers = ["NAME", "READY", "STATUS", "RESTARTS", "AGE"]; const rows = pods.map((pod) => { const containers = pod.status?.containerStatuses || []; const ready = containers.filter((c) => c.ready).length; const total = containers.length || pod.spec?.containers.length || 0; const restarts = containers.reduce((sum, c) => sum + (c.restartCount || 0), 0); const status = pod.status?.phase || "Unknown"; const age = pod.metadata?.creationTimestamp ? formatAge(pod.metadata.creationTimestamp) : "?"; return [ pod.metadata?.name || "unknown", `${ready}/${total}`, status, String(restarts), age, ]; }); return `Pods in namespace '${namespace}':\n\n${formatTable(headers, rows)}`; } - src/tools/kubernetes/index.ts:11-22 (registration)The tool definition (schema) for k8s_get_pods.
{ name: "k8s_get_pods", description: "List pods in a Kubernetes namespace with their status, restarts, and age", inputSchema: { type: "object" as const, properties: { namespace: { type: "string", description: "Kubernetes namespace (default: 'default')" }, label_selector: { type: "string", description: "Label selector to filter pods (e.g., 'app=nginx')" }, field_selector: { type: "string", description: "Field selector (e.g., 'status.phase=Running')" }, }, }, }, - src/tools/kubernetes/index.ts:196-196 (handler)The dispatcher logic inside handleKubernetesTool that routes the request to the getPods function.
case "k8s_get_pods": return getPods(a);