forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKubeUtil.ts
More file actions
92 lines (87 loc) · 3.34 KB
/
Copy pathKubeUtil.ts
File metadata and controls
92 lines (87 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { CoreV1Api, AppsV1Api, ExtensionsV1beta1Api, ExtensionsApi } from "@kubernetes/client-node";
import { Config } from "./Config";
export class KubeUtil {
public CoreV1Api: CoreV1Api = null; // kc.makeApiClient(k8s.CoreV1Api);
public AppsV1Api: AppsV1Api = null; // kc.makeApiClient(k8s.AppsV1Api);
public ExtensionsV1beta1Api: ExtensionsV1beta1Api = null; // kc.makeApiClient(k8s.ExtensionsV1beta1Api);
private static _instance: KubeUtil = null;
public static instance(): KubeUtil {
if (this._instance == null) {
this._instance = new KubeUtil();
}
return this._instance;
}
constructor() {
const k8s = require("@kubernetes/client-node");
const kc = new k8s.KubeConfig();
let success: boolean = false;
try {
kc.loadFromDefault();
success = true;
} catch (error) {
console.error(error);
}
if (success == false) {
try {
kc.loadFromCluster();
success = true;
} catch (error) {
console.error(error);
}
}
this.CoreV1Api = kc.makeApiClient(k8s.CoreV1Api);
this.AppsV1Api = kc.makeApiClient(k8s.AppsV1Api);
this.ExtensionsV1beta1Api = kc.makeApiClient(k8s.ExtensionsV1beta1Api);
}
async GetStatefulSet(namespace, name) {
const list = await this.AppsV1Api.listNamespacedStatefulSet(namespace);
for (let i = 0; i < list.body.items.length; i++) {
const item = list.body.items[i];
if (item.metadata.name == name) return item;
}
return null;
}
async GetService(namespace, name) {
const list = await this.CoreV1Api.listNamespacedService(namespace);
for (let i = 0; i < list.body.items.length; i++) {
const item = list.body.items[i];
if (item.metadata.name == name) return item;
}
return null;
}
async GetPod(namespace, name) {
const list = await this.CoreV1Api.listNamespacedPod(namespace);
for (let i = 0; i < list.body.items.length; i++) {
const item = list.body.items[i];
if (item.metadata.name == name) return item;
}
return null;
}
async GetReplicaset(namespace, labelskey, labelsvalue) {
const list = await this.AppsV1Api.listNamespacedReplicaSet(namespace);
for (let i = 0; i < list.body.items.length; i++) {
const item = list.body.items[i];
if (item.metadata && item.metadata.labels) {
const value = item.metadata.labels[labelskey];
if (value == labelsvalue) return item;
}
}
return null;
}
async GetDeployment(namespace, name) {
const list = await this.AppsV1Api.listNamespacedDeployment(namespace);
for (let i = 0; i < list.body.items.length; i++) {
const item = list.body.items[i];
if (item.metadata.name == name) return item;
}
return null;
}
async GetIngress(namespace, name) {
const list = await this.ExtensionsV1beta1Api.listNamespacedIngress(namespace);
for (let i = 0; i < list.body.items.length; i++) {
const item = list.body.items[i];
if (item.metadata.name == name) return item;
}
return null;
}
}