mcp-server-kubernetes 2.0.0 → 2.1.0
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.
|
@@ -9,6 +9,10 @@ export declare class KubernetesManager {
|
|
|
9
9
|
private k8sAppsApi;
|
|
10
10
|
private k8sBatchApi;
|
|
11
11
|
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* A very simple test to check if the application is running inside a Kubernetes cluster
|
|
14
|
+
*/
|
|
15
|
+
private isRunningInCluster;
|
|
12
16
|
/**
|
|
13
17
|
* Set the current context to the desired context name.
|
|
14
18
|
*
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as k8s from "@kubernetes/client-node";
|
|
2
|
+
import * as fs from "fs";
|
|
2
3
|
export class KubernetesManager {
|
|
3
4
|
resources = [];
|
|
4
5
|
portForwards = [];
|
|
@@ -9,11 +10,28 @@ export class KubernetesManager {
|
|
|
9
10
|
k8sBatchApi;
|
|
10
11
|
constructor() {
|
|
11
12
|
this.kc = new k8s.KubeConfig();
|
|
12
|
-
this.
|
|
13
|
+
if (this.isRunningInCluster()) {
|
|
14
|
+
this.kc.loadFromCluster();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
this.kc.loadFromDefault();
|
|
18
|
+
}
|
|
13
19
|
this.k8sApi = this.kc.makeApiClient(k8s.CoreV1Api);
|
|
14
20
|
this.k8sAppsApi = this.kc.makeApiClient(k8s.AppsV1Api);
|
|
15
21
|
this.k8sBatchApi = this.kc.makeApiClient(k8s.BatchV1Api);
|
|
16
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* A very simple test to check if the application is running inside a Kubernetes cluster
|
|
25
|
+
*/
|
|
26
|
+
isRunningInCluster() {
|
|
27
|
+
const serviceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/token";
|
|
28
|
+
try {
|
|
29
|
+
return fs.existsSync(serviceAccountPath);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
17
35
|
/**
|
|
18
36
|
* Set the current context to the desired context name.
|
|
19
37
|
*
|
|
@@ -22,10 +40,10 @@ export class KubernetesManager {
|
|
|
22
40
|
setCurrentContext(contextName) {
|
|
23
41
|
// Get all available contexts
|
|
24
42
|
const contexts = this.kc.getContexts();
|
|
25
|
-
const contextNames = contexts.map(context => context.name);
|
|
43
|
+
const contextNames = contexts.map((context) => context.name);
|
|
26
44
|
// Check if the requested context exists
|
|
27
45
|
if (!contextNames.includes(contextName)) {
|
|
28
|
-
throw new Error(`Context '${contextName}' not found. Available contexts: ${contextNames.join(
|
|
46
|
+
throw new Error(`Context '${contextName}' not found. Available contexts: ${contextNames.join(", ")}`);
|
|
29
47
|
}
|
|
30
48
|
// Set the current context
|
|
31
49
|
this.kc.setCurrentContext(contextName);
|