1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.setCurrentContext = exports.getCurrentContext = exports.kexec = exports.kshRestart = exports.klogs = exports.kdel = exports.kapply = exports.kcreate = void 0;
|
4 | const p_spawn_1 = require("p-spawn");
|
5 | const k8s_types_1 = require("./k8s-types");
|
6 | const realm_1 = require("./realm");
|
7 | const utils_1 = require("./utils");
|
8 |
|
9 | async function kcreate(realm, resourceNames) {
|
10 | await kubectlFile(realm, 'create', resourceNames);
|
11 | }
|
12 | exports.kcreate = kcreate;
|
13 | async function kapply(realm, resourceNames) {
|
14 | await kubectlFile(realm, 'apply', resourceNames);
|
15 | }
|
16 | exports.kapply = kapply;
|
17 |
|
18 | async function kdel(realm, resourceNames) {
|
19 | if (realm.confirmOnDelete) {
|
20 | const answer = await utils_1.prompt("Do you really want to delete? (YES to continue)");
|
21 | if (answer !== "YES") {
|
22 | console.log(`Operation not confirmed. Exiting (nothing done).`);
|
23 | return;
|
24 | }
|
25 | }
|
26 | await kubectlFile(realm, 'delete', resourceNames);
|
27 | }
|
28 | exports.kdel = kdel;
|
29 | let currentLogPodName = null;
|
30 | async function klogs(realm, resourceNames) {
|
31 | const names = await realm_1.getConfigurationNames(realm, resourceNames);
|
32 | const pods = await fetchK8sObjectsByType(realm, 'pods');
|
33 | for (let serviceName of names) {
|
34 | const kpods = await getKPods(pods, { labels: { run: `${realm.system}-${serviceName}` } });
|
35 | for (const kpod of kpods) {
|
36 | const podName = kpod.name;
|
37 | for (const ctn of kpod.containers) {
|
38 | const ctnName = ctn.name;
|
39 | const args = ['logs', '-f', podName, '-c', ctnName];
|
40 | addNamespaceIfDefined(realm, args);
|
41 | console.log(`Will output logs for -> kubectl ${args.join(' ')}`);
|
42 |
|
43 | p_spawn_1.spawn('kubectl', args, {
|
44 | detached: true,
|
45 | onStdout: function (data) {
|
46 |
|
47 | if (currentLogPodName != null && currentLogPodName !== podName) {
|
48 | console.log('-----------\n');
|
49 | }
|
50 |
|
51 | if (currentLogPodName !== podName) {
|
52 | console.log(`----- LOG for: ${serviceName} / ${podName} / ${ctnName}`);
|
53 | currentLogPodName = podName;
|
54 | }
|
55 |
|
56 | process.stdout.write(data);
|
57 | }
|
58 | });
|
59 | }
|
60 | }
|
61 | }
|
62 | }
|
63 | exports.klogs = klogs;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | async function kshRestart(realm, serviceNamesStr) {
|
73 | const serviceNames = utils_1.asNames(serviceNamesStr);
|
74 | const pods = await fetchK8sObjectsByType(realm, 'pods');
|
75 | for (let serviceName of serviceNames) {
|
76 | const podNames = await getPodNames(pods, { labels: { run: `${realm.system}-${serviceName}` } });
|
77 | for (let podName of podNames) {
|
78 |
|
79 | try {
|
80 | const args = ['exec', podName];
|
81 | addNamespaceIfDefined(realm, args);
|
82 | args.push('--', 'test', '-e', '/service/restart.sh');
|
83 | await p_spawn_1.spawn('kubectl', args, { toConsole: false });
|
84 | }
|
85 | catch (ex) {
|
86 | console.log(`Skipping service ${serviceName} - '/service/restart.sh' not found.`);
|
87 | continue;
|
88 | }
|
89 | console.log(`\n--- Restarting: ${serviceName} (pod: ${podName})`);
|
90 | const args = ['exec', podName];
|
91 | addNamespaceIfDefined(realm, args);
|
92 | args.push('--', '/service/restart.sh');
|
93 | await p_spawn_1.spawn('kubectl', args);
|
94 | console.log(`--- DONE: ${serviceName} : ${podName}`);
|
95 | }
|
96 | }
|
97 |
|
98 |
|
99 | }
|
100 | exports.kshRestart = kshRestart;
|
101 | async function kexec(realm, serviceNamesStr, commandAndArgs) {
|
102 | const serviceNames = utils_1.asNames(serviceNamesStr);
|
103 | const pods = await fetchK8sObjectsByType(realm, 'pods');
|
104 | for (let serviceName of serviceNames) {
|
105 | const podNames = await getPodNames(pods, { labels: { run: `${realm.system}-${serviceName}` } });
|
106 | for (let podName of podNames) {
|
107 | try {
|
108 | let args = ['exec', podName];
|
109 | addNamespaceIfDefined(realm, args);
|
110 | args.push('--');
|
111 | args = args.concat(commandAndArgs);
|
112 | await p_spawn_1.spawn('kubectl', args);
|
113 | }
|
114 | catch (ex) {
|
115 | console.log(`Cannot run ${commandAndArgs} on pod ${podName} because ${ex}`);
|
116 | continue;
|
117 | }
|
118 | }
|
119 | }
|
120 | }
|
121 | exports.kexec = kexec;
|
122 |
|
123 |
|
124 |
|
125 | async function getCurrentContext() {
|
126 |
|
127 | const psResult = await p_spawn_1.spawn('kubectl', ['config', 'current-context'], { capture: ['stdout', 'stderr'], ignoreFail: true });
|
128 | if (psResult.stderr) {
|
129 |
|
130 | return null;
|
131 | }
|
132 | else {
|
133 | return psResult.stdout.toString().trim();
|
134 | }
|
135 | }
|
136 | exports.getCurrentContext = getCurrentContext;
|
137 | async function setCurrentContext(name) {
|
138 |
|
139 |
|
140 | await p_spawn_1.spawn('kubectl', ['config', 'use-context', name]);
|
141 | }
|
142 | exports.setCurrentContext = setCurrentContext;
|
143 | async function kubectlFile(realm, action, resourceNames) {
|
144 | const names = await realm_1.getConfigurationNames(realm, resourceNames);
|
145 | for (let name of names) {
|
146 | const fileName = await realm_1.renderRealmFile(realm, name);
|
147 | try {
|
148 | const args = [action, '-f', fileName];
|
149 | addNamespaceIfDefined(realm, args);
|
150 | await p_spawn_1.spawn('kubectl', args);
|
151 | }
|
152 | catch (ex) {
|
153 | console.log(`Can't k${action} ${fileName}, skipping`);
|
154 | }
|
155 | console.log();
|
156 | }
|
157 | }
|
158 | function addNamespaceIfDefined(realm, args) {
|
159 | const namespace = realm.namespace;
|
160 | if (namespace) {
|
161 | args.push('--namespace', namespace);
|
162 | }
|
163 | }
|
164 |
|
165 | async function fetchK8sObjectsByType(realm, type) {
|
166 | const args = ['get', type, '-o', 'json'];
|
167 | addNamespaceIfDefined(realm, args);
|
168 | const psResult = await p_spawn_1.spawn('kubectl', args, { capture: 'stdout' });
|
169 | const podsJsonStr = psResult.stdout.toString();
|
170 | const response = JSON.parse(podsJsonStr);
|
171 | return response.items || [];
|
172 | }
|
173 |
|
174 | function getPodNames(pods, filter) {
|
175 | const kpods = getKPods(pods, filter);
|
176 | return kpods.map(kpod => kpod.name);
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function getKPods(pods, filter) {
|
184 | const kpods = [];
|
185 | for (let item of pods) {
|
186 | let pass = true;
|
187 | const itemName = item.metadata.name;
|
188 |
|
189 | if (filter && filter.imageName) {
|
190 |
|
191 | const itemImageName = (item.spec && item.spec.containers) ? item.spec.containers[0].image : null;
|
192 | if (itemImageName != null && itemImageName.startsWith(filter.imageName)) {
|
193 | pass = true;
|
194 | }
|
195 | else {
|
196 |
|
197 | continue;
|
198 | }
|
199 | }
|
200 |
|
201 | if (filter && filter.labels) {
|
202 | for (let labelName in filter.labels) {
|
203 | if (item.metadata.labels[labelName] === filter.labels[labelName]) {
|
204 | pass = true;
|
205 | }
|
206 | else {
|
207 | pass = false;
|
208 | continue;
|
209 | }
|
210 | }
|
211 | }
|
212 |
|
213 | if (pass) {
|
214 | kpods.push(k8s_types_1.toKPod(item));
|
215 | }
|
216 | }
|
217 | return kpods;
|
218 | }
|
219 |
|