-
Notifications
You must be signed in to change notification settings - Fork 3
/
k8s.ts
781 lines (730 loc) · 29.5 KB
/
k8s.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { sleep } from "@atomist/automation-client/lib/internal/util/poll";
import { guid } from "@atomist/automation-client/lib/internal/util/string";
import { GitCommandGitProject } from "@atomist/automation-client/lib/project/git/GitCommandGitProject";
import { GitProject } from "@atomist/automation-client/lib/project/git/GitProject";
import { minimalClone } from "@atomist/sdm/lib/api-helper/goal/minimalClone";
import { RepoContext } from "@atomist/sdm/lib/api/context/SdmContext";
import { ExecuteGoalResult } from "@atomist/sdm/lib/api/goal/ExecuteGoalResult";
import {
ExecuteGoal,
GoalProjectListenerEvent,
GoalProjectListenerRegistration,
} from "@atomist/sdm/lib/api/goal/GoalInvocation";
import {
GoalWithFulfillment,
ImplementationRegistration,
} from "@atomist/sdm/lib/api/goal/GoalWithFulfillment";
import { SdmGoalEvent } from "@atomist/sdm/lib/api/goal/SdmGoalEvent";
import { GoalScheduler } from "@atomist/sdm/lib/api/goal/support/GoalScheduler";
import { ServiceRegistrationGoalDataKey } from "@atomist/sdm/lib/api/registration/ServiceRegistration";
import { ProgressLog } from "@atomist/sdm/lib/spi/log/ProgressLog";
import * as k8s from "@kubernetes/client-node";
import * as fs from "fs-extra";
import * as stringify from "json-stringify-safe";
import * as _ from "lodash";
import * as os from "os";
import * as path from "path";
import * as request from "request";
import { Writable } from "stream";
import {
DeepPartial,
Merge,
} from "ts-essentials";
import { loadKubeConfig } from "../../pack/k8s/config";
import {
k8sJobEnv,
KubernetesGoalScheduler,
readNamespace,
} from "../../pack/k8s/KubernetesGoalScheduler";
import {
K8sServiceRegistrationType,
K8sServiceSpec,
} from "../../pack/k8s/service";
import { SdmGoalState } from "../../typings/types";
import { toArray } from "../../util/misc/array";
import {
CacheEntry,
CacheOutputGoalDataKey,
cachePut,
cacheRestore,
} from "../cache/goalCaching";
import {
Container,
ContainerInput,
ContainerOutput,
ContainerProjectHome,
ContainerRegistration,
ContainerRegistrationGoalDataKey,
ContainerScheduler,
GoalContainer,
GoalContainerVolume,
} from "./container";
import { prepareSecrets } from "./provider";
import {
containerEnvVars,
prepareInputAndOutput,
processResult,
} from "./util";
// tslint:disable:max-file-line-count
/** Merge of base and Kubernetes goal container interfaces. */
export type K8sGoalContainer =
Merge<GoalContainer, DeepPartial<k8s.V1Container>>
& Pick<GoalContainer, "name" | "image">;
/** Merge of base and Kubernetes goal container volume interfaces. */
export type K8sGoalContainerVolume = Merge<k8s.V1Volume, GoalContainerVolume>;
/** @deprecated use K8sContainerSpecCallback */
export type K8sGoalContainerSpec = Omit<K8sContainerRegistration, "callback">;
/**
* Function signature for callback that can modify and return the
* [[ContainerRegistration]] object.
*/
export type K8sContainerSpecCallback =
(r: K8sContainerRegistration, p: GitProject, g: Container, e: SdmGoalEvent, ctx: RepoContext) =>
Promise<Omit<K8sContainerRegistration, "callback">>;
/**
* Additional options for Kubernetes implementation of container goals.
*/
export interface K8sContainerRegistration extends ContainerRegistration {
/**
* Replace generic containers in [[ContainerRegistration]] with
* Kubernetes containers.
*
* Containers to run for this goal. The goal result is based on
* the exit status of the first element of the `containers` array.
* The other containers are considered "sidecar" containers
* provided functionality that the main container needs to
* function. If not set, the working directory of the first
* container is set to [[ContainerProjectHome]], which contains
* the project upon which the goal should operate. If
* `workingDir` is set, it is not changed. If `workingDir` is set
* to the empty string, the `workingDir` property is deleted from
* the main container spec, meaning the container default working
* directory will be used.
*/
containers: K8sGoalContainer[];
/**
* Replace generic callback in [[ContainerRegistration]] with
* Kubernetes-specific callback.
*/
callback?: K8sContainerSpecCallback;
/**
* Init containers to run for this goal. Any containers provided
* here will run after the one inserted by the SDM to manage the
* cloned repository.
*/
initContainers?: k8s.V1Container[];
/**
* Replace generic volumes in [[ContainerRegistration]] with
* Kubernetes volumes available to mount in containers.
*/
volumes?: K8sGoalContainerVolume[];
}
export const k8sContainerScheduler: ContainerScheduler = (goal, registration: K8sContainerRegistration) => {
goal.addFulfillment({
goalExecutor: executeK8sJob(),
...registration as ImplementationRegistration,
});
goal.addFulfillmentCallback({
goal,
callback: k8sFulfillmentCallback(goal, registration),
});
};
export const k8sSkillContainerScheduler: ContainerScheduler = (goal, registration: K8sContainerRegistration) => {
goal.addFulfillment({
goalExecutor: executeK8sJob(),
...registration as ImplementationRegistration,
});
};
/**
* Add Kubernetes job scheduling information to SDM goal event data
* for use by the [[KubernetesGoalScheduler]].
*/
export function k8sFulfillmentCallback(
goal: Container,
registration: K8sContainerRegistration,
): (sge: SdmGoalEvent, rc: RepoContext) => Promise<SdmGoalEvent> {
// tslint:disable-next-line:cyclomatic-complexity
return async (goalEvent, repoContext) => {
let spec: K8sContainerRegistration = _.cloneDeep(registration);
if (registration.callback) {
spec = await repoContext.configuration.sdm.projectLoader.doWithProject({
...repoContext,
readOnly: true,
cloneOptions: minimalClone(goalEvent.push, { detachHead: true }),
}, async p => {
return {
...spec,
...(await registration.callback(_.cloneDeep(registration), p, goal, goalEvent, repoContext)) || {},
};
});
}
if (!spec.containers || spec.containers.length < 1) {
throw new Error("No containers defined in K8sGoalContainerSpec");
}
// Preserve the container registration in the goal data before it gets munged with internals
let data = parseGoalEventData(goalEvent);
let newData: any = {};
delete spec.callback;
_.set<any>(newData, ContainerRegistrationGoalDataKey, spec);
goalEvent.data = JSON.stringify(_.merge(data, newData));
if (spec.containers[0].workingDir === "") {
delete spec.containers[0].workingDir;
} else if (!spec.containers[0].workingDir) {
spec.containers[0].workingDir = ContainerProjectHome;
}
const goalSchedulers: GoalScheduler[] = toArray(repoContext.configuration.sdm.goalScheduler) || [];
const k8sScheduler = goalSchedulers.find(gs => gs instanceof KubernetesGoalScheduler) as KubernetesGoalScheduler;
if (!k8sScheduler) {
throw new Error("Failed to find KubernetesGoalScheduler in goal schedulers");
}
if (!k8sScheduler.podSpec) {
throw new Error("KubernetesGoalScheduler has no podSpec defined");
}
const containerEnvs = await containerEnvVars(goalEvent, repoContext);
const projectVolume = `project-${guid().split("-")[0]}`;
const inputVolume = `input-${guid().split("-")[0]}`;
const outputVolume = `output-${guid().split("-")[0]}`;
const ioVolumes = [
{
name: projectVolume,
emptyDir: {},
},
{
name: inputVolume,
emptyDir: {},
},
{
name: outputVolume,
emptyDir: {},
},
];
const ioVolumeMounts = [
{
mountPath: ContainerProjectHome,
name: projectVolume,
},
{
mountPath: ContainerInput,
name: inputVolume,
},
{
mountPath: ContainerOutput,
name: outputVolume,
},
];
const copyContainer = _.cloneDeep(k8sScheduler.podSpec.spec.containers[0]);
delete copyContainer.lifecycle;
delete copyContainer.livenessProbe;
delete copyContainer.readinessProbe;
copyContainer.name = `container-goal-init-${guid().split("-")[0]}`;
copyContainer.env = [
...(copyContainer.env || []),
...k8sJobEnv(k8sScheduler.podSpec, goalEvent, repoContext.context as any),
...containerEnvs,
{
name: "ATOMIST_ISOLATED_GOAL_INIT",
value: "true",
},
{
name: "ATOMIST_CONFIG",
value: JSON.stringify({
cluster: {
enabled: false,
},
ws: {
enabled: false,
},
}),
},
];
spec.initContainers = spec.initContainers || [];
const parameters = JSON.parse((goalEvent as any).parameters || "{}");
const secrets = await prepareSecrets(
_.merge({}, registration.containers[0], (parameters["@atomist/sdm/secrets"] || {})), repoContext);
delete spec.containers[0].secrets;
[...spec.containers, ...spec.initContainers].forEach(c => {
c.env = [
...(secrets.env || []),
...containerEnvs,
...(c.env || []),
];
});
if (!!secrets?.files) {
for (const file of secrets.files) {
const fileName = path.basename(file.mountPath);
const dirname = path.dirname(file.mountPath);
let secretName = `secret-${guid().split("-")[0]}`;
const vm = (copyContainer.volumeMounts || [])
.find(m => m.mountPath === dirname);
if (!!vm) {
secretName = vm.name;
} else {
copyContainer.volumeMounts = [
...(copyContainer.volumeMounts || []),
{
mountPath: dirname,
name: secretName,
},
];
spec.volumes = [
...(spec.volumes || []),
{
name: secretName,
emptyDir: {},
} as any,
];
}
[...spec.containers, ...spec.initContainers].forEach((c: k8s.V1Container) => {
c.volumeMounts = [
...(c.volumeMounts || []),
{
mountPath: file.mountPath,
name: secretName,
subPath: fileName,
},
];
});
}
}
spec.initContainers = [
copyContainer,
...spec.initContainers,
];
const serviceSpec: { type: string, spec: K8sServiceSpec } = {
type: K8sServiceRegistrationType.K8sService,
spec: {
container: spec.containers,
initContainer: spec.initContainers,
volume: [
...ioVolumes,
...(spec.volumes || []),
],
volumeMount: ioVolumeMounts,
},
};
// Store k8s service registration in goal data
data = JSON.parse(goalEvent.data || "{}");
newData = {};
_.set<any>(newData, `${ServiceRegistrationGoalDataKey}.${registration.name}`, serviceSpec);
goalEvent.data = JSON.stringify(_.merge(data, newData));
return goalEvent;
};
}
/**
* Get container registration from goal event data, use
* [[k8sFulfillmentcallback]] to get a goal event schedulable by a
* [[KubernetesGoalScheduler]], then schedule the goal using that
* scheduler.
*/
export const scheduleK8sJob: ExecuteGoal = async gi => {
const { goalEvent } = gi;
const { uniqueName } = goalEvent;
const data = parseGoalEventData(goalEvent);
const containerReg: K8sContainerRegistration = data["@atomist/sdm/container"];
if (!containerReg) {
throw new Error(`Goal ${uniqueName} event data has no container spec: ${goalEvent.data}`);
}
const goalSchedulers: GoalScheduler[] = toArray(gi.configuration.sdm.goalScheduler) || [];
const k8sScheduler = goalSchedulers.find(gs => gs instanceof KubernetesGoalScheduler) as KubernetesGoalScheduler;
if (!k8sScheduler) {
throw new Error(`Failed to find KubernetesGoalScheduler in goal schedulers: ${stringify(goalSchedulers)}`);
}
// the k8sFulfillmentCallback may already have been called, so wipe it out
delete data[ServiceRegistrationGoalDataKey];
goalEvent.data = JSON.stringify(data);
try {
const schedulableGoalEvent = await k8sFulfillmentCallback(gi.goal as Container, containerReg)(goalEvent, gi);
const scheduleResult = await k8sScheduler.schedule({ ...gi, goalEvent: schedulableGoalEvent });
if (scheduleResult.code) {
return {
...scheduleResult,
message: `Failed to schedule container goal ${uniqueName}: ${scheduleResult.message}`,
};
}
schedulableGoalEvent.state = SdmGoalState.in_process;
return schedulableGoalEvent;
} catch (e) {
const message = `Failed to schedule container goal ${uniqueName} as Kubernetes job: ${e.message}`;
gi.progressLog.write(message);
return { code: 1, message };
}
};
/** Container information useful the various functions. */
interface K8sContainer {
/** Kubernetes configuration to use when creating API clients */
config: k8s.KubeConfig;
/** Name of container in pod */
name: string;
/** Pod name */
pod: string;
/** Pod namespace */
ns: string;
/** Log */
log: ProgressLog;
}
/**
* Wait for first container to exit and stream its logs to the
* progress log.
*/
export function executeK8sJob(): ExecuteGoal {
// tslint:disable-next-line:cyclomatic-complexity
return async gi => {
const { goalEvent, progressLog, configuration, id, credentials } = gi;
const projectDir = process.env.ATOMIST_PROJECT_DIR || ContainerProjectHome;
const inputDir = process.env.ATOMIST_INPUT_DIR || ContainerInput;
const outputDir = process.env.ATOMIST_OUTPUT_DIR || ContainerOutput;
const data = parseGoalEventData(goalEvent);
if (!data[ContainerRegistrationGoalDataKey]) {
throw new Error("Failed to read k8s ContainerRegistration from goal data");
}
if (!data[ContainerRegistrationGoalDataKey]) {
throw new Error(`Goal ${gi.goal.uniqueName} has no Kubernetes container registration: ${gi.goalEvent.data}`);
}
const registration: K8sContainerRegistration = data[ContainerRegistrationGoalDataKey];
if (process.env.ATOMIST_ISOLATED_GOAL_INIT === "true") {
return configuration.sdm.projectLoader.doWithProject({
...gi,
readOnly: false,
cloneDir: projectDir,
cloneOptions: minimalClone(goalEvent.push, { detachHead: true }),
}, async project => {
try {
await prepareInputAndOutput(inputDir, outputDir, gi);
} catch (e) {
const message = `Failed to prepare input and output for goal ${goalEvent.name}: ${e.message}`;
progressLog.write(message);
return { code: 1, message };
}
const secrets = await prepareSecrets(
_.merge({}, registration.containers[0], ((gi.parameters || {})["@atomist/sdm/secrets"] || {})), gi);
if (!!secrets?.files) {
for (const file of secrets.files) {
await fs.writeFile(file.mountPath, file.value);
}
}
goalEvent.state = SdmGoalState.in_process;
return goalEvent;
});
}
let containerName: string = _.get(registration, "containers[0].name");
if (!containerName) {
const msg = `Failed to get main container name from goal registration: ${stringify(registration)}`;
progressLog.write(msg);
let svcSpec: K8sServiceSpec;
try {
svcSpec = _.get(data, `${ServiceRegistrationGoalDataKey}.${registration.name}.spec`);
} catch (e) {
const message = `Failed to parse Kubernetes spec from goal data '${goalEvent.data}': ${e.message}`;
progressLog.write(message);
return { code: 1, message };
}
containerName = _.get(svcSpec, "container[1].name");
if (!containerName) {
const message = `Failed to get main container name from either goal registration or data: '${goalEvent.data}'`;
progressLog.write(message);
return { code: 1, message };
}
}
const ns = await readNamespace();
const podName = os.hostname();
let kc: k8s.KubeConfig;
try {
kc = loadKubeConfig();
} catch (e) {
const message = `Failed to load Kubernetes configuration: ${e.message}`;
progressLog.write(message);
return { code: 1, message };
}
const container: K8sContainer = {
config: kc,
name: containerName,
pod: podName,
ns,
log: progressLog,
};
try {
await containerStarted(container);
} catch (e) {
const message = `Failed to determine if container started: ${e.message}`;
progressLog.write(message);
return { code: 1, message };
}
const log = followK8sLog(container);
const status = { code: 0, message: `Container '${containerName}' completed successfully` };
try {
const timeout: number = _.get(configuration, "sdm.goal.timeout", 10 * 60 * 1000);
const podStatus = await containerWatch(container, timeout);
progressLog.write(`Container '${containerName}' exited: ${stringify(podStatus)}`);
} catch (e) {
const message = `Container '${containerName}' failed: ${e.message}`;
progressLog.write(message);
status.code++;
status.message = message;
}
// Give the logs some time to be delivered
await sleep(1000);
log.abort();
const outputFile = path.join(outputDir, "result.json");
let outputResult: ExecuteGoalResult;
if (status.code === 0 && (await fs.pathExists(outputFile))) {
try {
outputResult = await processResult(await fs.readJson(outputFile), gi);
} catch (e) {
const message = `Failed to read output from container: ${e.message}`;
progressLog.write(message);
status.code++;
status.message += ` but f${message.slice(1)}`;
}
}
const cacheEntriesToPut: CacheEntry[] = [
...(registration.output || []),
...((gi.parameters || {})[CacheOutputGoalDataKey] || []),
];
if (cacheEntriesToPut.length > 0) {
try {
const project = GitCommandGitProject.fromBaseDir(id, projectDir, credentials, async () => {
});
const cp = cachePut({
entries: cacheEntriesToPut.map(e => {
// Prevent the type on the entry to get passed along when goal actually failed
if (status.code !== 0) {
return {
classifier: e.classifier,
pattern: e.pattern,
};
} else {
return e;
}
}),
});
await cp.listener(project, gi, GoalProjectListenerEvent.after);
} catch (e) {
const message = `Failed to put cache output from container: ${e.message}`;
progressLog.write(message);
status.code++;
status.message += ` but f${message.slice(1)}`;
}
}
return outputResult || status;
};
}
/**
* Read and parse container goal registration from goal event data.
*/
export function parseGoalEventData(goalEvent: SdmGoalEvent): any {
const goalName = goalEvent.uniqueName;
if (!goalEvent || !goalEvent.data) {
return {};
}
let data: any;
try {
data = JSON.parse(goalEvent.data);
} catch (e) {
e.message = `Failed to parse goal event data for ${goalName} as JSON '${goalEvent.data}': ${e.message}`;
throw e;
}
return data;
}
/**
* If running as isolated goal, use [[executeK8sJob]] to execute the
* goal. Otherwise, schedule the goal execution as a Kubernetes job
* using [[scheduleK8sJob]].
*/
const containerExecutor: ExecuteGoal = gi => (process.env.ATOMIST_ISOLATED_GOAL) ? executeK8sJob()(gi) : scheduleK8sJob(gi);
/**
* Restore cache input entries before fulfilling goal.
*/
const containerFulfillerCacheRestore: GoalProjectListenerRegistration = {
name: "cache restore",
events: [GoalProjectListenerEvent.before],
listener: async (project, gi) => {
const data = parseGoalEventData(gi.goalEvent);
if (!data[ContainerRegistrationGoalDataKey]) {
throw new Error(`Goal ${gi.goal.uniqueName} has no Kubernetes container registration: ${gi.goalEvent.data}`);
}
const registration: K8sContainerRegistration = data[ContainerRegistrationGoalDataKey];
if (registration.input && registration.input.length > 0) {
try {
const cp = cacheRestore({ entries: registration.input });
return cp.listener(project, gi, GoalProjectListenerEvent.before);
} catch (e) {
const message = `Failed to restore cache input to container for goal ${gi.goal.uniqueName}: ${e.message}`;
gi.progressLog.write(message);
return { code: 1, message };
}
} else {
return { code: 0, message: "No container input cache entries to restore" };
}
},
};
/** Deterministic name for Kubernetes container goal fulfiller. */
export const K8sContainerFulfillerName = "Kubernetes Container Goal Fulfiller";
/**
* Goal that fulfills requested container goals by scheduling them as
* Kubernetes jobs.
*/
export function k8sContainerFulfiller(): GoalWithFulfillment {
return new GoalWithFulfillment({
displayName: K8sContainerFulfillerName,
uniqueName: K8sContainerFulfillerName,
})
.with({
goalExecutor: containerExecutor,
name: `${K8sContainerFulfillerName} Executor`,
})
.withProjectListener(containerFulfillerCacheRestore);
}
/**
* Wait for container in pod to start, return when it does.
*
* @param container Information about container to check
* @param attempts Maximum number of attempts, waiting 500 ms between
*/
async function containerStarted(container: K8sContainer, attempts: number = 240): Promise<void> {
let core: k8s.CoreV1Api;
try {
core = container.config.makeApiClient(k8s.CoreV1Api);
} catch (e) {
e.message = `Failed to create Kubernetes core API client: ${e.message}`;
container.log.write(e.message);
throw e;
}
const sleepTime = 500; // ms
for (let i = 0; i < attempts; i++) {
await sleep(500);
let pod: k8s.V1Pod;
try {
pod = (await core.readNamespacedPod(container.pod, container.ns)).body;
} catch (e) {
container.log.write(`Reading pod ${container.ns}/${container.pod} failed: ${k8sErrMsg(e)}`);
continue;
}
const containerStatus = pod.status.containerStatuses.find(c => c.name === container.name);
if (containerStatus && (!!_.get(containerStatus, "state.running.startedAt") || !!_.get(containerStatus, "state.terminated"))) {
const message = `Container '${container.name}' started`;
container.log.write(message);
return;
}
}
const errMsg = `Container '${container.name}' failed to start within ${attempts * sleepTime} ms`;
container.log.write(errMsg);
throw new Error(errMsg);
}
/**
* Watch pod until container `container.name` exits. Resolve promise
* with status if container `container.name` exits with status 0.
* Reject promise otherwise, including pod status in the `podStatus`
* property of the error.
*
* @param container Information about container to watch
* @return Status of pod after container terminates
*/
function containerWatch(container: K8sContainer, timeout: number): Promise<k8s.V1PodStatus> {
return new Promise(async (resolve, reject) => {
let watch: k8s.Watch;
try {
watch = new k8s.Watch(container.config);
} catch (e) {
e.message = `Failed to create Kubernetes watch client: ${e.message}`;
container.log.write(e.message);
reject(e);
}
let watcher: any;
const timeoutTimer = setTimeout(() => {
if (watcher?.abort) {
watcher.abort();
}
reject(new Error(`Goal timeout '${timeout}' exceeded`));
}, timeout);
const watchPath = `/api/v1/watch/namespaces/${container.ns}/pods/${container.pod}`;
watcher = await watch.watch(watchPath, {}, (phase, obj) => {
const pod = obj as k8s.V1Pod;
if (pod?.status?.containerStatuses) {
const containerStatus = pod.status.containerStatuses.find(c => c.name === container.name);
if (containerStatus?.state?.terminated) {
if (timeoutTimer) {
clearTimeout(timeoutTimer);
}
const exitCode: number = _.get(containerStatus, "state.terminated.exitCode");
if (exitCode === 0) {
const msg = `Container '${container.name}' exited with status 0`;
container.log.write(msg);
resolve(pod.status);
} else {
const msg = `Container '${container.name}' exited with status ${exitCode}`;
container.log.write(msg);
const err = new Error(msg);
(err as any).podStatus = pod.status;
reject(err);
}
if (watcher?.abort) {
watcher.abort();
}
return;
}
}
container.log.write(`Container '${container.name}' still running`);
}, err => {
if (timeoutTimer) {
clearTimeout(timeoutTimer);
}
err.message = `Container watcher failed: ${err.message}`;
container.log.write(err.message);
reject(err);
});
});
}
/**
* Set up log follower for container.
*/
function followK8sLog(container: K8sContainer): request.Request {
const k8sLog = new k8s.Log(container.config);
const logStream = new Writable({
write: (chunk, encoding, callback) => {
container.log.write(chunk.toString());
callback();
},
});
const doneCallback = e => {
if (e) {
if (e.message) {
container.log.write(e.message);
} else {
container.log.write(stringify(e));
}
}
};
const logOptions: k8s.LogOptions = { follow: true };
return k8sLog.log(container.ns, container.pod, container.name, logStream, doneCallback, logOptions);
}
/** Try to find a Kubernetes API error message. */
export function k8sErrMsg(e: any): string {
if (e.message && typeof e.message === "string") {
return e.message;
} else if (e.body && typeof e.body === "string") {
return e.body;
} else if (e.body?.message && typeof e.body.message === "string") {
return e.body.message;
} else if (e.response?.body && typeof e.response.body === "string") {
return e.response.body;
} else if (e.response?.body?.message && typeof e.response.body.message === "string") {
return e.response.body.message;
} else {
return "Kubernetes API request error";
}
}