-
Notifications
You must be signed in to change notification settings - Fork 267
/
api.ts
1018 lines (894 loc) · 27.7 KB
/
api.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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { IncomingMessage } from "http"
import { ReadStream } from "tty"
import {
ApiextensionsV1Api,
ApisApi,
AppsV1Api,
CoreApi,
CoreV1Api,
Exec,
KubeConfig,
KubernetesObject,
Log as K8sLog,
NetworkingV1Api,
PolicyV1Api,
RbacAuthorizationV1Api,
V1APIGroup,
V1APIResource,
V1APIVersions,
V1Deployment,
V1Secret,
V1Service,
} from "@kubernetes/client-node"
import { load } from "js-yaml"
import { readFile } from "fs-extra"
import WebSocket from "isomorphic-ws"
import pRetry from "p-retry"
import { Omit, StringCollector } from "../../util/util"
import { flatten, isPlainObject, keyBy } from "lodash"
import { ConfigurationError, GardenError, GardenErrorParams, RuntimeError } from "../../exceptions"
import {
BaseResource,
KubernetesList,
KubernetesPod,
KubernetesResource,
KubernetesServerList,
KubernetesServerResource,
} from "./types"
import { Log } from "../../logger/log-entry"
import { kubectl } from "./kubectl"
import { urlJoin } from "../../util/string"
import { KubernetesProvider } from "./config"
import { StringMap } from "../../config/common"
import { PluginContext } from "../../plugin-context"
import { PassThrough, Readable, Writable } from "stream"
import { getExecExitCode } from "./status/pod"
import { labelSelectorToString } from "./util"
import { safeDumpYaml } from "../../util/serialization"
import AsyncLock from "async-lock"
import { requestWithRetry, RetryOpts, toKubernetesError } from "./retry"
import request = require("request-promise")
interface ApiGroupMap {
[groupVersion: string]: V1APIGroup
}
interface ApiResourceMap {
[group: string]: { [kind: string]: V1APIResource }
}
interface ApiInfo {
coreApi: V1APIVersions
groups: V1APIGroup[]
groupMap: ApiGroupMap
}
const cachedConfigs: { [context: string]: KubeConfig } = {}
const cachedApiInfo: { [context: string]: ApiInfo } = {}
const cachedApiResourceInfo: { [context: string]: ApiResourceMap } = {}
const apiInfoLock = new AsyncLock()
// NOTE: be warned, the API of the client library is very likely to change
type K8sApi =
| ApiextensionsV1Api
| AppsV1Api
| CoreApi
| CoreV1Api
| NetworkingV1Api
| PolicyV1Api
| RbacAuthorizationV1Api
type K8sApiConstructor<T extends K8sApi> = new (basePath?: string) => T
const apiTypes: { [key: string]: K8sApiConstructor<any> } = {
apis: ApisApi,
apps: AppsV1Api,
core: CoreV1Api,
coreApi: CoreApi,
extensions: ApiextensionsV1Api,
networking: NetworkingV1Api,
policy: PolicyV1Api,
rbac: RbacAuthorizationV1Api,
}
const crudMap = {
Deployment: {
cls: new V1Deployment(),
group: "apps",
read: "readNamespacedDeployment",
create: "createNamespacedDeployment",
replace: "replaceNamespacedDeployment",
delete: "deleteNamespacedDeployment",
patch: "patchNamespacedDeployment",
},
Secret: {
cls: new V1Secret(),
group: "core",
read: "readNamespacedSecret",
create: "createNamespacedSecret",
replace: "replaceNamespacedSecret",
delete: "deleteNamespacedSecret",
patch: "patchNamespacedSecret",
},
Service: {
cls: new V1Service(),
group: "core",
read: "readNamespacedService",
create: "createNamespacedService",
replace: null,
delete: "deleteNamespacedService",
patch: "patchNamespacedService",
},
}
type CrudMap = typeof crudMap
type CrudMapTypes = { [T in keyof CrudMap]: CrudMap[T]["cls"] }
export class KubernetesError extends GardenError {
type = "kubernetes"
/**
* HTTP response status code
*/
responseStatusCode: number | undefined
/**
* If the Kubernetes API response body contained a message, it will be stored here.
*/
apiMessage: string | undefined
constructor(params: GardenErrorParams & { responseStatusCode?: number; apiMessage?: string }) {
super(params)
this.responseStatusCode = params.responseStatusCode
this.apiMessage = params.apiMessage
}
}
interface List {
items?: Array<any>
}
type WrappedList<T extends List> = T["items"] extends Array<infer V extends BaseResource | KubernetesObject>
? KubernetesServerList<V>
: KubernetesServerList
// This describes the API classes on KubeApi after they've been wrapped with KubeApi.wrapApi()
// prettier-ignore
type WrappedApi<T> = {
// Wrap each API method
[P in keyof T]:
T[P] extends (...args: infer A) => Promise<{ response: IncomingMessage; body: infer U }>
? (
// If so we wrap it and return the `body` part of the output directly and...
// If it's a list, we cast to a KubernetesServerList, which in turn wraps the array type
U extends List ? (...args: A) => Promise<WrappedList<U>> :
// If it's a resource, we wrap it as a KubernetesResource which makes some attributes required
// (as they should be)
U extends KubernetesObject ? (...args: A) => Promise<KubernetesServerResource<U>> :
// Otherwise we keep the body output type as-is
(...args: A) => Promise<U>
) :
T[P]
}
export interface ExecInPodResult {
exitCode?: number
allLogs: string
stdout: string
stderr: string
timedOut: boolean
}
interface ReadParams {
log: Log
namespace: string
apiVersion: string
kind: string
name: string
}
interface ReadBySpecParams {
log: Log
namespace: string
manifest: KubernetesResource
}
async function nullIfNotFound<T>(fn: () => Promise<T>) {
try {
const resource = await fn()
return resource
} catch (err) {
if (!(err instanceof KubernetesError)) {
throw err
}
if (err.responseStatusCode === 404) {
return null
} else {
throw err
}
}
}
export class KubeApi {
public apis: WrappedApi<ApisApi>
public apps: WrappedApi<AppsV1Api>
public core: WrappedApi<CoreV1Api>
public coreApi: WrappedApi<CoreApi>
public extensions: WrappedApi<ApiextensionsV1Api>
public networking: WrappedApi<NetworkingV1Api>
public policy: WrappedApi<PolicyV1Api>
public rbac: WrappedApi<RbacAuthorizationV1Api>
constructor(
public log: Log,
public context: string,
private config: KubeConfig
) {
const cluster = this.config.getCurrentCluster()
if (!cluster) {
throw new ConfigurationError({
message: `Could not read cluster from kubeconfig for context ${context}`,
})
}
for (const [name, cls] of Object.entries(apiTypes)) {
const api = new cls(cluster.server)
this[name] = this.wrapApi(log, api, this.config)
}
}
static async factory(log: Log, ctx: PluginContext, provider: KubernetesProvider) {
const config = await getContextConfig(log, ctx, provider)
return new KubeApi(log, provider.config.context, config)
}
async getApiInfo(): Promise<ApiInfo> {
if (cachedApiInfo[this.context]) {
return cachedApiInfo[this.context]
}
return apiInfoLock.acquire(this.context, async () => {
if (cachedApiInfo[this.context] === undefined) {
const coreApi = await this.coreApi.getAPIVersions()
const apis = await this.apis.getAPIVersions()
const coreGroups: V1APIGroup[] = coreApi.versions.map((version) => ({
apiVersion: "v1",
kind: "ApiGroup",
name: version,
preferredVersion: {
groupVersion: version,
version,
},
versions: [
{
groupVersion: "core/" + version,
version: "core/" + version,
},
{
groupVersion: version,
version,
},
],
serverAddressByClientCIDRs: coreApi.serverAddressByClientCIDRs,
}))
const groups = coreGroups.concat(apis.groups)
const groupMap: ApiGroupMap = {}
for (const group of groups) {
for (const version of group.versions) {
groupMap[version.groupVersion] = group
}
}
cachedApiInfo[this.context] = { coreApi, groups, groupMap }
}
return cachedApiInfo[this.context]
})
}
async getApiResourceInfo(log: Log, apiVersion: string, kind: string): Promise<V1APIResource> {
if (!cachedApiResourceInfo[this.context]) {
cachedApiResourceInfo[this.context] = {}
}
const apiResources = cachedApiResourceInfo[this.context]
const lockKey = `${this.context}/${apiVersion}`
const resourceMap =
apiResources[apiVersion] ||
(await apiInfoLock.acquire(lockKey, async () => {
if (apiResources[apiVersion]) {
return apiResources[apiVersion]
}
log.debug(`Kubernetes: Getting API resource info for group ${apiVersion}`)
try {
const res = await this.request({ log, path: getGroupBasePath(apiVersion) })
// We're only interested in the entities themselves, not the sub-resources
const resources = res.body.resources.filter((r: any) => !r.name.includes("/"))
apiResources[apiVersion] = keyBy(resources, "kind")
return apiResources[apiVersion]
} catch (err) {
if (!(err instanceof KubernetesError)) {
throw err
}
if (err.responseStatusCode === 404) {
// Could not find the resource type
return {}
} else {
throw err
}
}
}))
return resourceMap[kind]
}
async request({
log,
path,
opts = {},
retryOpts,
}: {
log: Log
path: string
opts?: Omit<request.OptionsWithUrl, "url">
retryOpts?: RetryOpts
}): Promise<any> {
const baseUrl = this.config.getCurrentCluster()!.server
const url = urlJoin(baseUrl, path)
// set some default values
const requestOpts = {
url,
method: "get",
json: true,
resolveWithFullResponse: true,
...opts,
}
// apply auth
await this.config.applyToRequest(requestOpts)
const context = `Kubernetes API: ${path}`
return await requestWithRetry(
log,
context,
async () => {
try {
log.silly(`${requestOpts.method.toUpperCase()} ${url}`)
return await request(requestOpts)
} catch (err) {
throw toKubernetesError(err, context)
}
},
retryOpts
)
}
/**
* Fetch the specified resource from the cluster.
*/
async read({ log, namespace, apiVersion, kind, name }: ReadParams) {
log.silly(`Fetching Kubernetes resource ${apiVersion}/${kind}/${name}`)
const typePath = await this.getResourceTypeApiPath({
log,
apiVersion,
kind,
namespace,
})
const apiPath = typePath + "/" + name
const res = await this.request({ log, path: apiPath })
return res.body
}
async readOrNull(params: ReadParams) {
return await nullIfNotFound(() => this.read(params))
}
/**
* Given a manifest, attempt to read the matching resource from the cluster.
*/
async readBySpec({ log, namespace, manifest }: ReadBySpecParams) {
log.silly(`Fetching Kubernetes resource ${manifest.apiVersion}/${manifest.kind}/${manifest.metadata.name}`)
const apiPath = await this.getResourceApiPathFromManifest({ manifest, log, namespace })
const res = await this.request({ log, path: apiPath })
return res.body
}
/**
* Same as readBySpec() but returns null if the resource is missing.
*/
async readBySpecOrNull(params: ReadBySpecParams) {
return await nullIfNotFound(() => this.readBySpec(params))
}
async listResources<T extends KubernetesResource>({
log,
apiVersion,
kind,
namespace,
labelSelector,
}: {
log: Log
apiVersion: string
kind: string
namespace: string
labelSelector?: { [label: string]: string }
}) {
const apiPath = await this.getResourceTypeApiPath({ log, apiVersion, kind, namespace })
const labelSelectorString = labelSelector ? labelSelectorToString(labelSelector) : undefined
const res = await this.request({ log, path: apiPath, opts: { qs: { labelSelector: labelSelectorString } } })
const list = res.body as KubernetesList<T>
// This fixes an odd issue where apiVersion and kind are sometimes missing from list items coming from the API :/
list.items = list.items.map((r) => ({
...r,
apiVersion: r.apiVersion || apiVersion,
kind: r.kind || kind,
}))
return list
}
/**
* Fetches all resources in the namespace matching the provided API version + kind pairs, optionally filtered by
* `labelSelector`.
*
* Useful when resources of several kinds need to be fetched at once.
*/
async listResourcesForKinds({
log,
namespace,
versionedKinds,
labelSelector,
}: {
log: Log
namespace: string
versionedKinds: { apiVersion: string; kind: string }[]
labelSelector?: { [label: string]: string }
}): Promise<KubernetesResource[]> {
const resources = await Promise.all(
versionedKinds.map(async ({ apiVersion, kind }) => {
try {
const resourceListForKind = await this.listResources({
log,
apiVersion,
kind,
namespace,
labelSelector,
})
return resourceListForKind.items
} catch (err) {
if (!(err instanceof KubernetesError)) {
throw err
}
if (err.responseStatusCode === 404) {
// Then this resource version + kind is not available in the cluster.
return []
}
// FIXME: OpenShift: developers have more restrictions on what they can list
// Ugly workaround right now, basically just shoving the problem under the rug.
const openShiftForbiddenList = ["Namespace", "PersistentVolume"]
if (err.responseStatusCode === 403 && openShiftForbiddenList.includes(kind)) {
log.warn(
`No permissions to list resources of kind ${kind}. If you are using OpenShift, ignore this warning.`
)
return []
}
throw err
}
})
)
return flatten(resources)
}
async replace({ log, resource, namespace }: { log: Log; resource: KubernetesServerResource; namespace?: string }) {
log.silly(`Replacing Kubernetes resource ${resource.apiVersion}/${resource.kind}/${resource.metadata.name}`)
const apiPath = await this.getResourceApiPathFromManifest({ manifest: resource, log, namespace })
const res = await this.request({ log, path: apiPath, opts: { method: "put", body: resource } })
return res.body
}
/**
* Applies the specified `annotations` to the given resource and persists to the cluster.
* Assumes the resource already exists in the cluster.
*/
async annotateResource({
log,
resource,
annotations,
}: {
log: Log
resource: KubernetesServerResource
annotations: StringMap
}) {
// TODO: use patch instead of replacing (it's weirdly complex, unfortunately)
resource.metadata.annotations = { ...resource.metadata.annotations, ...annotations }
await this.replace({ log, resource })
return resource
}
async deleteBySpec({ namespace, manifest, log }: { namespace: string; manifest: KubernetesResource; log: Log }) {
log.silly(`Deleting Kubernetes resource ${manifest.apiVersion}/${manifest.kind}/${manifest.metadata.name}`)
const apiPath = await this.getResourceApiPathFromManifest({ manifest, log, namespace })
try {
await this.request({ log, path: apiPath, opts: { method: "delete" } })
} catch (err) {
if (!(err instanceof KubernetesError)) {
throw err
}
if (err.responseStatusCode !== 404) {
throw err
}
}
}
private async getResourceTypeApiPath({
apiVersion,
kind,
log,
namespace,
}: {
apiVersion: string
kind: string
log: Log
namespace: string
}) {
const resourceInfo = await this.getApiResourceInfo(log, apiVersion, kind)
if (!resourceInfo) {
const err = new KubernetesError({
message: `Unrecognized resource type ${apiVersion}/${kind}`,
})
err.responseStatusCode = 404
throw err
}
const basePath = getGroupBasePath(apiVersion)
return resourceInfo.namespaced
? `${basePath}/namespaces/${namespace}/${resourceInfo.name}`
: `${basePath}/${resourceInfo.name}`
}
private async getResourceApiPathFromManifest({
manifest,
log,
namespace,
}: {
manifest: KubernetesResource
log: Log
namespace?: string
}) {
const apiVersion = manifest.apiVersion
if (!apiVersion) {
throw new KubernetesError({
message: `Missing apiVersion on ${manifest.kind} resource named ${manifest.metadata.name}`,
})
}
if (!namespace) {
namespace = manifest.metadata?.namespace
}
if (!namespace) {
throw new KubernetesError({
message: `Missing namespace on ${manifest.kind} resource named ${manifest.metadata.name} and no namespace specified`,
})
}
const typePath = await this.getResourceTypeApiPath({
log,
apiVersion,
kind: manifest.kind,
namespace,
})
return typePath + "/" + manifest.metadata.name
}
async upsert<K extends keyof CrudMap, O extends KubernetesResource<CrudMapTypes[K]>>({
kind,
namespace,
obj,
log,
}: {
kind: K
namespace: string
obj: O
log: Log
}) {
const api = this[crudMap[kind].group]
const name = obj.metadata.name
log.debug(`Upserting ${kind} ${namespace}/${name}`)
const replace = async () => {
await api[crudMap[kind].read](name, namespace)
if (api[crudMap[kind].replace]) {
await api[crudMap[kind].replace](name, namespace, obj)
log.debug(`Replaced ${kind} ${namespace}/${name}`)
} else {
await api[crudMap[kind].patch](name, namespace, obj)
log.debug(`Patched ${kind} ${namespace}/${name}`)
}
}
try {
await replace()
} catch (replaceError) {
if (!(replaceError instanceof KubernetesError)) {
throw replaceError
}
if (replaceError.responseStatusCode === 404) {
try {
await api[crudMap[kind].create](namespace, <any>obj)
log.debug(`Created ${kind} ${namespace}/${name}`)
} catch (createError) {
if (!(createError instanceof KubernetesError)) {
throw createError
}
if (createError.responseStatusCode === 409 || createError.responseStatusCode === 422) {
await replace()
} else {
throw createError
}
}
} else {
throw replaceError
}
}
}
/**
* Wrapping the API objects to deal with bugs.
*/
private wrapApi<T extends K8sApi>(log: Log, api: T, config: KubeConfig): T {
api.setDefaultAuthentication(config)
return new Proxy(api, {
get: (target: T, name: string, receiver) => {
if (!(name in Object.getPrototypeOf(target))) {
// assume methods live on the prototype
return Reflect.get(target, name, receiver)
}
return (...args: any[]) => {
const defaultHeaders = target["defaultHeaders"]
if (name.startsWith("patch")) {
// patch the patch bug... (https://github.com/kubernetes-client/javascript/issues/19)
target["defaultHeaders"] = { ...defaultHeaders, "content-type": "application/merge-patch+json" }
}
return requestWithRetry(log, `Kubernetes API: ${name}`, () => {
const output = target[name](...args)
target["defaultHeaders"] = defaultHeaders
if (typeof output.then === "function") {
return (
output
// return the result body directly if applicable
.then((res: any) => {
if (isPlainObject(res) && res.hasOwnProperty("body")) {
// inexplicably, this API sometimes returns apiVersion and kind as undefined...
if (name === "listNamespacedPod" && res.body.items) {
res.body.items = res.body.items.map((pod: any) => {
pod.apiVersion = "v1"
pod.kind = "Pod"
return pod
})
}
return res["body"]
} else {
return res
}
})
// the API errors are not properly formed Error objects
.catch((err: Error) => {
throw toKubernetesError(err, name)
})
)
}
return output
})
}
},
})
}
/**
* Exec a command in the specified Pod container.
*
* Warning: Do not use tty=true unless you're actually attaching to a terminal, since collecting output will not work.
*/
async execInPod({
log,
buffer,
namespace,
podName,
containerName,
command,
stdout,
stderr,
stdin,
tty,
timeoutSec,
}: {
log: Log
buffer: boolean
namespace: string
podName: string
containerName: string
command: string[]
stdout?: Writable
stderr?: Writable
stdin?: Readable
tty: boolean
timeoutSec?: number
}): Promise<ExecInPodResult> {
const stdoutCollector = new StringCollector()
const stderrCollector = new StringCollector()
const combinedCollector = new StringCollector()
let _stdout = stdout || null
let _stderr = stderr || null
if (tty) {
// We connect stdout and stderr directly.
if (stdout) {
_stdout = stdout
}
if (stderr) {
_stderr = stderr
}
if (stdin) {
/**
* We use raw mode for stdin to ensure that control characters aren't intercepted by the terminal and that
* input isn't echoed back (among other things).
*
* See https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode for more.
*/
const ttyStdin = stdin as ReadStream
ttyStdin.setRawMode && ttyStdin.setRawMode(true)
}
} else if (buffer) {
/**
* Unless we're attaching a TTY to the output streams or buffer=false, we multiplex the outputs to both a
* StringCollector, and whatever stream the caller provided.
*/
_stdout = new PassThrough()
_stdout.pipe(stdoutCollector)
_stdout.pipe(combinedCollector)
_stderr = new PassThrough()
_stderr.pipe(stderrCollector)
_stderr.pipe(combinedCollector)
if (stdout) {
_stdout.pipe(stdout)
}
if (stderr) {
_stderr.pipe(stderr)
}
}
return new Promise(async (resolve, reject) => {
let done = false
const finish = (timedOut: boolean, exitCode?: number) => {
if (!done) {
resolve({
allLogs: combinedCollector.getString(),
stdout: stdoutCollector.getString(),
stderr: stderrCollector.getString(),
timedOut,
exitCode,
})
done = true
}
}
const execWithRetry = async () => {
const execHandler = new Exec(this.config)
const description = "Pod exec"
try {
return await requestWithRetry(log, description, () =>
execHandler.exec(
namespace,
podName,
containerName,
command,
_stdout,
_stderr,
stdin || null,
tty,
(status) => {
finish(false, getExecExitCode(status))
}
)
)
} catch (err) {
throw toKubernetesError(err, description)
}
}
if (timeoutSec) {
setTimeout(() => {
if (!done) {
finish(true)
}
}, timeoutSec * 1000)
}
try {
const ws = attachWebsocketKeepalive(await execWithRetry())
ws.on("error", (err) => {
done = true
reject(err)
})
} catch (err) {
reject(err)
}
})
}
getLogger() {
return new K8sLog(this.config)
}
/**
* Create an ad-hoc Pod. Use this method to handle race-condition cases when creating Pods.
*
* @throws {KubernetesError}
*/
async createPod(namespace: string, pod: KubernetesPod) {
await pRetry(
async () => {
await this.core.createNamespacedPod(namespace, pod)
},
{
retries: 3,
minTimeout: 500,
onFailedAttempt(error) {
// This can occur in laggy environments, just need to retry
if (error.message.includes("No API token found for service account")) {
return
} else if (error.message.includes("error looking up service account")) {
return
}
throw new KubernetesError({
message: `Failed to create Pod ${pod.metadata.name}: ${error.message}`,
})
},
}
)
}
}
const WEBSOCKET_KEEPALIVE_INTERVAL = 5_000
const WEBSOCKET_PING_TIMEOUT = 30_000
function attachWebsocketKeepalive(ws: WebSocket): WebSocket {
let keepAlive: NodeJS.Timeout = setInterval(() => {
ws.ping()
}, WEBSOCKET_KEEPALIVE_INTERVAL)
let pingTimeout: NodeJS.Timeout | undefined
function heartbeat() {
if (pingTimeout) {
clearTimeout(pingTimeout)
}
pingTimeout = setTimeout(() => {
ws.emit(
"error",
new KubernetesError({
message: `Lost connection to the Kubernetes WebSocket API (Timed out after ${
WEBSOCKET_PING_TIMEOUT / 1000
}s)`,
})
)
ws.terminate()
}, WEBSOCKET_PING_TIMEOUT)
}
function clear() {
if (pingTimeout) {
clearTimeout(pingTimeout)
}
clearInterval(keepAlive)
}
ws.on("pong", () => {
heartbeat()
})
ws.on("error", () => {
clear()
})
ws.on("close", () => {
clear()
})
heartbeat()
return ws
}
function getGroupBasePath(apiVersion: string) {
// Of course, Kubernetes helpfully uses a singular for the core API and not everything else. So there you go.
return apiVersion.includes("/") ? `/apis/${apiVersion}` : `/api/${apiVersion}`
}
export const KUBECTL_RETRY_OPTS: RetryOpts = {
maxRetries: 3,
minTimeoutMs: 300,
// forceRetry is important, because shouldRetry cannot handle ChildProcessError.
forceRetry: true,
}
export async function getKubeConfig(log: Log, ctx: PluginContext, provider: KubernetesProvider) {
let kubeConfigStr: string
try {
if (provider.config.kubeconfig) {
kubeConfigStr = (await readFile(provider.config.kubeconfig)).toString()
} else {
const args = ["config", "view", "--raw"]
// We use kubectl for this, to support merging multiple paths in the KUBECONFIG env var
kubeConfigStr = await requestWithRetry(
log,
`kubectl ${args.join(" ")}`,
() =>
kubectl(ctx, provider).stdout({
log,
args,
}),
KUBECTL_RETRY_OPTS
)
}
return load(kubeConfigStr)!
} catch (error) {
throw new RuntimeError({
message: `Unable to load kubeconfig: ${error}`,
})
}
}
async function getContextConfig(log: Log, ctx: PluginContext, provider: KubernetesProvider): Promise<KubeConfig> {
const kubeconfigPath = provider.config.kubeconfig
const context = provider.config.context
const cacheKey = kubeconfigPath ? `${kubeconfigPath}:${context}` : context
if (cachedConfigs[cacheKey]) {
return cachedConfigs[cacheKey]
}