k8s_get_pod_logs
Fetch Kubernetes pod logs with options for tail lines, time range, and container selection to troubleshoot and monitor application performance.
Instructions
Fetch logs from a pod with options for tail lines, time range, and container selection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Pod name | |
| namespace | No | Kubernetes namespace (default: 'default') | |
| container | No | Container name (for multi-container pods) | |
| tail | No | Number of lines from the end (default: 100) | |
| since | No | Time duration (e.g., '5m', '1h', '2d') | |
| previous | No | Get logs from previous container instance |
Implementation Reference
- src/tools/kubernetes/logs.ts:3-45 (handler)The actual implementation of the tool that interacts with the Kubernetes API to fetch pod logs.
export async function getPodLogs(args: Record<string, unknown>): Promise<string> { const api = getCoreV1Api(); const namespace = (args.namespace as string) || "default"; const name = args.name as string; const container = args.container as string | undefined; const tail = (args.tail as number) || 100; const since = args.since as string | undefined; const previous = (args.previous as boolean) || false; if (!name) throw new Error("Pod name is required"); let sinceSeconds: number | undefined; if (since) { const match = since.match(/^(\d+)([smhd])$/); if (match) { const value = parseInt(match[1]); const unit = match[2]; const multipliers: Record<string, number> = { s: 1, m: 60, h: 3600, d: 86400 }; sinceSeconds = value * (multipliers[unit] || 1); } } const response = await api.readNamespacedPodLog( name, namespace, container, undefined, undefined, undefined, undefined, previous, sinceSeconds, tail ); const logs = response.body; if (!logs || logs.trim().length === 0) { return `No logs found for pod '${name}'${container ? ` container '${container}'` : ""} in namespace '${namespace}'.`; } const header = `Logs for pod '${name}'${container ? ` (container: ${container})` : ""} in '${namespace}' (last ${tail} lines):`; return `${header}\n\n${logs}`; } - src/tools/kubernetes/index.ts:48-62 (schema)Schema definition for k8s_get_pod_logs.
name: "k8s_get_pod_logs", description: "Fetch logs from a pod with options for tail lines, time range, and container selection", inputSchema: { type: "object" as const, properties: { name: { type: "string", description: "Pod name" }, namespace: { type: "string", description: "Kubernetes namespace (default: 'default')" }, container: { type: "string", description: "Container name (for multi-container pods)" }, tail: { type: "number", description: "Number of lines from the end (default: 100)" }, since: { type: "string", description: "Time duration (e.g., '5m', '1h', '2d')" }, previous: { type: "boolean", description: "Get logs from previous container instance" }, }, required: ["name"], }, }, - src/tools/kubernetes/index.ts:199-199 (registration)Tool registration/routing for k8s_get_pod_logs.
case "k8s_get_pod_logs": return getPodLogs(a);