-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
59 lines (50 loc) · 1.28 KB
/
Copy pathcore.js
File metadata and controls
59 lines (50 loc) · 1.28 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
const k8s = require('@kubernetes/client-node')
const { default: axios } = require('axios')
const yaml = require('js-yaml')
const np = require('nested-property')
const logger = require('./logger')
const ns = 'emphasis'
const pod = async (name, prop) => {
const kc = new k8s.KubeConfig()
kc.loadFromDefault()
const k8sApi = kc.makeApiClient(k8s.CoreV1Api)
// console.log(await (await k8sApi.listNamespacedPod(ns)).body)
try {
const data = await k8sApi.readNamespacedPod(name, ns)
const value = np.get(data.body, prop)
return value
} catch {
return null
}
}
const api = async (url, prop) => {
try {
const { data } = await axios.get(url)
const value = np.get(data, prop)
return value
} catch {
return null
}
}
const apply = async (file) => {
const kc = new k8s.KubeConfig()
kc.loadFromDefault()
const json = yaml.load(file.content)
try {
const k8sApi = kc.makeApiClient(k8s.CoreV1Api)
await k8sApi.readNamespacedPod(json.metadata.name, ns)
} catch {
try {
const client = k8s.KubernetesObjectApi.makeApiClient(kc)
logger.info(`Applying ${file.name}`)
await client.create(json)
} catch (err) {
logger.error(`Error applying ${file.name}: ${err}`)
}
}
}
module.exports = {
apply,
pod,
api
}