Skip to content
Open
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
5 changes: 3 additions & 2 deletions frontend/src/components/crd/CustomResourceDetails.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export default {
'http://localhost:4466/apis/my.phonyresources.io/v1/namespaces/mynamespace/mycustomresources/mycustomresource',
() => HttpResponse.json(mockCRList[0])
),
http.get('http://localhost:4466/apis/my.phonyresources.io/v1/mycustomresources', () =>
HttpResponse.json({})
http.get(
'http://localhost:4466/apis/my.phonyresources.io/v1/namespaces/mynamespace/mycustomresources',
() => HttpResponse.json({})
),
http.get(
'http://localhost:4466/apis/my.phonyresources.io/v1/mycustomresources/nonexistentcustomresource',
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/cronjob/CronJobDetails.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/apis/batch/v1/cronjobs', () => HttpResponse.json({})),
http.get('http://localhost:4466/apis/batch/v1/namespaces/default/cronjobs', () =>
HttpResponse.error()
),
http.get('http://localhost:4466/api/v1/namespaces/default/events', () =>
HttpResponse.json({
kind: 'EventList',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default {
msw: {
handlers: {
storyBase: [
http.get(
'http://localhost:4466/apis/discovery.k8s.io/v1/namespaces/my-namespace/endpointslices',
() => HttpResponse.error()
),
http.get('http://localhost:4466/apis/discovery.k8s.io/v1/endpointslices', () =>
HttpResponse.error()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/api/v1/endpoints', () => HttpResponse.error()),
http.get('http://localhost:4466/api/v1/namespaces/my-namespace/endpoints', () =>
HttpResponse.error()
),
http.get('http://localhost:4466/api/v1/namespaces/my-namespace/events', () =>
HttpResponse.error()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default {
handlers: {
story: [],
storyBase: [
http.get(
'http://localhost:4466/apis/gateway.networking.k8s.io/v1alpha3/namespaces/default/backendtlspolicies',
() => HttpResponse.error()
),
http.get(
'http://localhost:4466/apis/gateway.networking.k8s.io/v1alpha3/namespaces/default/backendtlspolicies/example-policy',
() => HttpResponse.json(DEFAULT_BACKEND_TLS_POLICY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default {
handlers: {
story: [],
storyBase: [
http.get(
'http://localhost:4466/apis/gateway.networking.x-k8s.io/v1alpha1/namespaces/default/xbackendtrafficpolicies',
() => HttpResponse.error()
),
http.get(
'http://localhost:4466/apis/gateway.networking.x-k8s.io/v1alpha1/namespaces/default/xbackendtrafficpolicies/example-traffic-policy',
() => HttpResponse.json(DEFAULT_BACKEND_TRAFFIC_POLICY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default {
story: [],
storyBase: [
http.get(
'http://localhost:4466/apis/gateway.networking.k8s.io/v1beta1/referencegrants',
'http://localhost:4466/apis/gateway.networking.k8s.io/v1beta1/namespaces/default/referencegrants',
() => HttpResponse.json({})
),
http.get('http://localhost:4466/api/v1/namespaces/default/events', () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/apis/autoscaling/v2/horizontalpodautoscalers', () =>
HttpResponse.error()
http.get(
'http://localhost:4466/apis/autoscaling/v2/namespaces/my-namespace/horizontalpodautoscalers',
() => HttpResponse.error()
),
http.get('http://localhost:4466/api/v1/namespaces/default/events', () =>
HttpResponse.json({
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/components/networkpolicy/Details.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ Default.parameters = {
'http://localhost:4466/apis/networking.k8s.io/v1/namespaces/default/networkpolicies/allow-frontend-traffic',
() => HttpResponse.json(NETWORK_POLICY_DETAIL)
),
http.get('http://localhost:4466/apis/networking.k8s.io/v1/networkpolicies', () =>
HttpResponse.json({
kind: 'NetworkPolicyList',
items: NETWORK_POLICY_ITEMS,
metadata: {},
})
http.get(
'http://localhost:4466/apis/networking.k8s.io/v1/namespaces/default/networkpolicies',
() =>
HttpResponse.json({
kind: 'NetworkPolicyList',
items: NETWORK_POLICY_ITEMS,
metadata: {},
})
),
http.get('http://localhost:4466/api/v1/namespaces/default/events', () =>
HttpResponse.json({
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/pod/PodDetails.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Error.parameters = {
handlers: {
storyBase: null,
story: [
http.get('http://localhost:4466/api/v1/namespaces/default/pods', () =>
HttpResponse.json({})
),
http.get('http://localhost:4466/api/v1/namespaces/default/pods/terminated', () =>
HttpResponse.json(podList.find(pod => pod.metadata.name === 'terminated'))
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/apis/policy/v1/poddisruptionbudgets', () =>
HttpResponse.error()
http.get(
'http://localhost:4466/apis/policy/v1/namespaces/my-namespace/poddisruptionbudgets',
() => HttpResponse.error()
),
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/api/v1/resourcequotas', () => HttpResponse.error()),
http.get('http://localhost:4466/api/v1/namespaces/my-namespace/resourcequotas', () =>
HttpResponse.error()
),
http.get('http://localhost:4466/api/v1/namespaces/test/events', () =>
HttpResponse.error()
),
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/components/service/ServiceDetails.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ Default.parameters = {
http.get('http://localhost:4466/api/v1/namespaces/default/events', () =>
HttpResponse.json({ kind: 'EventList', items: [], metadata: {} })
),
http.get('http://localhost:4466/api/v1/namespaces/default/services', () =>
HttpResponse.error()
),
http.get('http://localhost:4466/api/v1/namespaces/default/services/example-service', () =>
HttpResponse.json(serviceMock)
),
Expand Down Expand Up @@ -182,6 +185,9 @@ ErrorWithEndpoints.parameters = {
http.get('http://localhost:4466/api/v1/namespaces/default/events', () =>
HttpResponse.json({ kind: 'EventList', items: [], metadata: {} })
),
http.get('http://localhost:4466/api/v1/namespaces/default/services', () =>
HttpResponse.error()
),
http.get('http://localhost:4466/api/v1/namespaces/default/services/example-service', () =>
HttpResponse.json(serviceMock)
),
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/components/statefulset/Details.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/apis/apps/v1/namespaces/default/statefulsets', () => {
return HttpResponse.json({
kind: 'StatefulSetList',
items: [],
});
}),
http.get('http://localhost:4466/api/v1/namespaces/default/pods', () => {
return HttpResponse.json({
kind: 'PodList',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ export default {
msw: {
handlers: {
storyBase: [
http.get('http://localhost:4466/apis/autoscaling.k8s.io/v1/verticalpodautoscalers', () =>
HttpResponse.error()
http.get(
'http://localhost:4466/apis/autoscaling.k8s.io/v1/namespaces/default/verticalpodautoscalers',
() => HttpResponse.error()
),
],
},
Expand Down
64 changes: 48 additions & 16 deletions frontend/src/lib/k8s/api/v2/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { clusterFetch } from './fetch';
import type { KubeListUpdateEvent } from './KubeList';
import { KubeObjectEndpoint } from './KubeObjectEndpoint';
import { makeUrl } from './makeUrl';
import { useWebSocket } from './webSocket';
import { useWebSocket } from './multiplexer';
import { getWebsocketMultiplexerEnabled } from './useKubeObjectList';
import { useWebSockets } from './webSocket';

export type QueryStatus = 'pending' | 'success' | 'error';

Expand Down Expand Up @@ -149,21 +151,51 @@ export function useKubeObject<K extends KubeObject>({

const data: Instance | null = query.error ? null : query.data ?? null;

useWebSocket<KubeListUpdateEvent<K>>({
url: () =>
makeUrl([KubeObjectEndpoint.toUrl(endpoint!)], {
...cleanedUpQueryParams,
watch: 1,
fieldSelector: `metadata.name=${name}`,
}),
enabled: !!endpoint && !!data,
cluster,
onMessage(update: KubeListUpdateEvent<K>) {
if (update.type !== 'ADDED' && update.object) {
client.setQueryData(queryKey, new kubeObjectClass(update.object));
}
},
});
const connectionsRequests = useMemo(() => {
if (!endpoint) return [];

return [
{
url: makeUrl([KubeObjectEndpoint.toUrl(endpoint!, namespace)], {
...cleanedUpQueryParams,
watch: 1,
fieldSelector: `metadata.name=${name}`,
}),
cluster,
onMessage(update: KubeListUpdateEvent<K>) {
if (update.type !== 'ADDED' && update.object) {
client.setQueryData(queryKey, new kubeObjectClass(update.object));
}
},
},
];
}, [endpoint]);

// Breaking rules of hooks here a little but
// getWebsocketMultiplexerEnabled is a feature toggle
// and not a variable so this `if` should never change during runtime
if (getWebsocketMultiplexerEnabled()) {
useWebSocket<KubeListUpdateEvent<K>>({
url: () =>
makeUrl([KubeObjectEndpoint.toUrl(endpoint!)], {
...cleanedUpQueryParams,
watch: 1,
fieldSelector: `metadata.name=${name}`,
}),
enabled: !!endpoint && !!data,
cluster,
onMessage(update: KubeListUpdateEvent<K>) {
if (update.type !== 'ADDED' && update.object) {
client.setQueryData(queryKey, new kubeObjectClass(update.object));
}
},
});
} else {
useWebSockets({
Comment on lines +174 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this violate the no-conditional hooks rule?

Copy link
Contributor Author

@sniok sniok Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of but not really, I explained it in the comment. Think of it as a compile time 'if', it will never change during the runtime

enabled: !!endpoint && !!data,
connections: connectionsRequests,
});
}

// @ts-ignore
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import WS from 'vitest-websocket-mock';
import { findKubeconfigByClusterName } from '../../../../stateless/findKubeconfigByClusterName';
import { getUserIdFromLocalStorage } from '../../../../stateless/getUserIdFromLocalStorage';
import { getCluster } from '../../../cluster';
import { BASE_WS_URL, MULTIPLEXER_ENDPOINT, useWebSocket, WebSocketManager } from './webSocket';
import { MULTIPLEXER_ENDPOINT } from './multiplexer';
import { useWebSocket } from './multiplexer';
import { WebSocketManager } from './multiplexer';
import { BASE_WS_URL } from './webSocket';

// Mock dependencies
vi.mock('../../../cluster', () => ({
Expand Down Expand Up @@ -51,7 +54,7 @@ vi.mock('./makeUrl', () => ({
const clusterName = 'test-cluster';
const userId = 'test-user';

describe('WebSocket Tests', () => {
describe('WebSocket Multiplexer', () => {
let mockServer: WS;
let onMessage: ReturnType<typeof vi.fn>;
let onError: ReturnType<typeof vi.fn>;
Expand Down
Loading
Loading