diff --git a/internal/kube/client.go b/internal/kube/client.go index 926eb67..10e7064 100644 --- a/internal/kube/client.go +++ b/internal/kube/client.go @@ -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 } diff --git a/internal/panel/client.go b/internal/panel/client.go index e896a75..b4061f7 100644 --- a/internal/panel/client.go +++ b/internal/panel/client.go @@ -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. @@ -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 } diff --git a/internal/panel/fanout.go b/internal/panel/fanout.go index 43d0627..5a7fff0 100644 --- a/internal/panel/fanout.go +++ b/internal/panel/fanout.go @@ -2,7 +2,6 @@ package panel import ( "context" - "errors" "net/http" "strings" "sync" @@ -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) { @@ -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) @@ -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) @@ -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. diff --git a/specs.md b/specs.md index 2ee17cd..aefb532 100644 --- a/specs.md +++ b/specs.md @@ -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 diff --git a/tools.go b/tools.go index 8f19648..e45a62a 100644 --- a/tools.go +++ b/tools.go @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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}}} }