forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp.ts
96 lines (92 loc) · 3.13 KB
/
cp.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
import fs from 'node:fs';
import { WritableStreamBuffer } from 'stream-buffers';
import * as tar from 'tar';
import tmp from 'tmp-promise';
import { KubeConfig } from './config.js';
import { Exec } from './exec.js';
export class Cp {
public execInstance: Exec;
public constructor(config: KubeConfig, execInstance?: Exec) {
this.execInstance = execInstance || new Exec(config);
}
/**
* @param {string} namespace - The namespace of the pod to exec the command inside.
* @param {string} podName - The name of the pod to exec the command inside.
* @param {string} containerName - The name of the container in the pod to exec the command inside.
* @param {string} srcPath - The source path in the pod
* @param {string} tgtPath - The target path in local
*/
public async cpFromPod(
namespace: string,
podName: string,
containerName: string,
srcPath: string,
tgtPath: string,
): Promise<void> {
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
const command = ['tar', 'zcf', '-', srcPath];
const writerStream = fs.createWriteStream(tmpFileName);
const errStream = new WritableStreamBuffer();
this.execInstance.exec(
namespace,
podName,
containerName,
command,
writerStream,
errStream,
null,
false,
async () => {
if (errStream.size()) {
throw new Error(`Error from cpFromPod - details: \n ${errStream.getContentsAsString()}`);
}
await tar.x({
file: tmpFileName,
cwd: tgtPath,
});
},
);
}
/**
* @param {string} namespace - The namespace of the pod to exec the command inside.
* @param {string} podName - The name of the pod to exec the command inside.
* @param {string} containerName - The name of the container in the pod to exec the command inside.
* @param {string} srcPath - The source path in local
* @param {string} tgtPath - The target path in the pod
*/
public async cpToPod(
namespace: string,
podName: string,
containerName: string,
srcPath: string,
tgtPath: string,
): Promise<void> {
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
const command = ['tar', 'xf', '-', '-C', tgtPath];
await tar.c(
{
file: tmpFile.name,
},
[srcPath],
);
const readStream = fs.createReadStream(tmpFileName);
const errStream = new WritableStreamBuffer();
this.execInstance.exec(
namespace,
podName,
containerName,
command,
null,
errStream,
readStream,
false,
async () => {
if (errStream.size()) {
throw new Error(`Error from cpToPod - details: \n ${errStream.getContentsAsString()}`);
}
},
);
}
}