-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
152 lines (128 loc) · 3.83 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { bootstrap } = require("@kaholo/plugin-library");
const { CoreV1Api, KubernetesObjectApi } = require("@kubernetes/client-node");
const yaml = require("js-yaml");
const fs = require("fs");
const k8sFunctions = require("./k8s-functions");
const k8sClient = require("./k8s-client");
const kubectl = require("./kubectl");
const { mapResourceTypeToDeleteFunctionName, getDeleteApi } = require("./k8s-delete-utils");
const { validateNamespace, convertCertificateToBase64 } = require("./helpers");
async function apply(params) {
const {
kubeCertificate,
kubeApiServer,
kubeToken,
yamlPath,
namespace,
} = params;
const specs = yaml.loadAll(
fs.readFileSync(yamlPath),
).filter(
(s) => s && s.kind && s.metadata,
);
const client = k8sClient.create(KubernetesObjectApi, {
kubeCertificate: convertCertificateToBase64(kubeCertificate),
kubeApiServer,
kubeToken,
});
const specProcessingPromises = specs
.map((spec) => k8sFunctions.apply(client, {
spec,
namespace,
}));
const creationResults = await Promise.allSettled(specProcessingPromises);
if (creationResults.some((result) => result.status === "rejected")) {
// eslint-disable-next-line no-throw-literal
throw {
successes: creationResults
.filter((result) => result.status === "fulfilled")
.map((result) => result.value),
failures: creationResults
.filter((result) => result.status === "rejected")
.map((result) => result.reason),
};
}
return creationResults.map((result) => result.value);
}
async function deleteObjects(params) {
const {
kubeCertificate,
kubeApiServer,
kubeToken,
objectsMap,
namespace,
} = params;
const objectsMapDeletionPromises = objectsMap.map((mapping) => {
const [type, name] = mapping.split(" ");
const functionName = mapResourceTypeToDeleteFunctionName(type);
const api = getDeleteApi(type, functionName);
const client = k8sClient.create(api, {
kubeCertificate: convertCertificateToBase64(kubeCertificate),
kubeApiServer,
kubeToken,
});
validateNamespace(namespace, client[functionName], type);
return {
type,
name,
promise: k8sFunctions.deleteObject(client, {
functionName,
objectType: type,
objectName: name,
namespace,
}),
};
});
const results = await Promise.allSettled(objectsMapDeletionPromises.map((map) => map.promise));
if (results.some((result) => result.status === "rejected")) {
const successes = results
.filter((result) => result.status === "fulfilled")
.map((result) => result.value);
const failures = results
.filter((result) => result.status === "rejected")
.map((result) => result.reason);
// eslint-disable-next-line no-throw-literal
throw {
successes,
failures,
};
}
return results.map((result) => result.value);
}
async function getService(params) {
const {
kubeCertificate,
kubeApiServer,
kubeToken,
name,
namespace,
} = params;
const client = k8sClient.create(CoreV1Api, {
kubeCertificate: convertCertificateToBase64(kubeCertificate),
kubeApiServer,
kubeToken,
});
return k8sFunctions.getService(client, { name, namespace });
}
async function getAllServices(params) {
const {
kubeCertificate,
kubeApiServer,
kubeToken,
labelsFilter = [], // TODO remove when library starts to parse empty text into empty arrays
namespace,
} = params;
const client = k8sClient.create(CoreV1Api, {
kubeCertificate: convertCertificateToBase64(kubeCertificate),
kubeApiServer,
kubeToken,
});
return k8sFunctions.getAllServices(client, { labelsFilter, namespace });
}
module.exports = bootstrap({
apply,
deleteObjects,
getService,
getAllServices,
runKubectlCommand: kubectl.runCommand,
});