Skip to content

Commit d5aa8bb

Browse files
committed
changed: get/list resources can work with cluster level
1 parent 4c7aeb1 commit d5aa8bb

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
case: Get k8s node using get-k8s-resources tool
2+
in:
3+
{
4+
"jsonrpc": "2.0",
5+
"method": "tools/call",
6+
"id": 2,
7+
"params":
8+
{
9+
"name": "get-k8s-resource",
10+
"arguments":
11+
{
12+
"context": "k3d-mcp-k8s-integration-test",
13+
"kind": "node",
14+
"name": "k3d-mcp-k8s-integration-test-server-0",
15+
},
16+
},
17+
}
18+
out:
19+
{
20+
"jsonrpc": "2.0",
21+
"id": 2,
22+
"result":
23+
{
24+
"content":
25+
[
26+
{
27+
"type": "text",
28+
"text": !!ere '{"apiVersion":"v1","kind":"Node","metadata":{/.*/"name":"k3d-mcp-k8s-integration-test-server-0"/.*/',
29+
},
30+
],
31+
"isError": false,
32+
},
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
case: List k8s nodes using list-k8s-resources tool
2+
in:
3+
{
4+
"jsonrpc": "2.0",
5+
"method": "tools/call",
6+
"id": 2,
7+
"params":
8+
{
9+
"name": "list-k8s-resources",
10+
"arguments":
11+
{ "context": "k3d-mcp-k8s-integration-test", "kind": "node" },
12+
},
13+
}
14+
out:
15+
{
16+
"jsonrpc": "2.0",
17+
"id": 2,
18+
"result":
19+
{
20+
"content": ["text": '{"name":"k3d-mcp-k8s-integration-test-server-0"}'],
21+
"isError": false,
22+
},
23+
}

internal/tools/get_resource_tool.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewGetResourceTool(pool k8s.ClientPool) fxctx.Tool {
2626

2727
inputSchema := toolinput.NewToolInputSchema(
2828
toolinput.WithString(contextProperty, "Name of the Kubernetes context to use, defaults to current context"),
29-
toolinput.WithRequiredString(namespaceProperty, "Namespace to get resource from"),
29+
toolinput.WithString(namespaceProperty, "Namespace to get resource from"),
3030
toolinput.WithString(groupProperty, "API Group of the resource to get"),
3131
toolinput.WithString(versionProperty, "API Version of the resource to get"),
3232
toolinput.WithRequiredString(kindProperty, "Kind of resource to get"),
@@ -47,10 +47,7 @@ func NewGetResourceTool(pool k8s.ClientPool) fxctx.Tool {
4747
}
4848

4949
k8sCtx := input.StringOr(contextProperty, "")
50-
namespace, err := input.String(namespaceProperty)
51-
if err != nil {
52-
return utils.ErrResponse(err)
53-
}
50+
namespace := input.StringOr(namespaceProperty, "")
5451

5552
kind, err := input.String(kindProperty)
5653
if err != nil {
@@ -72,7 +69,14 @@ func NewGetResourceTool(pool k8s.ClientPool) fxctx.Tool {
7269
return utils.ErrResponse(err)
7370
}
7471

75-
accumulator, exist, err := informer.Informer().GetIndexer().GetByKey(fmt.Sprintf("%s/%s", namespace, name))
72+
var key string
73+
74+
if namespace == "" {
75+
key = name
76+
} else {
77+
key = fmt.Sprintf("%s/%s", namespace, name)
78+
}
79+
accumulator, exist, err := informer.Informer().GetIndexer().GetByKey(key)
7680
if err != nil {
7781
return utils.ErrResponse(err)
7882
}
@@ -87,7 +91,7 @@ func NewGetResourceTool(pool k8s.ClientPool) fxctx.Tool {
8791
object := unstructuredAcc.Object
8892

8993
if metadata, ok := object["metadata"]; ok {
90-
if metadataMap, ok := metadata.(map[string]interface{}); ok {
94+
if metadataMap, ok := metadata.(map[string]any); ok {
9195
// this is too big and somewhat useless
9296
delete(metadataMap, "managedFields")
9397
}
@@ -118,7 +122,7 @@ func NewGetResourceTool(pool k8s.ClientPool) fxctx.Tool {
118122
var contents = []any{cnt}
119123

120124
return &mcp.CallToolResult{
121-
Meta: map[string]interface{}{},
125+
Meta: map[string]any{},
122126
Content: contents,
123127
IsError: utils.Ptr(false),
124128
}

internal/tools/list_resources_tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func NewListResourcesTool(pool k8s.ClientPool) fxctx.Tool {
142142

143143
type GenericListContent struct {
144144
Name string `json:"name"`
145-
Namespace string `json:"namespace"`
145+
Namespace string `json:"namespace,omitempty"`
146146
}
147147

148148
func (l GenericListContent) GetName() string {

main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func TestInK3dCluster(t *testing.T) {
6161
"testdata/with_k3d",
6262
"internal/k8s/apps/v1/deployment",
6363
"internal/k8s/core/v1/pod",
64+
"internal/k8s/core/v1/node",
6465
"internal/k8s/core/v1/service",
6566
}
6667

0 commit comments

Comments
 (0)