mcp-server-kubernetes 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -5
- package/dist/index.js +161 -3
- package/dist/types.d.ts +22 -0
- package/dist/types.js +6 -0
- package/dist/unit.test.js +227 -110
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -40,15 +40,11 @@ If you have errors, open up a standard terminal and run `kubectl get pods` to se
|
|
|
40
40
|
- [x] Delete a pod
|
|
41
41
|
- [x] Describe a pod
|
|
42
42
|
- [x] List all namespaces
|
|
43
|
+
- [x] Get logs from a pod for debugging (supports pods, deployments, jobs, and label selectors)
|
|
43
44
|
- [ ] Port forward to a pod
|
|
44
|
-
- [ ] Get logs from a pod for debugging
|
|
45
45
|
- [ ] Choose namespace for next commands (memory)
|
|
46
46
|
- [ ] Support Helm for installing charts
|
|
47
47
|
|
|
48
|
-
## In Progress
|
|
49
|
-
|
|
50
|
-
- [ ] [Docker support](https://github.com/Flux159/mcp-server-kubernetes/pull/9)
|
|
51
|
-
|
|
52
48
|
## Local Development
|
|
53
49
|
|
|
54
50
|
```bash
|
package/dist/index.js
CHANGED
|
@@ -316,6 +316,55 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
|
316
316
|
properties: {},
|
|
317
317
|
},
|
|
318
318
|
},
|
|
319
|
+
{
|
|
320
|
+
name: "get_logs",
|
|
321
|
+
description: "Get logs from pods, deployments, jobs, or resources matching a label selector",
|
|
322
|
+
inputSchema: {
|
|
323
|
+
type: "object",
|
|
324
|
+
properties: {
|
|
325
|
+
resourceType: {
|
|
326
|
+
type: "string",
|
|
327
|
+
enum: ["pod", "deployment", "job"],
|
|
328
|
+
description: "Type of resource to get logs from",
|
|
329
|
+
},
|
|
330
|
+
name: {
|
|
331
|
+
type: "string",
|
|
332
|
+
description: "Name of the resource",
|
|
333
|
+
},
|
|
334
|
+
namespace: {
|
|
335
|
+
type: "string",
|
|
336
|
+
description: "Namespace of the resource",
|
|
337
|
+
default: "default",
|
|
338
|
+
},
|
|
339
|
+
labelSelector: {
|
|
340
|
+
type: "string",
|
|
341
|
+
description: "Label selector to filter resources",
|
|
342
|
+
optional: true,
|
|
343
|
+
},
|
|
344
|
+
container: {
|
|
345
|
+
type: "string",
|
|
346
|
+
description: "Container name (required when pod has multiple containers)",
|
|
347
|
+
optional: true,
|
|
348
|
+
},
|
|
349
|
+
tail: {
|
|
350
|
+
type: "number",
|
|
351
|
+
description: "Number of lines to show from end of logs",
|
|
352
|
+
optional: true,
|
|
353
|
+
},
|
|
354
|
+
since: {
|
|
355
|
+
type: "number",
|
|
356
|
+
description: "Get logs since relative time in seconds",
|
|
357
|
+
optional: true,
|
|
358
|
+
},
|
|
359
|
+
timestamps: {
|
|
360
|
+
type: "boolean",
|
|
361
|
+
description: "Include timestamps in logs",
|
|
362
|
+
default: false,
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
required: ["resourceType"],
|
|
366
|
+
},
|
|
367
|
+
},
|
|
319
368
|
],
|
|
320
369
|
};
|
|
321
370
|
});
|
|
@@ -426,21 +475,30 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
426
475
|
...templateConfig,
|
|
427
476
|
...(createPodInput.command && {
|
|
428
477
|
command: createPodInput.command,
|
|
478
|
+
args: undefined, // Clear default args when command is overridden
|
|
429
479
|
}),
|
|
430
480
|
},
|
|
431
481
|
],
|
|
432
482
|
},
|
|
433
483
|
};
|
|
434
|
-
const
|
|
484
|
+
const response = await k8sManager
|
|
435
485
|
.getCoreApi()
|
|
436
|
-
.createNamespacedPod(createPodInput.namespace, pod)
|
|
486
|
+
.createNamespacedPod(createPodInput.namespace, pod)
|
|
487
|
+
.catch((error) => {
|
|
488
|
+
console.error("Pod creation error:", {
|
|
489
|
+
status: error.response?.statusCode,
|
|
490
|
+
message: error.response?.body?.message || error.message,
|
|
491
|
+
details: error.response?.body,
|
|
492
|
+
});
|
|
493
|
+
throw error;
|
|
494
|
+
});
|
|
437
495
|
k8sManager.trackResource("Pod", createPodInput.name, createPodInput.namespace);
|
|
438
496
|
return {
|
|
439
497
|
content: [
|
|
440
498
|
{
|
|
441
499
|
type: "text",
|
|
442
500
|
text: JSON.stringify({
|
|
443
|
-
podName: body.metadata.name,
|
|
501
|
+
podName: response.body.metadata.name,
|
|
444
502
|
status: "created",
|
|
445
503
|
}, null, 2),
|
|
446
504
|
},
|
|
@@ -580,6 +638,106 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
580
638
|
],
|
|
581
639
|
};
|
|
582
640
|
}
|
|
641
|
+
case "get_logs": {
|
|
642
|
+
const { resourceType, name, namespace = "default", labelSelector, container, tail = 100, sinceSeconds, timestamps, pretty = true, follow = false, } = input;
|
|
643
|
+
async function getPodLogs(podName, podNamespace) {
|
|
644
|
+
try {
|
|
645
|
+
const { body } = await k8sManager.getCoreApi().readNamespacedPodLog(podName, podNamespace, container, follow, undefined, // insecureSkipTLSVerifyBackend
|
|
646
|
+
undefined, // limitBytes
|
|
647
|
+
pretty ? "true" : "false", undefined, // previous
|
|
648
|
+
sinceSeconds, tail, timestamps);
|
|
649
|
+
return body;
|
|
650
|
+
}
|
|
651
|
+
catch (error) {
|
|
652
|
+
if (error.response?.statusCode === 404) {
|
|
653
|
+
throw new McpError(ErrorCode.InvalidRequest, `Pod ${podName} not found in namespace ${podNamespace}`);
|
|
654
|
+
}
|
|
655
|
+
// Log full error details
|
|
656
|
+
console.error("Full error:", {
|
|
657
|
+
statusCode: error.response?.statusCode,
|
|
658
|
+
message: error.response?.body?.message || error.message,
|
|
659
|
+
details: error.response?.body,
|
|
660
|
+
});
|
|
661
|
+
throw new McpError(ErrorCode.InternalError, `Failed to get logs for pod ${podName}: ${error.response?.body?.message || error.message}`);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
const logs = {};
|
|
665
|
+
try {
|
|
666
|
+
// Get logs based on resource type
|
|
667
|
+
switch (resourceType.toLowerCase()) {
|
|
668
|
+
case "pod": {
|
|
669
|
+
if (!name) {
|
|
670
|
+
throw new McpError(ErrorCode.InvalidRequest, "Pod name is required when resourceType is 'pod'");
|
|
671
|
+
}
|
|
672
|
+
logs[name] = await getPodLogs(name, namespace);
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
case "deployment": {
|
|
676
|
+
if (!name) {
|
|
677
|
+
throw new McpError(ErrorCode.InvalidRequest, "Deployment name is required when resourceType is 'deployment'");
|
|
678
|
+
}
|
|
679
|
+
const { body: deployment } = await k8sManager
|
|
680
|
+
.getAppsApi()
|
|
681
|
+
.readNamespacedDeployment(name, namespace);
|
|
682
|
+
if (!deployment.spec?.selector?.matchLabels) {
|
|
683
|
+
throw new McpError(ErrorCode.InvalidRequest, `Deployment ${name} has no selector`);
|
|
684
|
+
}
|
|
685
|
+
const selector = Object.entries(deployment.spec.selector.matchLabels)
|
|
686
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
687
|
+
.join(",");
|
|
688
|
+
const { body: podList } = await k8sManager
|
|
689
|
+
.getCoreApi()
|
|
690
|
+
.listNamespacedPod(namespace, undefined, undefined, undefined, undefined, selector);
|
|
691
|
+
for (const pod of podList.items) {
|
|
692
|
+
if (pod.metadata?.name) {
|
|
693
|
+
logs[pod.metadata.name] = await getPodLogs(pod.metadata.name, namespace);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
case "job": {
|
|
699
|
+
if (!name) {
|
|
700
|
+
throw new McpError(ErrorCode.InvalidRequest, "Job name is required when resourceType is 'job'");
|
|
701
|
+
}
|
|
702
|
+
const { body: podList } = await k8sManager
|
|
703
|
+
.getCoreApi()
|
|
704
|
+
.listNamespacedPod(namespace, undefined, undefined, undefined, undefined, `job-name=${name}`);
|
|
705
|
+
for (const pod of podList.items) {
|
|
706
|
+
if (pod.metadata?.name) {
|
|
707
|
+
logs[pod.metadata.name] = await getPodLogs(pod.metadata.name, namespace);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
default:
|
|
713
|
+
throw new McpError(ErrorCode.InvalidRequest, `Unsupported resource type: ${resourceType}`);
|
|
714
|
+
}
|
|
715
|
+
// If labelSelector is provided, filter or add logs by label
|
|
716
|
+
if (labelSelector) {
|
|
717
|
+
const { body: labeledPods } = await k8sManager
|
|
718
|
+
.getCoreApi()
|
|
719
|
+
.listNamespacedPod(namespace, undefined, undefined, undefined, undefined, labelSelector);
|
|
720
|
+
for (const pod of labeledPods.items) {
|
|
721
|
+
if (pod.metadata?.name) {
|
|
722
|
+
logs[pod.metadata.name] = await getPodLogs(pod.metadata.name, namespace);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
return {
|
|
727
|
+
content: [
|
|
728
|
+
{
|
|
729
|
+
type: "text",
|
|
730
|
+
text: JSON.stringify({ logs }, null, 2),
|
|
731
|
+
},
|
|
732
|
+
],
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
catch (error) {
|
|
736
|
+
if (error instanceof McpError)
|
|
737
|
+
throw error;
|
|
738
|
+
throw new McpError(ErrorCode.InternalError, `Failed to get logs: ${error}`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
583
741
|
default:
|
|
584
742
|
throw new McpError(ErrorCode.InvalidRequest, `Unknown tool: ${name}`);
|
|
585
743
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -251,6 +251,28 @@ export declare const ListNodesResponseSchema: z.ZodObject<{
|
|
|
251
251
|
text: string;
|
|
252
252
|
}[];
|
|
253
253
|
}>;
|
|
254
|
+
export declare const GetLogsResponseSchema: z.ZodObject<{
|
|
255
|
+
content: z.ZodArray<z.ZodObject<{
|
|
256
|
+
type: z.ZodLiteral<"text">;
|
|
257
|
+
text: z.ZodString;
|
|
258
|
+
}, "strip", z.ZodTypeAny, {
|
|
259
|
+
type: "text";
|
|
260
|
+
text: string;
|
|
261
|
+
}, {
|
|
262
|
+
type: "text";
|
|
263
|
+
text: string;
|
|
264
|
+
}>, "many">;
|
|
265
|
+
}, "strip", z.ZodTypeAny, {
|
|
266
|
+
content: {
|
|
267
|
+
type: "text";
|
|
268
|
+
text: string;
|
|
269
|
+
}[];
|
|
270
|
+
}, {
|
|
271
|
+
content: {
|
|
272
|
+
type: "text";
|
|
273
|
+
text: string;
|
|
274
|
+
}[];
|
|
275
|
+
}>;
|
|
254
276
|
export declare const ListResourcesResponseSchema: z.ZodObject<{
|
|
255
277
|
resources: z.ZodArray<z.ZodObject<{
|
|
256
278
|
uri: z.ZodString;
|
package/dist/types.js
CHANGED
|
@@ -76,6 +76,12 @@ export const ListNodesResponseSchema = z.object({
|
|
|
76
76
|
text: z.string(),
|
|
77
77
|
})),
|
|
78
78
|
});
|
|
79
|
+
export const GetLogsResponseSchema = z.object({
|
|
80
|
+
content: z.array(z.object({
|
|
81
|
+
type: z.literal("text"),
|
|
82
|
+
text: z.string(),
|
|
83
|
+
})),
|
|
84
|
+
});
|
|
79
85
|
export const ListResourcesResponseSchema = z.object({
|
|
80
86
|
resources: z.array(ResourceSchema),
|
|
81
87
|
});
|
package/dist/unit.test.js
CHANGED
|
@@ -1,24 +1,79 @@
|
|
|
1
|
-
|
|
1
|
+
// Import required test frameworks and SDK components
|
|
2
|
+
import { expect, test, describe, beforeEach, afterEach, } from "vitest";
|
|
2
3
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
3
4
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
4
|
-
import { ListToolsResponseSchema, ListPodsResponseSchema,
|
|
5
|
+
import { ListToolsResponseSchema, ListPodsResponseSchema, ListNamespacesResponseSchema, ListNodesResponseSchema, CreatePodResponseSchema, DeletePodResponseSchema, } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Utility function to create a promise that resolves after specified milliseconds
|
|
8
|
+
* Useful for waiting between operations or ensuring async operations complete
|
|
9
|
+
*/
|
|
5
10
|
async function sleep(ms) {
|
|
6
11
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
12
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Generates a random SHA-like string for unique resource naming
|
|
15
|
+
* Used to avoid naming conflicts when creating test resources
|
|
16
|
+
*/
|
|
17
|
+
function generateRandomSHA() {
|
|
18
|
+
return Math.random().toString(36).substring(2, 15);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Test suite for kubernetes server operations
|
|
22
|
+
* Tests the core functionality of kubernetes operations including:
|
|
23
|
+
* - Listing available tools
|
|
24
|
+
* - Namespace and node operations
|
|
25
|
+
* - Pod lifecycle management (create, monitor, delete)
|
|
26
|
+
*/
|
|
27
|
+
describe("kubernetes server operations", () => {
|
|
28
|
+
let transport;
|
|
29
|
+
let client;
|
|
30
|
+
/**
|
|
31
|
+
* Set up before each test:
|
|
32
|
+
* - Creates a new StdioClientTransport instance
|
|
33
|
+
* - Initializes and connects the MCP client
|
|
34
|
+
* - Waits for connection to be established
|
|
35
|
+
*/
|
|
36
|
+
beforeEach(async () => {
|
|
37
|
+
try {
|
|
38
|
+
transport = new StdioClientTransport({
|
|
39
|
+
command: "bun",
|
|
40
|
+
args: ["src/index.ts"],
|
|
41
|
+
stderr: "pipe",
|
|
42
|
+
});
|
|
43
|
+
client = new Client({
|
|
44
|
+
name: "test-client",
|
|
45
|
+
version: "1.0.0",
|
|
46
|
+
}, {
|
|
47
|
+
capabilities: {},
|
|
48
|
+
});
|
|
49
|
+
await client.connect(transport);
|
|
50
|
+
// Wait for connection to be fully established
|
|
51
|
+
await sleep(1000);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
console.error("Error in beforeEach:", e);
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
13
57
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Clean up after each test:
|
|
60
|
+
* - Closes the transport connection
|
|
61
|
+
* - Waits to ensure clean shutdown
|
|
62
|
+
*/
|
|
63
|
+
afterEach(async () => {
|
|
64
|
+
try {
|
|
65
|
+
await transport.close();
|
|
66
|
+
await sleep(1000);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
console.error("Error during cleanup:", e);
|
|
70
|
+
}
|
|
19
71
|
});
|
|
20
|
-
|
|
21
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Test case: Verify the availability of kubernetes tools
|
|
74
|
+
* Ensures that the server exposes the expected kubernetes operations
|
|
75
|
+
*/
|
|
76
|
+
test("list available tools", async () => {
|
|
22
77
|
// List available tools stays the same
|
|
23
78
|
console.log("Listing available tools...");
|
|
24
79
|
const toolsList = await client.request({
|
|
@@ -26,13 +81,19 @@ test("kubernetes server operations", async () => {
|
|
|
26
81
|
}, ListToolsResponseSchema);
|
|
27
82
|
expect(toolsList.tools).toBeDefined();
|
|
28
83
|
expect(toolsList.tools.length).toBeGreaterThan(0);
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* Test case: Verify namespace and node listing functionality
|
|
87
|
+
* Tests both namespace and node listing operations in sequence
|
|
88
|
+
*/
|
|
89
|
+
test("list namespaces and nodes", async () => {
|
|
29
90
|
// List namespaces
|
|
30
91
|
console.log("Listing namespaces...");
|
|
31
92
|
const namespacesResult = await client.request({
|
|
32
93
|
method: "tools/call",
|
|
33
94
|
params: {
|
|
34
95
|
name: "list_namespaces",
|
|
35
|
-
arguments: {},
|
|
96
|
+
arguments: {},
|
|
36
97
|
},
|
|
37
98
|
}, ListNamespacesResponseSchema);
|
|
38
99
|
expect(namespacesResult.content[0].type).toBe("text");
|
|
@@ -51,122 +112,178 @@ test("kubernetes server operations", async () => {
|
|
|
51
112
|
const nodes = JSON.parse(listNodesResult.content[0].text);
|
|
52
113
|
expect(nodes.nodes).toBeDefined();
|
|
53
114
|
expect(Array.isArray(nodes.nodes)).toBe(true);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* Test case: Complete pod lifecycle management
|
|
118
|
+
* Tests the full lifecycle of a pod including:
|
|
119
|
+
* 1. Cleanup of existing test pods
|
|
120
|
+
* 2. Creation of new test pod
|
|
121
|
+
* 3. Monitoring pod until running state
|
|
122
|
+
* 4. Verification of pod logs
|
|
123
|
+
* 5. Pod deletion and termination verification
|
|
124
|
+
*
|
|
125
|
+
* Note: Test timeout is set to 120 seconds to accommodate all operations via vitest.config.ts
|
|
126
|
+
*/
|
|
127
|
+
test("pod lifecycle management", async () => {
|
|
128
|
+
const podBaseName = "unit-test";
|
|
129
|
+
const podName = `${podBaseName}-${generateRandomSHA()}`;
|
|
130
|
+
// Step 1: Check if pods with unit-test prefix exist and terminate them if found
|
|
131
|
+
const existingPods = await client.request({
|
|
57
132
|
method: "tools/call",
|
|
58
133
|
params: {
|
|
59
|
-
name: "
|
|
134
|
+
name: "list_pods",
|
|
60
135
|
arguments: {
|
|
61
|
-
// Changed from input to arguments
|
|
62
|
-
name: "test-pod",
|
|
63
136
|
namespace: "default",
|
|
64
|
-
ignoreNotFound: true,
|
|
65
137
|
},
|
|
66
138
|
},
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
139
|
+
}, ListPodsResponseSchema);
|
|
140
|
+
const podsResponse = JSON.parse(existingPods.content[0].text);
|
|
141
|
+
const existingTestPods = podsResponse.items?.filter((pod) => pod.metadata?.name?.startsWith(podBaseName)) || [];
|
|
142
|
+
// Terminate existing test pods if found
|
|
143
|
+
for (const pod of existingTestPods) {
|
|
144
|
+
await client.request({
|
|
145
|
+
method: "tools/call",
|
|
146
|
+
params: {
|
|
147
|
+
name: "delete_pod",
|
|
148
|
+
arguments: {
|
|
149
|
+
name: pod.metadata.name,
|
|
150
|
+
namespace: "default",
|
|
151
|
+
ignoreNotFound: true,
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
}, DeletePodResponseSchema);
|
|
155
|
+
// Wait for pod to be fully terminated
|
|
156
|
+
let podDeleted = false;
|
|
157
|
+
const terminationStartTime = Date.now();
|
|
158
|
+
while (!podDeleted && Date.now() - terminationStartTime < 10000) {
|
|
159
|
+
try {
|
|
160
|
+
await client.request({
|
|
161
|
+
method: "tools/call",
|
|
162
|
+
params: {
|
|
163
|
+
name: "describe_pod",
|
|
164
|
+
arguments: {
|
|
165
|
+
name: pod.metadata.name,
|
|
166
|
+
namespace: "default",
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
}, ListPodsResponseSchema);
|
|
170
|
+
await sleep(500);
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
// If we get an error, it might be because the pod is gone (404)
|
|
174
|
+
podDeleted = true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Create new pod with random SHA name
|
|
74
179
|
const createPodResult = await client.request({
|
|
75
180
|
method: "tools/call",
|
|
76
181
|
params: {
|
|
77
182
|
name: "create_pod",
|
|
78
183
|
arguments: {
|
|
79
|
-
|
|
80
|
-
name: "test-pod",
|
|
184
|
+
name: podName,
|
|
81
185
|
namespace: "default",
|
|
82
|
-
template: "
|
|
186
|
+
template: "busybox",
|
|
187
|
+
command: ["/bin/sh", "-c", "echo Pod is running && sleep infinity"],
|
|
83
188
|
},
|
|
84
189
|
},
|
|
85
190
|
}, CreatePodResponseSchema);
|
|
86
191
|
expect(createPodResult.content[0].type).toBe("text");
|
|
87
|
-
const
|
|
88
|
-
expect(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}, CreatePodResponseSchema // Reusing existing schema since response format is similar
|
|
102
|
-
);
|
|
103
|
-
expect(describePodResult.content[0].type).toBe("text");
|
|
104
|
-
const podDescription = JSON.parse(describePodResult.content[0].text);
|
|
105
|
-
expect(podDescription.metadata.name).toBe("test-pod");
|
|
106
|
-
expect(podDescription.metadata.namespace).toBe("default");
|
|
107
|
-
expect(podDescription.kind).toBe("Pod");
|
|
108
|
-
// List pods to verify creation
|
|
109
|
-
console.log("Listing pods...");
|
|
110
|
-
const listPodsResult = await client.request({
|
|
111
|
-
method: "tools/call",
|
|
112
|
-
params: {
|
|
113
|
-
name: "list_pods",
|
|
114
|
-
arguments: {
|
|
115
|
-
// Changed from input to arguments
|
|
116
|
-
namespace: "default",
|
|
192
|
+
const podResult = JSON.parse(createPodResult.content[0].text);
|
|
193
|
+
expect(podResult.podName).toBe(podName);
|
|
194
|
+
// Step 2: Wait for Running state (up to 60 seconds)
|
|
195
|
+
let podRunning = false;
|
|
196
|
+
const startTime = Date.now();
|
|
197
|
+
while (!podRunning && Date.now() - startTime < 60000) {
|
|
198
|
+
const podStatus = await client.request({
|
|
199
|
+
method: "tools/call",
|
|
200
|
+
params: {
|
|
201
|
+
name: "describe_pod",
|
|
202
|
+
arguments: {
|
|
203
|
+
name: podName,
|
|
204
|
+
namespace: "default",
|
|
205
|
+
},
|
|
117
206
|
},
|
|
118
|
-
},
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
arguments: {}, // Changed from input to arguments
|
|
146
|
-
},
|
|
147
|
-
}, CleanupResponseSchema);
|
|
148
|
-
expect(cleanupResult.content[0].type).toBe("text");
|
|
149
|
-
const cleanupData = JSON.parse(cleanupResult.content[0].text);
|
|
150
|
-
expect(cleanupData.success).toBe(true);
|
|
151
|
-
// Verify cleanup by listing pods again
|
|
152
|
-
console.log("Verifying cleanup...");
|
|
153
|
-
const finalPodsResult = await client.request({
|
|
207
|
+
}, ListPodsResponseSchema);
|
|
208
|
+
const status = JSON.parse(podStatus.content[0].text);
|
|
209
|
+
if (status.status?.phase === "Running") {
|
|
210
|
+
podRunning = true;
|
|
211
|
+
console.log(`Pod ${podName} is running. Checking logs...`);
|
|
212
|
+
// Check pod logs once running
|
|
213
|
+
const logsResult = await client.request({
|
|
214
|
+
method: "tools/call",
|
|
215
|
+
params: {
|
|
216
|
+
name: "get_logs",
|
|
217
|
+
arguments: {
|
|
218
|
+
resourceType: "pod",
|
|
219
|
+
name: podName,
|
|
220
|
+
namespace: "default",
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
}, ListPodsResponseSchema);
|
|
224
|
+
expect(logsResult.content[0].type).toBe("text");
|
|
225
|
+
const logs = JSON.parse(logsResult.content[0].text);
|
|
226
|
+
expect(logs.logs[podName]).toContain("Pod is running");
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
await sleep(1000);
|
|
230
|
+
}
|
|
231
|
+
expect(podRunning).toBe(true);
|
|
232
|
+
// Step 3: Terminate pod and verify termination (wait up to 10 seconds)
|
|
233
|
+
const deletePodResult = await client.request({
|
|
154
234
|
method: "tools/call",
|
|
155
235
|
params: {
|
|
156
|
-
name: "
|
|
236
|
+
name: "delete_pod",
|
|
157
237
|
arguments: {
|
|
158
|
-
|
|
238
|
+
name: podName,
|
|
159
239
|
namespace: "default",
|
|
160
240
|
},
|
|
161
241
|
},
|
|
162
|
-
},
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
//
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
242
|
+
}, DeletePodResponseSchema);
|
|
243
|
+
expect(deletePodResult.content[0].type).toBe("text");
|
|
244
|
+
const deleteResult = JSON.parse(deletePodResult.content[0].text);
|
|
245
|
+
expect(deleteResult.status).toBe("deleted");
|
|
246
|
+
// Try to verify pod termination, but don't fail the test if we can't confirm it
|
|
247
|
+
try {
|
|
248
|
+
let podTerminated = false;
|
|
249
|
+
const terminationStartTime = Date.now();
|
|
250
|
+
while (!podTerminated && Date.now() - terminationStartTime < 10000) {
|
|
251
|
+
try {
|
|
252
|
+
const podStatus = await client.request({
|
|
253
|
+
method: "tools/call",
|
|
254
|
+
params: {
|
|
255
|
+
name: "describe_pod",
|
|
256
|
+
arguments: {
|
|
257
|
+
name: podName,
|
|
258
|
+
namespace: "default",
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
}, ListPodsResponseSchema);
|
|
262
|
+
// Pod still exists, check if it's in Terminating state
|
|
263
|
+
const status = JSON.parse(podStatus.content[0].text);
|
|
264
|
+
if (status.status?.phase === "Terminating") {
|
|
265
|
+
podTerminated = true;
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
await sleep(500);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
// If we get an error (404), the pod is gone which also means it's terminated
|
|
272
|
+
podTerminated = true;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
// Log termination status but don't fail the test
|
|
277
|
+
if (podTerminated) {
|
|
278
|
+
console.log(`Pod ${podName} termination confirmed`);
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
console.log(`Pod ${podName} termination could not be confirmed within timeout, but deletion was initiated`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
// Ignore any errors during termination check
|
|
286
|
+
console.log(`Error checking pod termination status: ${error}`);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
172
289
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-server-kubernetes",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "MCP server for interacting with Kubernetes clusters via kubectl",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"build": "tsc && shx chmod +x dist/*.js",
|
|
20
20
|
"dev": "tsc --watch",
|
|
21
21
|
"start": "node dist/index.js",
|
|
22
|
-
"test": "vitest
|
|
22
|
+
"test": "vitest run",
|
|
23
23
|
"prepublishOnly": "npm run build",
|
|
24
24
|
"dockerbuild": "docker buildx build -t flux159/mcp-server-kubernetes --platform linux/amd64,linux/arm64 --push ."
|
|
25
25
|
},
|