-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes.js
116 lines (100 loc) · 2.6 KB
/
kubernetes.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const k8s = require('@kubernetes/client-node');
const axios = require('axios');
const https = require('https');
const crd = require('./crd.json');
class Client {
constructor(options) {
this.config = this.createConfig(options);
this.httpsAgent = this.createHttpsAgent(this.config);
this.axios = this.createAxios(this.config, this.httpsAgent);
}
createConfig(options) {
const config = new k8s.KubeConfig();
if (options && options.kubeconfig) {
config.loadFromOptions(options.kubeconfig);
} else {
config.loadFromDefault();
}
return config;
}
createHttpsAgent(config) {
const options = {};
config.applyHTTPSOptions(options);
return new https.Agent(options);
}
createAxios(config, httpsAgent) {
const cluster = config.getCurrentCluster();
const baseURL = cluster && cluster.server;
const options = {
httpsAgent,
baseURL,
};
config.applyToRequest(options);
options.headers = { common: { ...options.headers } };
return axios.default.create(options);
}
async request(options) {
const response = await this.axios(options);
return response.data;
}
async applyCRD() {
return await this.apply(
{
type: types.CustomResourceDefinition,
name: crd.metadata.name,
},
crd
);
}
async getAllInNamespace({ type, namespace }) {
return await this.request({
method: 'GET',
url: type.allPattern({ namespace }),
});
}
async get({ type, namespace, name }) {
return await this.request({
method: 'GET',
url: type.namedPattern({ namespace, name }),
});
}
async apply({ type, namespace, name }, data) {
return await this.request({
method: 'PATCH',
headers: {
'Content-Type': 'application/apply-patch+yaml',
},
params: { fieldManager: 'endb-k8s', force: 'true' },
url: type.namedPattern({ namespace, name }),
data,
});
}
async delete({ type, namespace, name }) {
return await this.request({
method: 'DELETE',
url: type.namedPattern({ namespace, name }),
});
}
async deleteAllInNamespace({ type, namespace }) {
return await this.request({
method: 'DELETE',
url: type.allPattern({ namespace }),
});
}
}
const types = {
CustomResourceDefinition: {
allPattern: () => `/apis/apiextensions.k8s.io/v1/customresourcedefinitions/`,
namedPattern: ({ name }) =>
`/apis/apiextensions.k8s.io/v1/customresourcedefinitions/${name}`,
},
EndbData: {
allPattern: ({ namespace }) => `/apis/hubc.app/v1/namespaces/${namespace}/endbdata`,
namedPattern: ({ namespace, name }) =>
`/apis/hubc.app/v1/namespaces/${namespace}/endbdata/${name}`,
},
};
module.exports = {
createClient: options => new Client(options),
types,
};