Skip to content

Commit e47e5f9

Browse files
authored
fix: add subprotocol for logs websocket (#80)
* fix: add subprotocol for logs websocket * fix: add unit tests for websockets utils
1 parent b5460e5 commit e47e5f9

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

src/k8s/__tests__/k8s-utils.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { getWebsocketSubProtocolAndPathPrefix } from '../k8s-utils';
2+
3+
describe('getWebsocketSubProtocolAndPathPrefix', () => {
4+
it('should return correct path with leading slash, host, and subProtocols', () => {
5+
const path = '/example-path';
6+
const result = getWebsocketSubProtocolAndPathPrefix(path);
7+
8+
expect(result).toEqual({
9+
path: '/wss/k8s/example-path',
10+
host: 'auto',
11+
subProtocols: ['base64.binary.k8s.io'],
12+
});
13+
});
14+
15+
it('should set path to undefined if an empty string is provided', () => {
16+
const path = '';
17+
const result = getWebsocketSubProtocolAndPathPrefix(path);
18+
19+
expect(result).toEqual({
20+
path: undefined,
21+
host: 'auto',
22+
subProtocols: ['base64.binary.k8s.io'],
23+
});
24+
});
25+
26+
it('should add a leading slash to paths without one', () => {
27+
const path = 'no-leading-slash';
28+
const result = getWebsocketSubProtocolAndPathPrefix(path);
29+
30+
expect(result).toEqual({
31+
path: '/wss/k8s/no-leading-slash',
32+
host: 'auto',
33+
subProtocols: ['base64.binary.k8s.io'],
34+
});
35+
});
36+
37+
it('should not add an extra leading slash if the path already has one', () => {
38+
const path = '/already-has-slash';
39+
const result = getWebsocketSubProtocolAndPathPrefix(path);
40+
41+
expect(result).toEqual({
42+
path: '/wss/k8s/already-has-slash',
43+
host: 'auto',
44+
subProtocols: ['base64.binary.k8s.io'],
45+
});
46+
});
47+
});

src/k8s/k8s-utils.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ export const getK8sResourceURL = (
188188
return resourcePath;
189189
};
190190

191+
/**
192+
* returns websocket subprotocol, host with added prefix to path
193+
* @param path {String}
194+
*/
195+
export const getWebsocketSubProtocolAndPathPrefix = (path: string) => {
196+
return {
197+
path: path === '' ? undefined : `/wss/k8s${path.startsWith('/') ? path : `/${path}`}`,
198+
host: 'auto',
199+
subProtocols: ['base64.binary.k8s.io'],
200+
};
201+
};
202+
191203
export const k8sWatch = (
192204
kind: K8sModelCommon,
193205
query: {
@@ -239,11 +251,11 @@ export const k8sWatch = (
239251
}
240252

241253
const path = getK8sResourceURL(kind, undefined, opts);
242-
wsOptionsUpdated.path = `/wss/k8s${path}`;
243-
wsOptionsUpdated.host = 'auto';
244-
wsOptionsUpdated.subProtocols = ['base64.binary.k8s.io'];
245254

246-
return new WebSocketFactory(path, wsOptionsUpdated);
255+
return new WebSocketFactory(path, {
256+
...wsOptionsUpdated,
257+
...getWebsocketSubProtocolAndPathPrefix(path),
258+
});
247259
};
248260

249261
/**

src/shared/components/pipeline-run-logs/logs/Logs.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Alert } from '@patternfly/react-core';
44
import { Base64 } from 'js-base64';
55
import { useWorkspaceInfo } from '../../../../components/Workspace/useWorkspaceInfo';
66
import { commonFetchText } from '../../../../k8s';
7-
import { getK8sResourceURL } from '../../../../k8s/k8s-utils';
7+
import { getK8sResourceURL, getWebsocketSubProtocolAndPathPrefix } from '../../../../k8s/k8s-utils';
88
import { WebSocketFactory } from '../../../../k8s/web-socket/WebSocketFactory';
99
import { PodModel } from '../../../../models/pod';
1010
import { ContainerSpec, PodKind } from '../../types';
@@ -107,9 +107,7 @@ const Logs: React.FC<React.PropsWithChildren<LogsProps>> = ({
107107
onCompleteRef.current(name);
108108
});
109109
} else {
110-
const wsOpts = {
111-
path: watchURL,
112-
};
110+
const wsOpts = getWebsocketSubProtocolAndPathPrefix(watchURL);
113111
ws = new WebSocketFactory(watchURL, wsOpts);
114112
ws.onMessage((msg) => {
115113
if (loaded) return;

src/shared/components/pipeline-run-logs/logs/LogsWrapperComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const LogsWrapperComponent: React.FC<React.PropsWithChildren<LogsWrapperComponen
3232
data: obj,
3333
isLoading,
3434
error,
35-
} = useK8sWatchResource<PodKind>(resource, PodModel, { retry: false });
35+
} = useK8sWatchResource<PodKind>({ ...resource, watch: true }, PodModel, { retry: false });
3636
const [isFullscreen, fullscreenRef, fullscreenToggle] = useFullscreen<HTMLDivElement>();
3737
const [downloadAllStatus, setDownloadAllStatus] = React.useState(false);
3838
const currentLogGetterRef = React.useRef<() => string>();

0 commit comments

Comments
 (0)