Skip to content

Commit dee2426

Browse files
authored
Merge pull request #2664 from sachiniyer/siyer/fix-cp-from-pod
fix: cp from pod was broken
2 parents ac81b7d + d701976 commit dee2426

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/cp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Cp {
2626
tgtPath: string,
2727
cwd?: string,
2828
): Promise<void> {
29-
const command = ['tar', 'zcf', '-'];
29+
const command = ['tar', 'cf', '-'];
3030
if (cwd) {
3131
command.push('-C', cwd);
3232
}

src/cp_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Cp', () => {
2222
const container = 'container';
2323
const srcPath = '/';
2424
const tgtPath = '/';
25-
const cmdArray = ['tar', 'zcf', '-', srcPath];
25+
const cmdArray = ['tar', 'cf', '-', srcPath];
2626
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
2727

2828
const query = {
@@ -51,7 +51,7 @@ describe('Cp', () => {
5151
const srcPath = '/';
5252
const tgtPath = '/';
5353
const cwd = '/abc';
54-
const cmdArray = ['tar', 'zcf', '-', '-C', cwd, srcPath];
54+
const cmdArray = ['tar', 'cf', '-', '-C', cwd, srcPath];
5555
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
5656

5757
const query = {

src/test/integration/cpFromPod.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import assert from 'node:assert';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import os from 'node:os';
5+
import { setTimeout } from 'node:timers/promises';
6+
import { CoreV1Api, KubeConfig, V1Pod } from '../../index.js';
7+
import { Cp } from '../../cp.js';
8+
import { generateName } from './name.js';
9+
10+
export default async function cpFromPod() {
11+
const kc = new KubeConfig();
12+
kc.loadFromDefault();
13+
14+
const coreV1Client = kc.makeApiClient(CoreV1Api);
15+
const cp = new Cp(kc);
16+
17+
const testPodName = generateName('cp-test-pod');
18+
const namespace = 'default';
19+
20+
const pod = new V1Pod();
21+
pod.metadata = { name: testPodName };
22+
pod.spec = {
23+
containers: [
24+
{
25+
name: 'test-container',
26+
image: 'busybox',
27+
command: ['sh', '-c', 'echo "test content" > /tmp/test.txt && sleep 3600'],
28+
},
29+
],
30+
restartPolicy: 'Never',
31+
};
32+
33+
console.log(`Creating pod ${testPodName}`);
34+
await coreV1Client.createNamespacedPod({ namespace, body: pod });
35+
36+
console.log('Waiting for pod to be ready...');
37+
let podReady = false;
38+
for (let i = 0; i < 30; i++) {
39+
const currentPod = await coreV1Client.readNamespacedPod({ name: testPodName, namespace });
40+
if (currentPod.status?.phase === 'Running') {
41+
podReady = true;
42+
break;
43+
}
44+
await setTimeout(1000);
45+
}
46+
47+
assert.strictEqual(podReady, true, 'Pod did not become ready in time');
48+
console.log('Pod is ready');
49+
50+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'k8s-cp-test-'));
51+
52+
try {
53+
console.log('Copying file from pod...');
54+
55+
cp.cpFromPod(namespace, testPodName, 'test-container', 'test.txt', tempDir, '/tmp');
56+
57+
// Wait for file to appear
58+
const copiedFilePath = path.join(tempDir, 'test.txt');
59+
let fileExists = false;
60+
for (let i = 0; i < 20; i++) {
61+
if (fs.existsSync(copiedFilePath)) {
62+
fileExists = true;
63+
break;
64+
}
65+
await setTimeout(500);
66+
}
67+
68+
assert.strictEqual(fileExists, true, 'File was not copied');
69+
70+
const content = fs.readFileSync(copiedFilePath, 'utf-8');
71+
assert.strictEqual(content.trim(), 'test content', 'File content does not match');
72+
73+
console.log('cpFromPod test passed!');
74+
} finally {
75+
console.log('Cleaning up...');
76+
await coreV1Client.deleteNamespacedPod({ name: testPodName, namespace });
77+
fs.rmSync(tempDir, { recursive: true, force: true });
78+
}
79+
}

src/test/integration/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import patchNamespace from './patchNamespace.js';
2+
import cpFromPod from './cpFromPod.js';
23

34
console.log('Integration testing');
45

56
await patchNamespace();
7+
await cpFromPod();

0 commit comments

Comments
 (0)