Skip to content

Commit f1efb5c

Browse files
committed
Fix up web sockets
1 parent 73e15d1 commit f1efb5c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

node-client/src/attach.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import stream = require('stream');
33

44
import { KubeConfig } from './config';
55
import { WebSocketHandler } from './web-socket-handler';
6+
import { connection } from 'websocket';
7+
import { resolve } from 'url';
68

79
export class Attach {
810
public 'handler': WebSocketHandler;
@@ -13,7 +15,7 @@ export class Attach {
1315

1416
public attach(namespace: string, podName: string, containerName: string,
1517
stdout: stream.Writable | any, stderr: stream.Writable | any, stdin: stream.Readable | any,
16-
tty: boolean) {
18+
tty: boolean): Promise<void> {
1719
const query = {
1820
container: containerName,
1921
stderr: stderr != null,
@@ -23,8 +25,12 @@ export class Attach {
2325
};
2426
const queryStr = querystring.stringify(query);
2527
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/attach?${queryStr}`;
26-
this.handler.connect(path, null, (streamNum: number, buff: Buffer) => {
28+
const promise = this.handler.connect(path, null, (streamNum: number, buff: Buffer) => {
2729
WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
2830
});
31+
const result = new Promise<void>((resolvePromise, reject) => {
32+
promise.then(() => resolvePromise(), (err) => reject(err));
33+
});
34+
return result;
2935
}
3036
}

node-client/src/web-socket-handler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ export class WebSocketHandler {
6969
binaryHandler: (stream: number, buff: Buffer) => void): Promise<ws.connection> {
7070

7171
const server = this.config.getCurrentCluster().server;
72-
const target = server.startsWith('https://') ? server.substr(8) : server.substr(7);
73-
const uri = `wss://${target}${path}`;
72+
const ssl = server.startsWith('https://');
73+
const target = ssl ? server.substr(8) : server.substr(7);
74+
const proto = ssl ? 'wss' : 'ws';
75+
const uri = `${proto}://${target}${path}`;
7476

7577
const opts: https.RequestOptions = {};
7678
this.config.applytoHTTPSOptions(opts);
@@ -97,8 +99,7 @@ export class WebSocketHandler {
9799
client.on('connectFailed', (err) => {
98100
reject(err);
99101
});
100-
101-
client.connect(uri, protocols);
102+
client.connect(uri, protocols, null, { Host: target, Upgrade: 'websocket' });
102103
});
103104
}
104105
}

0 commit comments

Comments
 (0)