Skip to content

Instantly share code, notes, and snippets.

@Dvorson
Created July 10, 2020 14:46
Show Gist options
  • Save Dvorson/1ddc0a6c2e8ba646861458b2185de121 to your computer and use it in GitHub Desktop.
Save Dvorson/1ddc0a6c2e8ba646861458b2185de121 to your computer and use it in GitHub Desktop.
async function portForward(podName, localPort, remotePort) {
return new Promise((resolve, reject) => {
const child = spawn('oc', ['port-forward', podName, `${localPort}:${remotePort}`]); // , { stdio: 'inherit' });
['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach(signal => {
process.on(signal, () => {
child.stdin.pause();
child.kill();
});
});
child.stderr.on('data', err => reject(err));
child.on('close', code => {
console.log(`Port forwarding ${podName} exited with code ${code}`);
child.stdin.pause();
process.exit();
});
child.stdout.on('data', data => {
console.log(`${podName}: ${data}`);
if (data.includes('Forwarding from')) {
console.log(`Started port forwarding for ${podName}`);
child.stdout.off('data');
resolve();
}
});
}).catch(err => console.error(`${err}`));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment