Skip to content

Commit ec82de1

Browse files
authored
feat: add optional container parameter for pods_log tool
1 parent 705a8fe commit ec82de1

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

pkg/kubernetes/pods.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ func (k *Kubernetes) PodsDelete(ctx context.Context, namespace, name string) (st
7777
k.clientSet.CoreV1().Pods(namespace).Delete(ctx, name, metav1.DeleteOptions{})
7878
}
7979

80-
func (k *Kubernetes) PodsLog(ctx context.Context, namespace, name string) (string, error) {
80+
func (k *Kubernetes) PodsLog(ctx context.Context, namespace, name, container string) (string, error) {
8181
tailLines := int64(256)
8282
req := k.clientSet.CoreV1().Pods(namespaceOrDefault(namespace)).GetLogs(name, &v1.PodLogOptions{
8383
TailLines: &tailLines,
84+
Container: container,
8485
})
8586
res := req.Do(ctx)
8687
if res.Error() != nil {

pkg/mcp/pods.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (s *Server) initPods() []server.ServerTool {
4949
mcp.WithDescription("Get the logs of a Kubernetes Pod in the current or provided namespace with the provided name"),
5050
mcp.WithString("namespace", mcp.Description("Namespace to get the Pod logs from")),
5151
mcp.WithString("name", mcp.Description("Name of the Pod to get the logs from"), mcp.Required()),
52+
mcp.WithString("container", mcp.Description("Name of the Pod container to get the logs from (Optional)")),
5253
), s.podsLog},
5354
{mcp.NewTool("pods_run",
5455
mcp.WithDescription("Run a Kubernetes Pod in the current or provided namespace with the provided container image and optional name"),
@@ -150,7 +151,11 @@ func (s *Server) podsLog(ctx context.Context, ctr mcp.CallToolRequest) (*mcp.Cal
150151
if name == nil {
151152
return NewTextResult("", errors.New("failed to get pod log, missing argument name")), nil
152153
}
153-
ret, err := s.k.PodsLog(ctx, ns.(string), name.(string))
154+
container := ctr.Params.Arguments["container"]
155+
if container == nil {
156+
container = ""
157+
}
158+
ret, err := s.k.PodsLog(ctx, ns.(string), name.(string), container.(string))
154159
if err != nil {
155160
return NewTextResult("", fmt.Errorf("failed to get pod %s log in namespace %s: %v", name, ns, err)), nil
156161
} else if ret == "" {

pkg/mcp/pods_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,36 @@ func TestPodsLog(t *testing.T) {
526526
return
527527
}
528528
})
529+
podsContainerLogInNamespace, err := c.callTool("pods_log", map[string]interface{}{
530+
"namespace": "ns-1",
531+
"name": "a-pod-in-ns-1",
532+
"container": "nginx",
533+
})
534+
t.Run("pods_log with name, container and namespace returns pod log", func(t *testing.T) {
535+
if err != nil {
536+
t.Fatalf("call tool failed %v", err)
537+
return
538+
}
539+
if podsContainerLogInNamespace.IsError {
540+
t.Fatalf("call tool failed")
541+
return
542+
}
543+
})
544+
toolResult, err := c.callTool("pods_log", map[string]interface{}{
545+
"namespace": "ns-1",
546+
"name": "a-pod-in-ns-1",
547+
"container": "a-not-existing-container",
548+
})
549+
t.Run("pods_log with non existing container returns error", func(t *testing.T) {
550+
if toolResult.IsError != true {
551+
t.Fatalf("call tool should fail")
552+
return
553+
}
554+
if toolResult.Content[0].(mcp.TextContent).Text != "failed to get pod a-pod-in-ns-1 log in namespace ns-1: container a-not-existing-container is not valid for pod a-pod-in-ns-1" {
555+
t.Fatalf("invalid error message, got %v", toolResult.Content[0].(mcp.TextContent).Text)
556+
return
557+
}
558+
})
529559
})
530560
}
531561

0 commit comments

Comments
 (0)