Skip to content

Commit

Permalink
Merge pull request #1169 from DavidIsa/rm-tmp-promise
Browse files Browse the repository at this point in the history
Remove tmp-promise
  • Loading branch information
k8s-ci-robot committed Jul 28, 2023
2 parents 9e8d36f + f9d127f commit 38cd517
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
82 changes: 35 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"rfc4648": "^1.3.0",
"stream-buffers": "^3.0.2",
"tar": "^6.1.11",
"tmp-promise": "^3.0.2",
"tslib": "^2.4.1",
"ws": "^8.11.0"
},
Expand Down
21 changes: 6 additions & 15 deletions src/cp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as fs from 'fs';
import { WritableStreamBuffer } from 'stream-buffers';
import * as tar from 'tar';
import * as tmp from 'tmp-promise';

import { KubeConfig } from './config';
import { Exec } from './exec';
import { generateTmpFileName } from './util';

export class Cp {
public execInstance: Exec;
Expand All @@ -18,7 +17,7 @@ export class Cp {
* @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
* @param {string} cwd - The directory that is used as the parent in the pod when downloading
* @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
*/
public async cpFromPod(
namespace: string,
Expand All @@ -28,8 +27,7 @@ export class Cp {
tgtPath: string,
cwd?: string,
): Promise<void> {
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
const tmpFileName = await generateTmpFileName();
const command = ['tar', 'zcf', '-'];
if (cwd) {
command.push('-C', cwd);
Expand Down Expand Up @@ -65,7 +63,7 @@ export class Cp {
* @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
* @param {string} cwd - The directory that is used as the parent in the host when uploading
* @param {string} [cwd] - The directory that is used as the parent in the host when uploading
*/
public async cpToPod(
namespace: string,
Expand All @@ -75,16 +73,9 @@ export class Cp {
tgtPath: string,
cwd?: string,
): Promise<void> {
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
const tmpFileName = await generateTmpFileName();
const command = ['tar', 'xf', '-', '-C', tgtPath];
await tar.c(
{
file: tmpFile.name,
cwd,
},
[srcPath],
);
await tar.c({ file: tmpFileName, cwd }, [srcPath]);
const readStream = fs.createReadStream(tmpFileName);
const errStream = new WritableStreamBuffer();
this.execInstance.exec(
Expand Down
24 changes: 24 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { tmpdir } from 'os';
import { randomUUID } from 'crypto';
import { promises as fs, constants as fsConstants } from 'fs';
import { CoreV1Api, V1Container, V1Pod } from './gen/api';

export async function podsForNode(api: CoreV1Api, nodeName: string): Promise<V1Pod[]> {
Expand Down Expand Up @@ -142,3 +145,24 @@ export function totalForResource(pod: V1Pod, resource: string): ResourceStatus {
});
return new ResourceStatus(reqTotal, limitTotal, resource);
}

export async function generateTmpFileName(): Promise<string> {
let tmpFileName: string;

let i = 0;
do {
tmpFileName = `${tmpdir()}/${randomUUID()}`;

try {
await fs.access(tmpFileName, fsConstants.W_OK);

console.warn('Tmp file already exists');
} catch (err) {
return tmpFileName;
}

i++;
} while (i < 10);

throw new Error('Cannot generate tmp file name');
}

0 comments on commit 38cd517

Please sign in to comment.