Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pkg/kubernetes/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package kubernetes

import (
"context"
"fmt"
)

func (k *Kubernetes) NodeLog(ctx context.Context, name string, logPath string, tail int64) (string, error) {
// Use the node proxy API to access logs from the kubelet
// Common log paths:
// - /var/log/kubelet.log - kubelet logs
// - /var/log/kube-proxy.log - kube-proxy logs
// - /var/log/containers/ - container logs

if logPath == "" {
logPath = "kubelet.log"
}

// Build the URL for the node proxy logs endpoint
url := []string{"api", "v1", "nodes", name, "proxy", "logs", logPath}

// Query parameters for tail
params := make(map[string]string)
if tail > 0 {
params["tailLines"] = fmt.Sprintf("%d", tail)
}

req := k.manager.discoveryClient.RESTClient().
Get().
AbsPath(url...)

// Add tail parameter if specified
for key, value := range params {
req.Param(key, value)
}

result := req.Do(ctx)
if result.Error() != nil {
return "", fmt.Errorf("failed to get node logs: %w", result.Error())
}

rawData, err := result.Raw()
if err != nil {
return "", fmt.Errorf("failed to read node log response: %w", err)
}

return string(rawData), nil
}
66 changes: 66 additions & 0 deletions pkg/mcp/nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package mcp

import (
"strings"
"testing"

"github.com/mark3labs/mcp-go/mcp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNodeLog(t *testing.T) {
testCase(t, func(c *mcpContext) {
c.withEnvTest()

// Create test node
kubernetesAdmin := c.newKubernetesClient()
node := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test-node-log",
},
Status: corev1.NodeStatus{
Addresses: []corev1.NodeAddress{
{Type: corev1.NodeInternalIP, Address: "192.168.1.10"},
},
},
}

_, _ = kubernetesAdmin.CoreV1().Nodes().Create(c.ctx, node, metav1.CreateOptions{})

// Test node_log tool
toolResult, err := c.callTool("node_log", map[string]interface{}{
"name": "test-node-log",
})

t.Run("node_log returns successfully or with expected error", func(t *testing.T) {
if err != nil {
t.Fatalf("call tool failed: %v", err)
}
// Node logs might not be available in test environment
// We just check that the tool call completes
if toolResult.IsError {
content := toolResult.Content[0].(mcp.TextContent).Text
// Expected error messages in test environment
if !strings.Contains(content, "failed to get node logs") &&
!strings.Contains(content, "not logged any message yet") {
t.Logf("tool returned error (expected in test environment): %v", content)
}
}
})
})
}

func TestNodeLogMissingArguments(t *testing.T) {
testCase(t, func(c *mcpContext) {
c.withEnvTest()

t.Run("node_log requires name", func(t *testing.T) {
toolResult, err := c.callTool("node_log", map[string]interface{}{})

if err == nil && !toolResult.IsError {
t.Fatal("expected error when name is missing")
}
})
})
}
34 changes: 34 additions & 0 deletions pkg/mcp/testdata/toolsets-core-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@
},
"name": "namespaces_list"
},
{
"annotations": {
"title": "Node: Log",
"readOnlyHint": true,
"destructiveHint": false,
"idempotentHint": false,
"openWorldHint": true
},
"description": "Get logs from a Kubernetes node (kubelet, kube-proxy, or other system logs). This accesses node logs through the Kubernetes API proxy to the kubelet",
"inputSchema": {
"type": "object",
"properties": {
"log_path": {
"default": "kubelet.log",
"description": "Path to the log file on the node (e.g. 'kubelet.log', 'kube-proxy.log'). Default is 'kubelet.log'",
"type": "string"
},
"name": {
"description": "Name of the node to get logs from",
"type": "string"
},
"tail": {
"default": 100,
"description": "Number of lines to retrieve from the end of the logs (Optional, 0 means all logs)",
"minimum": 0,
"type": "integer"
}
},
"required": [
"name"
]
},
"name": "node_log"
},
{
"annotations": {
"title": "Pods: Delete",
Expand Down
42 changes: 42 additions & 0 deletions pkg/mcp/testdata/toolsets-full-tools-multicluster-enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,48 @@
},
"name": "namespaces_list"
},
{
"annotations": {
"title": "Node: Log",
"readOnlyHint": true,
"destructiveHint": false,
"idempotentHint": false,
"openWorldHint": true
},
"description": "Get logs from a Kubernetes node (kubelet, kube-proxy, or other system logs). This accesses node logs through the Kubernetes API proxy to the kubelet",
"inputSchema": {
"type": "object",
"properties": {
"context": {
"description": "Optional parameter selecting which context to run the tool in. Defaults to fake-context if not set",
"enum": [
"extra-cluster",
"fake-context"
],
"type": "string"
},
"log_path": {
"default": "kubelet.log",
"description": "Path to the log file on the node (e.g. 'kubelet.log', 'kube-proxy.log'). Default is 'kubelet.log'",
"type": "string"
},
"name": {
"description": "Name of the node to get logs from",
"type": "string"
},
"tail": {
"default": 100,
"description": "Number of lines to retrieve from the end of the logs (Optional, 0 means all logs)",
"minimum": 0,
"type": "integer"
}
},
"required": [
"name"
]
},
"name": "node_log"
},
{
"annotations": {
"title": "Pods: Delete",
Expand Down
38 changes: 38 additions & 0 deletions pkg/mcp/testdata/toolsets-full-tools-multicluster.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,44 @@
},
"name": "namespaces_list"
},
{
"annotations": {
"title": "Node: Log",
"readOnlyHint": true,
"destructiveHint": false,
"idempotentHint": false,
"openWorldHint": true
},
"description": "Get logs from a Kubernetes node (kubelet, kube-proxy, or other system logs). This accesses node logs through the Kubernetes API proxy to the kubelet",
"inputSchema": {
"type": "object",
"properties": {
"context": {
"description": "Optional parameter selecting which context to run the tool in. Defaults to fake-context if not set",
"type": "string"
},
"log_path": {
"default": "kubelet.log",
"description": "Path to the log file on the node (e.g. 'kubelet.log', 'kube-proxy.log'). Default is 'kubelet.log'",
"type": "string"
},
"name": {
"description": "Name of the node to get logs from",
"type": "string"
},
"tail": {
"default": 100,
"description": "Number of lines to retrieve from the end of the logs (Optional, 0 means all logs)",
"minimum": 0,
"type": "integer"
}
},
"required": [
"name"
]
},
"name": "node_log"
},
{
"annotations": {
"title": "Pods: Delete",
Expand Down
34 changes: 34 additions & 0 deletions pkg/mcp/testdata/toolsets-full-tools-openshift.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,40 @@
},
"name": "namespaces_list"
},
{
"annotations": {
"title": "Node: Log",
"readOnlyHint": true,
"destructiveHint": false,
"idempotentHint": false,
"openWorldHint": true
},
"description": "Get logs from a Kubernetes node (kubelet, kube-proxy, or other system logs). This accesses node logs through the Kubernetes API proxy to the kubelet",
"inputSchema": {
"type": "object",
"properties": {
"log_path": {
"default": "kubelet.log",
"description": "Path to the log file on the node (e.g. 'kubelet.log', 'kube-proxy.log'). Default is 'kubelet.log'",
"type": "string"
},
"name": {
"description": "Name of the node to get logs from",
"type": "string"
},
"tail": {
"default": 100,
"description": "Number of lines to retrieve from the end of the logs (Optional, 0 means all logs)",
"minimum": 0,
"type": "integer"
}
},
"required": [
"name"
]
},
"name": "node_log"
},
{
"annotations": {
"title": "Pods: Delete",
Expand Down
34 changes: 34 additions & 0 deletions pkg/mcp/testdata/toolsets-full-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,40 @@
},
"name": "namespaces_list"
},
{
"annotations": {
"title": "Node: Log",
"readOnlyHint": true,
"destructiveHint": false,
"idempotentHint": false,
"openWorldHint": true
},
"description": "Get logs from a Kubernetes node (kubelet, kube-proxy, or other system logs). This accesses node logs through the Kubernetes API proxy to the kubelet",
"inputSchema": {
"type": "object",
"properties": {
"log_path": {
"default": "kubelet.log",
"description": "Path to the log file on the node (e.g. 'kubelet.log', 'kube-proxy.log'). Default is 'kubelet.log'",
"type": "string"
},
"name": {
"description": "Name of the node to get logs from",
"type": "string"
},
"tail": {
"default": 100,
"description": "Number of lines to retrieve from the end of the logs (Optional, 0 means all logs)",
"minimum": 0,
"type": "integer"
}
},
"required": [
"name"
]
},
"name": "node_log"
},
{
"annotations": {
"title": "Pods: Delete",
Expand Down
Loading