Skip to content

Commit 9599fbc

Browse files
committed
feat(pods): add tailLines parameter to pod logs retrieval with default 256 lines
Signed-off-by: iamsudip <[email protected]>
1 parent 5b33e1a commit 9599fbc

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

pkg/kubernetes/pods.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,27 @@ func (k *Kubernetes) PodsDelete(ctx context.Context, namespace, name string) (st
9292
k.ResourcesDelete(ctx, &schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}, namespace, name)
9393
}
9494

95-
func (k *Kubernetes) PodsLog(ctx context.Context, namespace, name, container string, previous bool) (string, error) {
96-
tailLines := int64(256)
95+
func (k *Kubernetes) PodsLog(ctx context.Context, namespace, name, container string, previous bool, tailLines int64) (string, error) {
9796
pods, err := k.manager.accessControlClientSet.Pods(k.NamespaceOrDefault(namespace))
9897
if err != nil {
9998
return "", err
10099
}
101-
req := pods.GetLogs(name, &v1.PodLogOptions{
102-
TailLines: &tailLines,
100+
101+
logOptions := &v1.PodLogOptions{
103102
Container: container,
104103
Previous: previous,
105-
})
104+
}
105+
106+
// Only set tailLines if a value is provided (non-zero)
107+
if tailLines > 0 {
108+
logOptions.TailLines = &tailLines
109+
} else {
110+
// Default to 256 lines when not specified
111+
defaultLines := int64(256)
112+
logOptions.TailLines = &defaultLines
113+
}
114+
115+
req := pods.GetLogs(name, logOptions)
106116
res := req.Do(ctx)
107117
if res.Error() != nil {
108118
return "", res.Error()

pkg/mcp/pods_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,41 @@ func TestPodsLog(t *testing.T) {
756756
return
757757
}
758758
})
759+
760+
// Test with tailLines parameter
761+
podsTailLines, err := c.callTool("pods_log", map[string]interface{}{
762+
"namespace": "ns-1",
763+
"name": "a-pod-in-ns-1",
764+
"tailLines": 100,
765+
})
766+
t.Run("pods_log with tailLines=100 returns pod log", func(t *testing.T) {
767+
if err != nil {
768+
t.Fatalf("call tool failed %v", err)
769+
return
770+
}
771+
if podsTailLines.IsError {
772+
t.Fatalf("call tool failed")
773+
return
774+
}
775+
})
776+
777+
// Test with invalid tailLines parameter
778+
podsInvalidTailLines, _ := c.callTool("pods_log", map[string]interface{}{
779+
"namespace": "ns-1",
780+
"name": "a-pod-in-ns-1",
781+
"tailLines": "invalid",
782+
})
783+
t.Run("pods_log with invalid tailLines returns error", func(t *testing.T) {
784+
if !podsInvalidTailLines.IsError {
785+
t.Fatalf("call tool should fail")
786+
return
787+
}
788+
expectedErrorMsg := "failed to parse tailLines parameter: expected integer"
789+
if errMsg := podsInvalidTailLines.Content[0].(mcp.TextContent).Text; !strings.Contains(errMsg, expectedErrorMsg) {
790+
t.Fatalf("unexpected error message, expected to contain '%s', got '%s'", expectedErrorMsg, errMsg)
791+
return
792+
}
793+
})
759794
})
760795
}
761796

pkg/toolsets/core/pods.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ func initPods() []api.ServerTool {
201201
Type: "string",
202202
Description: "Name of the Pod container to get the logs from (Optional)",
203203
},
204+
"tailLines": {
205+
Type: "integer",
206+
Description: "Number of lines to retrieve from the end of the logs (Optional, default: 256)",
207+
Default: api.ToRawMessage(int64(256)),
208+
Minimum: ptr.To(float64(0)),
209+
},
204210
"previous": {
205211
Type: "boolean",
206212
Description: "Return previous terminated container logs (Optional)",
@@ -396,7 +402,24 @@ func podsLog(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
396402
if previous != nil {
397403
previousBool = previous.(bool)
398404
}
399-
ret, err := params.PodsLog(params, ns.(string), name.(string), container.(string), previousBool)
405+
// Extract tailLines parameter
406+
tailLines := params.GetArguments()["tailLines"]
407+
var tailLinesInt int64
408+
if tailLines != nil {
409+
// Convert to int64 - safely handle both float64 (JSON number) and int types
410+
switch v := tailLines.(type) {
411+
case float64:
412+
tailLinesInt = int64(v)
413+
case int:
414+
tailLinesInt = int64(v)
415+
case int64:
416+
tailLinesInt = v
417+
default:
418+
return api.NewToolCallResult("", fmt.Errorf("failed to parse tailLines parameter: expected integer, got %T", tailLines)), nil
419+
}
420+
}
421+
422+
ret, err := params.PodsLog(params.Context, ns.(string), name.(string), container.(string), previousBool, tailLinesInt)
400423
if err != nil {
401424
return api.NewToolCallResult("", fmt.Errorf("failed to get pod %s log in namespace %s: %v", name, ns, err)), nil
402425
} else if ret == "" {

0 commit comments

Comments
 (0)