Skip to content

Commit bb87d9a

Browse files
committed
Fix sending protocols for web sockets.
1 parent 7639c00 commit bb87d9a

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

examples/typescript/exec/exec-example.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ const kc = new k8s.KubeConfig();
66
kc.loadFromDefault();
77

88
const exec = new k8s.Exec(kc);
9-
exec.exec('default', 'nginx-4217019353-9gl4s', 'nginx', command,
10-
process.stdout, process.stderr, process.stdin, true /* tty */);
9+
exec.exec('default', 'demo', 'demo', command,
10+
process.stdout, process.stderr, process.stdin,
11+
(status: k8s.V1Status) => {
12+
console.log('Exited with status:');
13+
console.log(JSON.stringify(status, null, 2));
14+
},
15+
true /* tty */);

src/exec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import WebSocket = require('isomorphic-ws');
22
import querystring = require('querystring');
33
import stream = require('stream');
44

5+
import { V1Status } from './api';
56
import { KubeConfig } from './config';
67
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
78

@@ -18,7 +19,8 @@ export class Exec {
1819

1920
// TODO: make command an array and support multiple args
2021
public async exec(namespace: string, podName: string, containerName: string, command: string,
21-
stdout: stream.Writable | any, stderr: stream.Writable | any, stdin: stream.Readable | any,
22+
stdout: stream.Writable | null, stderr: stream.Writable | null, stdin: stream.Readable | null,
23+
statusCallback: (status: V1Status) => void,
2224
tty: boolean): Promise<WebSocket> {
2325
const query = {
2426
stdout: stdout != null,
@@ -31,7 +33,13 @@ export class Exec {
3133
const queryStr = querystring.stringify(query);
3234
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/exec?${queryStr}`;
3335
const conn = await this.handler.connect(path, null, (streamNum: number, buff: Buffer): boolean => {
34-
WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
36+
const status = WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
37+
if (status != null) {
38+
if (statusCallback) {
39+
statusCallback(status);
40+
}
41+
return false;
42+
}
3543
return true;
3644
});
3745
if (stdin != null) {

src/exec_test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { Exec } from './exec';
88
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
99

1010
describe('Exec', () => {
11+
// tslint:disable-next-line:no-empty
12+
const nop = () => {};
13+
1114
describe('basic', () => {
1215
it('should correctly exec to a url', async () => {
1316
const kc = new KubeConfig();
@@ -24,27 +27,27 @@ describe('Exec', () => {
2427
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
2528

2629
await exec.exec(
27-
namespace, pod, container, cmd, osStream, errStream, isStream, false);
30+
namespace, pod, container, cmd, osStream, errStream, isStream, nop, false);
2831
let args = `stdout=true&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
2932
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
3033

3134
await exec.exec(
32-
namespace, pod, container, cmd, null, errStream, isStream, false);
35+
namespace, pod, container, cmd, null, errStream, isStream, nop, false);
3336
args = `stdout=false&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
3437
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
3538

3639
await exec.exec(
37-
namespace, pod, container, cmd, null, null, isStream, false);
40+
namespace, pod, container, cmd, null, null, isStream, nop, false);
3841
args = `stdout=false&stderr=false&stdin=true&tty=false&command=${cmd}&container=${container}`;
3942
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
4043

4144
await exec.exec(
42-
namespace, pod, container, cmd, null, null, null, false);
45+
namespace, pod, container, cmd, null, null, null, nop, false);
4346
args = `stdout=false&stderr=false&stdin=false&tty=false&command=${cmd}&container=${container}`;
4447
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
4548

4649
await exec.exec(
47-
namespace, pod, container, cmd, null, errStream, isStream, true);
50+
namespace, pod, container, cmd, null, errStream, isStream, nop, true);
4851
args = `stdout=false&stderr=true&stdin=true&tty=true&command=${cmd}&container=${container}`;
4952
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
5053
});
@@ -69,7 +72,7 @@ describe('Exec', () => {
6972
when(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).thenResolve(fakeConn);
7073

7174
await exec.exec(
72-
namespace, pod, container, cmd, osStream, errStream, isStream, false);
75+
namespace, pod, container, cmd, osStream, errStream, isStream, nop, false);
7376

7477
const [, , outputFn] = capture(fakeWebSocket.connect).last();
7578

src/web-socket-handler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ export class WebSocketHandler implements WebSocketInterface {
2727

2828
public static handleStandardStreams(
2929
streamNum: number, buff: Buffer,
30-
stdout: stream.Writable, stderr: stream.Writable,
30+
stdout: stream.Writable | null, stderr: stream.Writable | null,
3131
): V1Status | null {
3232
if (buff.length < 1) {
3333
return null;
3434
}
35-
if (streamNum === WebSocketHandler.StdoutStream) {
35+
if (stdout && streamNum === WebSocketHandler.StdoutStream) {
3636
stdout.write(buff);
37-
} else if (streamNum === WebSocketHandler.StderrStream) {
37+
} else if (stderr && streamNum === WebSocketHandler.StderrStream) {
3838
stderr.write(buff);
3939
} else if (streamNum === WebSocketHandler.StatusStream) {
4040
// stream closing.
41-
if (stdout) {
41+
if (stdout && stdout !== process.stdout) {
4242
stdout.end();
4343
}
44-
if (stderr) {
44+
if (stderr && stderr !== process.stderr) {
4545
stderr.end();
4646
}
4747
return JSON.parse(buff.toString('utf8')) as V1Status;
@@ -109,7 +109,7 @@ export class WebSocketHandler implements WebSocketInterface {
109109
this.config.applytoHTTPSOptions(opts);
110110

111111
return new Promise((resolve, reject) => {
112-
const client = (this.socketFactory ? this.socketFactory(uri, opts) : new WebSocket(uri, opts));
112+
const client = (this.socketFactory ? this.socketFactory(uri, opts) : new WebSocket(uri, protocols, opts));
113113
let resolved = false;
114114

115115
client.onopen = () => {

0 commit comments

Comments
 (0)