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
6 changes: 3 additions & 3 deletions internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,10 @@ func (c *Client) GetCRD(ctx context.Context, kind, namespace, name string) (*uns
if err != nil {
return nil, err
}
if k.Namespaced && namespace == "" {
return nil, fmt.Errorf("%s is namespace-scoped: namespace is required", kind)
}
if k.Namespaced {
if namespace == "" {
return nil, fmt.Errorf("%s is namespace-scoped: namespace is required", kind)
}
if err := c.checkNamespace(namespace); err != nil {
return nil, err
}
Expand Down
20 changes: 8 additions & 12 deletions internal/panel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ const (
// Client is the per-user reader. It holds the panel base URL, the MCP token
// and a per-process session ID.
type Client struct {
base *url.URL
token string
session string
hc *http.Client
fanout int
perCluster time.Duration
base *url.URL
token string
session string
hc *http.Client
}

// kindInfo maps a CRD kind onto its panel API path.
Expand Down Expand Up @@ -135,12 +133,10 @@ func New(baseURL, token, caFile string) (*Client, error) {
}

return &Client{
base: u,
token: token,
session: base64.RawURLEncoding.EncodeToString(raw),
hc: httpClient,
fanout: defaultFanout,
perCluster: defaultPerCluster,
base: u,
token: token,
session: base64.RawURLEncoding.EncodeToString(raw),
hc: httpClient,
}, nil
}

Expand Down
25 changes: 6 additions & 19 deletions internal/panel/fanout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package panel

import (
"context"
"errors"
"net/http"
"strings"
"sync"
Expand All @@ -19,8 +18,8 @@ type clusterResult[T any] struct {
val T
}

// fanOut runs fn once per registered cluster, at most c.fanout at a time, each
// under its own c.perCluster timeout. It returns the successful results and the
// fanOut runs fn once per registered cluster, at most defaultFanout at a time, each
// under its own defaultPerCluster timeout. It returns the successful results and the
// IDs of clusters that failed for any reason other than 403 or 404; those two
// are skipped silently. An error from /clusters itself is returned.
func fanOut[T any](ctx context.Context, c *Client, fn func(context.Context) (T, error)) ([]clusterResult[T], []string, error) {
Expand All @@ -45,18 +44,9 @@ func fanOut[T any](ctx context.Context, c *Client, fn func(context.Context) (T,
ids = append(ids, id)
}

width := c.fanout
if width < 1 {
width = 1
}
timeout := c.perCluster
if timeout <= 0 {
timeout = defaultPerCluster
}

results := make([]clusterResult[T], len(ids))
errs := make([]error, len(ids))
sem := make(chan struct{}, width)
sem := make(chan struct{}, defaultFanout)
var wg sync.WaitGroup
for i, id := range ids {
wg.Add(1)
Expand All @@ -65,7 +55,7 @@ func fanOut[T any](ctx context.Context, c *Client, fn func(context.Context) (T,
sem <- struct{}{}
defer func() { <-sem }()

cctx, cancel := context.WithTimeout(ctx, timeout)
cctx, cancel := context.WithTimeout(ctx, defaultPerCluster)
defer cancel()
cctx = reader.WithTarget(cctx, reader.Target{Cluster: id, Server: parent.Server})
val, err := fn(cctx)
Expand All @@ -91,11 +81,8 @@ func fanOut[T any](ctx context.Context, c *Client, fn func(context.Context) (T,

// isSkippedStatus reports whether err is a 403 or 404 from the API.
func isSkippedStatus(err error) bool {
var apiErr *APIError
if !errors.As(err, &apiErr) {
return false
}
return apiErr.Status == http.StatusForbidden || apiErr.Status == http.StatusNotFound
status := statusOf(err)
return status == http.StatusForbidden || status == http.StatusNotFound
}

// annotateCluster sets the cluster annotation on an object's annotation map.
Expand Down
2 changes: 1 addition & 1 deletion specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> This repository is checked out as the `mcp-server/` submodule of [GameplanePanel/Gameplane](https://github.com/GameplanePanel/Gameplane). Paths such as `docs/`, `operator/`, `api/`, `charts/` and `web/` below refer to that repository.

**Status:** pre-v1 (v0.4.1)
**Status:** pre-v1 (v0.5.0)
**Module / package:** `github.com/GameplanePanel/gameplane/mcp-server`

## Purpose
Expand Down
64 changes: 19 additions & 45 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,7 @@ func listResourcesHandler(c reader.Reader) mcp.ToolHandlerFor[listResourcesInput
if err != nil {
return nil, nil, err
}
stripped, err := stripManagedFields(list)
if err != nil {
return nil, nil, fmt.Errorf("strip managedFields from %s list: %w", in.Kind, err)
}
text, err := marshalIndent(stripped)
if err != nil {
return nil, nil, fmt.Errorf("marshal %s list: %w", in.Kind, err)
}
return textResult(text), nil, nil
return renderJSON(in.Kind+" list", list)
}
}

Expand All @@ -154,15 +146,7 @@ func getResourceHandler(c reader.Reader) mcp.ToolHandlerFor[getResourceInput, an
if err != nil {
return nil, nil, err
}
stripped, err := stripManagedFields(obj)
if err != nil {
return nil, nil, fmt.Errorf("strip managedFields from %s %s/%s: %w", in.Kind, in.Namespace, in.Name, err)
}
text, err := marshalIndent(stripped)
if err != nil {
return nil, nil, fmt.Errorf("marshal %s %s/%s: %w", in.Kind, in.Namespace, in.Name, err)
}
return textResult(text), nil, nil
return renderJSON(fmt.Sprintf("%s %s/%s", in.Kind, in.Namespace, in.Name), obj)
}
}

Expand All @@ -182,15 +166,7 @@ func listPodsHandler(c reader.Reader) mcp.ToolHandlerFor[listPodsInput, any] {
if err != nil {
return nil, nil, err
}
stripped, err := stripManagedFields(list)
if err != nil {
return nil, nil, fmt.Errorf("strip managedFields from pod list: %w", err)
}
text, err := marshalIndent(stripped)
if err != nil {
return nil, nil, fmt.Errorf("marshal pod list: %w", err)
}
return textResult(text), nil, nil
return renderJSON("pod list", list)
}
}

Expand All @@ -209,15 +185,7 @@ func getPodHandler(c reader.Reader) mcp.ToolHandlerFor[getPodInput, any] {
if err != nil {
return nil, nil, err
}
stripped, err := stripManagedFields(pod)
if err != nil {
return nil, nil, fmt.Errorf("strip managedFields from pod %s/%s: %w", in.Namespace, in.Name, err)
}
text, err := marshalIndent(stripped)
if err != nil {
return nil, nil, fmt.Errorf("marshal pod %s/%s: %w", in.Namespace, in.Name, err)
}
return textResult(text), nil, nil
return renderJSON(fmt.Sprintf("pod %s/%s", in.Namespace, in.Name), pod)
}
}

Expand Down Expand Up @@ -248,15 +216,7 @@ func listEventsHandler(c reader.Reader) mcp.ToolHandlerFor[listEventsInput, any]
// Sort events by lastTimestamp (newest first) and bound to 100 events
truncated := boundAndSortEvents(list)

stripped, err := stripManagedFields(truncated)
if err != nil {
return nil, nil, fmt.Errorf("strip managedFields from event list: %w", err)
}
text, err := marshalIndent(stripped)
if err != nil {
return nil, nil, fmt.Errorf("marshal event list: %w", err)
}
return textResult(text), nil, nil
return renderJSON("event list", truncated)
}
}

Expand Down Expand Up @@ -364,6 +324,20 @@ func proposeFixHandler(c reader.Reader) mcp.ToolHandlerFor[proposeFixInput, any]

// --- shared helpers ---

// renderJSON drops managedFields from v and returns it as indented JSON text.
// what names the object in error messages.
func renderJSON(what string, v any) (*mcp.CallToolResult, any, error) {
stripped, err := stripManagedFields(v)
if err != nil {
return nil, nil, fmt.Errorf("strip managedFields from %s: %w", what, err)
}
text, err := marshalIndent(stripped)
if err != nil {
return nil, nil, fmt.Errorf("marshal %s: %w", what, err)
}
return textResult(text), nil, nil
}

func textResult(text string) *mcp.CallToolResult {
return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: text}}}
}
Expand Down