Skip to content

Commit 98ddc38

Browse files
committed
another attempt to fix copyFrom
Signed-off-by: Jeromy Cannon <[email protected]>
1 parent c3934c0 commit 98ddc38

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/core/k8.mjs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -567,21 +567,22 @@ export class K8 {
567567
const destPath = path.join(destDir, srcFile)
568568

569569
// download the tar file to a temp location
570-
const tmpFile = this._tempFileFor(srcFile)
570+
const tmpFile = `${this._tempFileFor(srcFile)}.tar`
571571

572572
const self = this
573573
return new Promise((resolve, reject) => {
574574
const execInstance = new k8s.Exec(this.kubeConfig)
575575
const command = ['tar', 'cf', '-', '-C', srcDir, srcFile]
576576
const outputFileStream = fs.createWriteStream(tmpFile)
577-
const outputPassthroughStream = new stream.PassThrough({ highWaterMark: 1024 * 1024 })
577+
const outputPassthroughStream = new stream.PassThrough({ highWaterMark: 10 * 1024 * 1024 })
578578
const errStream = new stream.PassThrough()
579579
let additionalErrorMessageDetail = ''
580580

581581
// Use pipe() to automatically handle backpressure between streams
582582
outputPassthroughStream.pipe(outputFileStream)
583583

584584
outputPassthroughStream.on('data', (chunk) => {
585+
this.logger.debug(`received chunk size=${chunk.length}`)
585586
const canWrite = outputFileStream.write(chunk) // Write chunk to file and check if buffer is full
586587

587588
if (!canWrite) {
@@ -605,42 +606,44 @@ export class K8 {
605606
null,
606607
false,
607608
async ({ status }) => {
608-
this.logger.debug(`copyFrom.callback(status)=${status}`)
609-
outputFileStream.end()
610-
outputFileStream.close(() => {
611-
this.logger.debug(`finished closing writerStream copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
612-
})
613609
if (status === 'Failure') {
614610
self._deleteTempFile(tmpFile)
611+
return reject(new FullstackTestingError('Tar command failed with status Failure'))
615612
}
613+
this.logger.debug(`copyFrom.callback(status)=${status}`)
616614
})
617615
.then(conn => {
618616
conn.on('close', async (code, reason) => {
619617
if (code !== 1000) { // code 1000 is the success code
620618
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} because of error (${code}): ${reason}`))
621619
}
622620

623-
try {
624-
// extract the downloaded file
625-
await tar.x({
626-
file: tmpFile,
627-
cwd: destDir
628-
})
629-
self._deleteTempFile(tmpFile)
630-
631-
const stat = fs.statSync(destPath)
632-
if (stat && stat.size === srcFileSize) {
633-
return resolve(true)
634-
} else {
635-
if (!stat) {
636-
additionalErrorMessageDetail = ', statSync returned no file status for the destination file'
621+
outputFileStream.end()
622+
outputFileStream.close(async () => {
623+
this.logger.debug(`finished closing writerStream copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
624+
625+
try {
626+
// extract the downloaded file
627+
await tar.x({
628+
file: tmpFile,
629+
cwd: destDir
630+
})
631+
self._deleteTempFile(tmpFile)
632+
633+
const stat = fs.statSync(destPath)
634+
if (stat && stat.size === srcFileSize) {
635+
return resolve(true)
637636
} else {
638-
additionalErrorMessageDetail = `, stat.size=${stat.size} != srcFileSize=${srcFileSize}`
637+
if (!stat) {
638+
additionalErrorMessageDetail = ', statSync returned no file status for the destination file'
639+
} else {
640+
additionalErrorMessageDetail = `, stat.size=${stat.size} != srcFileSize=${srcFileSize}`
641+
}
639642
}
643+
} catch (e) {
644+
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to extract file: ${destPath}`, e))
640645
}
641-
} catch (e) {
642-
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to extract file: ${destPath}`, e))
643-
}
646+
})
644647

645648
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`))
646649
})
@@ -653,6 +656,7 @@ export class K8 {
653656
})
654657

655658
errStream.on('data', (data) => {
659+
this.logger.error(`error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, error: ${data.toString()}`)
656660
return reject(new FullstackTestingError(`error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, error: ${data.toString()}`))
657661
})
658662

@@ -673,12 +677,10 @@ export class K8 {
673677
})
674678

675679
outputFileStream.on('finish', () => {
676-
outputFileStream.end()
677680
this.logger.debug(`stopping copy, writerStream has finished for copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
678681
})
679682

680683
outputPassthroughStream.on('finish', () => {
681-
outputFileStream.end()
682684
this.logger.debug(`stopping copy, writerPassthroughStream has finished for copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
683685
})
684686
})

0 commit comments

Comments
 (0)