-
Notifications
You must be signed in to change notification settings - Fork 1
/
k8s-functions.js
115 lines (95 loc) · 2.84 KB
/
k8s-functions.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
const {
applySpec,
extractResponseData,
parseError,
} = require("./helpers");
async function apply(client, { spec, namespace }) {
const adjustedSpec = {
...spec,
metadata: {
...spec.metadata,
annotations: spec.metadata.annotations || {},
},
};
if (namespace && !spec.metadata.namespace && spec.kind !== "Namespace") {
adjustedSpec.metadata.namespace = namespace;
}
adjustedSpec.metadata.annotations["kubectl.kubernetes.io/last-applied-configuration"] = JSON.stringify(adjustedSpec);
try {
const response = await applySpec(client, adjustedSpec);
return response.body;
} catch (err) {
return parseError(err);
}
}
async function deleteObject(client, {
functionName,
objectType,
objectName,
namespace,
}) {
const deletionInfo = {
objectType,
objectName,
namespace: namespace || "default",
};
try {
const result = namespace
? await client[functionName](objectName, namespace)
: await client[functionName](objectName);
deletionInfo.result = JSON.stringify(result.body);
} catch (error) {
deletionInfo.error = JSON.stringify(parseError(error));
throw deletionInfo;
}
return deletionInfo;
}
async function getService(client, { name, namespace }) {
try {
const res = await client.readNamespacedService(name, namespace);
return res.body;
} catch (err) {
throw parseError(err);
}
}
async function getAllServices(client, { labelsFilter, namespace: userDefinedNamespace }) {
const filters = labelsFilter.map((filter) => {
const [key, value] = filter.split("=");
return { key, value };
});
try {
let namespacesWithServices = null;
if (userDefinedNamespace !== "*") {
const response = await client.listNamespacedService(userDefinedNamespace);
namespacesWithServices = [extractResponseData(response)];
} else {
const namespacesResponse = await client.listNamespace();
const namespaces = extractResponseData(namespacesResponse);
const namespacesWithServicesPromises = namespaces.map(
(namespace) => client.listNamespacedService(namespace.metadata.name),
);
const namespacesWithServicesResults = await Promise.all(namespacesWithServicesPromises);
namespacesWithServices = namespacesWithServicesResults.map(
(result) => extractResponseData(result),
);
}
const allFilteredServices = namespacesWithServices.map(
(namespaceServices) => namespaceServices.filter(
(service) => filters.every(
(filter) => service.metadata.labels
&& service.metadata.labels[filter.key]
&& service.metadata.labels[filter.key] === filter.value,
),
),
).flat();
return allFilteredServices;
} catch (err) {
throw parseError(err);
}
}
module.exports = {
apply,
getService,
getAllServices,
deleteObject,
};