Skip to content

Commit 44e070a

Browse files
committed
Address comments.
1 parent bb87d9a commit 44e070a

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

examples/typescript/exec/exec-example.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const kc = new k8s.KubeConfig();
66
kc.loadFromDefault();
77

88
const exec = new k8s.Exec(kc);
9-
exec.exec('default', 'demo', 'demo', command,
9+
exec.exec('default', 'nginx-4217019353-9gl4s', 'nginx', command,
1010
process.stdout, process.stderr, process.stdin,
1111
(status: k8s.V1Status) => {
12+
// tslint:disable-next-line:no-console
1213
console.log('Exited with status:');
14+
// tslint:disable-next-line:no-console
1315
console.log(JSON.stringify(status, null, 2));
1416
},
1517
true /* tty */);

src/exec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@ export class Exec {
1818
}
1919

2020
// TODO: make command an array and support multiple args
21+
/**
22+
* @param {string} namespace - The namespace of the pod to exec the command inside.
23+
* @param {string} podName - The name of the pod to exec the command inside.
24+
* @param {string} containerName - The name of the container in the pod to exec the command inside.
25+
* @param {string} command - The command to execute.
26+
* @param {stream.Writable} stdout - The stream to write stdout data from the command.
27+
* @param {stream.Writable} stderr - The stream to write stderr data from the command.
28+
* @param {stream.Readable} stdin - The strream to write stdin data into the command.
29+
* @param {boolean} tty - Should the command execute in a TTY enabled session.
30+
* @param {(V1Status) => void} statusCallback -
31+
* A callback to received the status (e.g. exit code) from the command, optional.
32+
* @return {string} This is the result
33+
*/
2134
public async exec(namespace: string, podName: string, containerName: string, command: string,
2235
stdout: stream.Writable | null, stderr: stream.Writable | null, stdin: stream.Readable | null,
23-
statusCallback: (status: V1Status) => void,
24-
tty: boolean): Promise<WebSocket> {
36+
tty: boolean,
37+
statusCallback?: (status: V1Status) => void): Promise<WebSocket> {
2538
const query = {
2639
stdout: stdout != null,
2740
stderr: stderr != null,

src/exec_test.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import WebSocket = require('isomorphic-ws');
33
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
44
import { anyFunction, capture, instance, mock, verify, when } from 'ts-mockito';
55

6+
import { V1Status } from './api';
67
import { KubeConfig } from './config';
78
import { Exec } from './exec';
89
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
910

1011
describe('Exec', () => {
11-
// tslint:disable-next-line:no-empty
12-
const nop = () => {};
13-
1412
describe('basic', () => {
1513
it('should correctly exec to a url', async () => {
1614
const kc = new KubeConfig();
@@ -27,27 +25,27 @@ describe('Exec', () => {
2725
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
2826

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

3432
await exec.exec(
35-
namespace, pod, container, cmd, null, errStream, isStream, nop, false);
33+
namespace, pod, container, cmd, null, errStream, isStream, false);
3634
args = `stdout=false&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
3735
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
3836

3937
await exec.exec(
40-
namespace, pod, container, cmd, null, null, isStream, nop, false);
38+
namespace, pod, container, cmd, null, null, isStream, false);
4139
args = `stdout=false&stderr=false&stdin=true&tty=false&command=${cmd}&container=${container}`;
4240
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
4341

4442
await exec.exec(
45-
namespace, pod, container, cmd, null, null, null, nop, false);
43+
namespace, pod, container, cmd, null, null, null, false);
4644
args = `stdout=false&stderr=false&stdin=false&tty=false&command=${cmd}&container=${container}`;
4745
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
4846

4947
await exec.exec(
50-
namespace, pod, container, cmd, null, errStream, isStream, nop, true);
48+
namespace, pod, container, cmd, null, errStream, isStream, true);
5149
args = `stdout=false&stderr=true&stdin=true&tty=true&command=${cmd}&container=${container}`;
5250
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
5351
});
@@ -68,11 +66,15 @@ describe('Exec', () => {
6866
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
6967
const args = `stdout=true&stderr=true&stdin=true&tty=false&command=${cmd}&container=${container}`;
7068

69+
let statusOut = {} as V1Status;
70+
7171
const fakeConn: WebSocket = mock(WebSocket);
7272
when(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).thenResolve(fakeConn);
7373

7474
await exec.exec(
75-
namespace, pod, container, cmd, osStream, errStream, isStream, nop, false);
75+
namespace, pod, container, cmd, osStream, errStream, isStream, false, (status: V1Status) => {
76+
statusOut = status;
77+
});
7678

7779
const [, , outputFn] = capture(fakeWebSocket.connect).last();
7880

@@ -105,6 +107,14 @@ describe('Exec', () => {
105107
isStream.put(msg);
106108
verify(fakeConn.send(msg));
107109

110+
const statusIn = {
111+
code: 100,
112+
message: 'this is a test',
113+
} as V1Status;
114+
115+
outputFn(WebSocketHandler.StatusStream, Buffer.from(JSON.stringify(statusIn)));
116+
expect(statusOut).to.deep.equal(statusIn);
117+
108118
isStream.stop();
109119
verify(fakeConn.close());
110120
});

0 commit comments

Comments
 (0)