|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/strowk/mcp-k8s-go/internal/k8s" |
| 10 | + "github.com/strowk/mcp-k8s-go/internal/utils" |
| 11 | + |
| 12 | + "github.com/strowk/foxy-contexts/pkg/fxctx" |
| 13 | + "github.com/strowk/foxy-contexts/pkg/mcp" |
| 14 | + "github.com/strowk/foxy-contexts/pkg/toolinput" |
| 15 | + |
| 16 | + corev1 "k8s.io/api/core/v1" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "k8s.io/client-go/kubernetes/scheme" |
| 19 | + "k8s.io/client-go/rest" |
| 20 | + "k8s.io/client-go/tools/remotecommand" |
| 21 | +) |
| 22 | + |
| 23 | +func NewPodExecCommandTool(pool k8s.ClientPool) fxctx.Tool { |
| 24 | + k8sNamespace := "namespace" |
| 25 | + k8sPodName := "podName" |
| 26 | + execCommand := "command" |
| 27 | + k8sContext := "context" |
| 28 | + schema := toolinput.NewToolInputSchema( |
| 29 | + toolinput.WithRequiredString(k8sNamespace, "The name of the namespace where the pod to execute the command is located."), |
| 30 | + toolinput.WithRequiredString(k8sPodName, "The name of the pod in which the command needs to be executed."), |
| 31 | + toolinput.WithRequiredString(execCommand, "The command to be executed inside the pod."), |
| 32 | + toolinput.WithString(k8sContext, "Kubernetes context name."), |
| 33 | + ) |
| 34 | + return fxctx.NewTool( |
| 35 | + &mcp.Tool{ |
| 36 | + Name: "k8s-pod-exec", |
| 37 | + Description: utils.Ptr("Execute command in Kubernetes pod"), |
| 38 | + InputSchema: schema.GetMcpToolInputSchema(), |
| 39 | + }, |
| 40 | + func(args map[string]interface{}) *mcp.CallToolResult { |
| 41 | + input, err := schema.Validate(args) |
| 42 | + if err != nil { |
| 43 | + return errResponse(err) |
| 44 | + } |
| 45 | + k8sNamespace, err := input.String(k8sNamespace) |
| 46 | + if err != nil { |
| 47 | + return errResponse(fmt.Errorf("invalid input namespace: %w", err)) |
| 48 | + } |
| 49 | + k8sPodName, err := input.String(k8sPodName) |
| 50 | + if err != nil { |
| 51 | + return errResponse(fmt.Errorf("invalid input pod: %w", err)) |
| 52 | + } |
| 53 | + execCommand, err := input.String(execCommand) |
| 54 | + if err != nil { |
| 55 | + return errResponse(fmt.Errorf("invalid input command: %w", err)) |
| 56 | + } |
| 57 | + k8sContext := input.StringOr(k8sContext, "default") |
| 58 | + |
| 59 | + kubeconfig := k8s.GetKubeConfigForContext(k8sContext) |
| 60 | + config, err := kubeconfig.ClientConfig() |
| 61 | + if err != nil { |
| 62 | + return errResponse(fmt.Errorf("invalid config: %w", err)) |
| 63 | + } |
| 64 | + execResult, err := cmdExecuter(pool, config, k8sPodName, k8sNamespace, execCommand, k8sContext) |
| 65 | + if err != nil { |
| 66 | + return errResponse(fmt.Errorf("command execute failed: %w", err)) |
| 67 | + } |
| 68 | + |
| 69 | + var content mcp.TextContent |
| 70 | + contents := []interface{}{} |
| 71 | + content, err = NewJsonContent(execResult) |
| 72 | + if err != nil { |
| 73 | + return errResponse(err) |
| 74 | + } |
| 75 | + contents = append(contents, content) |
| 76 | + |
| 77 | + return &mcp.CallToolResult{ |
| 78 | + Meta: map[string]interface{}{}, |
| 79 | + Content: contents, |
| 80 | + IsError: utils.Ptr(false), |
| 81 | + } |
| 82 | + }, |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +type ExecResult struct { |
| 87 | + Stdout interface{} `json:"stdout"` |
| 88 | + Stderr interface{} `json:"stderr"` |
| 89 | +} |
| 90 | + |
| 91 | +func cmdExecuter(pool k8s.ClientPool, config *rest.Config, podName, namespace, cmd, k8sContext string) (ExecResult, error) { |
| 92 | + execResult := ExecResult{} |
| 93 | + clientset, err := pool.GetClientset(k8sContext) |
| 94 | + if err != nil { |
| 95 | + return execResult, err |
| 96 | + } |
| 97 | + |
| 98 | + pod, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) |
| 99 | + if err != nil { |
| 100 | + return execResult, err |
| 101 | + } |
| 102 | + |
| 103 | + if len(pod.Spec.Containers) == 0 { |
| 104 | + return execResult, fmt.Errorf("Pod %s has no containers", podName) |
| 105 | + } |
| 106 | + |
| 107 | + containerName := pod.Spec.Containers[0].Name |
| 108 | + req := clientset.CoreV1().RESTClient().Post(). |
| 109 | + Resource("pods"). |
| 110 | + Name(podName). |
| 111 | + Namespace(namespace). |
| 112 | + SubResource("exec"). |
| 113 | + VersionedParams(&corev1.PodExecOptions{ |
| 114 | + Container: containerName, |
| 115 | + Command: []string{"sh", "-c", cmd}, |
| 116 | + Stdin: true, |
| 117 | + Stdout: true, |
| 118 | + Stderr: true, |
| 119 | + TTY: false, |
| 120 | + }, scheme.ParameterCodec) |
| 121 | + executor, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL()) |
| 122 | + if err != nil { |
| 123 | + return execResult, err |
| 124 | + } |
| 125 | + var stdout, stderr bytes.Buffer |
| 126 | + if err = executor.Stream(remotecommand.StreamOptions{ |
| 127 | + Stdin: strings.NewReader(""), |
| 128 | + Stdout: &stdout, |
| 129 | + Stderr: &stderr, |
| 130 | + }); err != nil { |
| 131 | + return execResult, err |
| 132 | + } |
| 133 | + execResult.Stdout = stdout.String() |
| 134 | + execResult.Stderr = stderr.String() |
| 135 | + return execResult, nil |
| 136 | +} |
0 commit comments