Skip to content

fix: add subprotocol for logs websocket #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/k8s/__tests__/k8s-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getWebsocketSubProtocolAndPathPrefix } from '../k8s-utils';

describe('getWebsocketSubProtocolAndPathPrefix', () => {
it('should return correct path with leading slash, host, and subProtocols', () => {
const path = '/example-path';
const result = getWebsocketSubProtocolAndPathPrefix(path);

expect(result).toEqual({
path: '/wss/k8s/example-path',
host: 'auto',
subProtocols: ['base64.binary.k8s.io'],
});
});

it('should set path to undefined if an empty string is provided', () => {
const path = '';
const result = getWebsocketSubProtocolAndPathPrefix(path);

expect(result).toEqual({
path: undefined,
host: 'auto',
subProtocols: ['base64.binary.k8s.io'],
});
});

it('should add a leading slash to paths without one', () => {
const path = 'no-leading-slash';
const result = getWebsocketSubProtocolAndPathPrefix(path);

expect(result).toEqual({
path: '/wss/k8s/no-leading-slash',
host: 'auto',
subProtocols: ['base64.binary.k8s.io'],
});
});

it('should not add an extra leading slash if the path already has one', () => {
const path = '/already-has-slash';
const result = getWebsocketSubProtocolAndPathPrefix(path);

expect(result).toEqual({
path: '/wss/k8s/already-has-slash',
host: 'auto',
subProtocols: ['base64.binary.k8s.io'],
});
});
});
20 changes: 16 additions & 4 deletions src/k8s/k8s-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ export const getK8sResourceURL = (
return resourcePath;
};

/**
* returns websocket subprotocol, host with added prefix to path
* @param path {String}
*/
export const getWebsocketSubProtocolAndPathPrefix = (path: string) => {
return {
path: path === '' ? undefined : `/wss/k8s${path.startsWith('/') ? path : `/${path}`}`,
host: 'auto',
subProtocols: ['base64.binary.k8s.io'],
};
};

export const k8sWatch = (
kind: K8sModelCommon,
query: {
Expand Down Expand Up @@ -239,11 +251,11 @@ export const k8sWatch = (
}

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

return new WebSocketFactory(path, wsOptionsUpdated);
return new WebSocketFactory(path, {
...wsOptionsUpdated,
...getWebsocketSubProtocolAndPathPrefix(path),
});
};

/**
Expand Down
6 changes: 2 additions & 4 deletions src/shared/components/pipeline-run-logs/logs/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Base64 } from 'js-base64';
import { useWorkspaceInfo } from '../../../../components/Workspace/useWorkspaceInfo';
import { commonFetchText } from '../../../../k8s';
import { getK8sResourceURL } from '../../../../k8s/k8s-utils';
import { getK8sResourceURL, getWebsocketSubProtocolAndPathPrefix } from '../../../../k8s/k8s-utils';

Check warning on line 7 in src/shared/components/pipeline-run-logs/logs/Logs.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L7 was not covered by tests
import { WebSocketFactory } from '../../../../k8s/web-socket/WebSocketFactory';
import { PodModel } from '../../../../models/pod';
import { ContainerSpec, PodKind } from '../../types';
Expand Down Expand Up @@ -107,9 +107,7 @@
onCompleteRef.current(name);
});
} else {
const wsOpts = {
path: watchURL,
};
const wsOpts = getWebsocketSubProtocolAndPathPrefix(watchURL);

Check warning on line 110 in src/shared/components/pipeline-run-logs/logs/Logs.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L110 was not covered by tests
ws = new WebSocketFactory(watchURL, wsOpts);
ws.onMessage((msg) => {
if (loaded) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const LogsWrapperComponent: React.FC<React.PropsWithChildren<LogsWrapperComponen
data: obj,
isLoading,
error,
} = useK8sWatchResource<PodKind>(resource, PodModel, { retry: false });
} = useK8sWatchResource<PodKind>({ ...resource, watch: true }, PodModel, { retry: false });
const [isFullscreen, fullscreenRef, fullscreenToggle] = useFullscreen<HTMLDivElement>();
const [downloadAllStatus, setDownloadAllStatus] = React.useState(false);
const currentLogGetterRef = React.useRef<() => string>();
Expand Down
Loading