Skip to content

Commit e069d3f

Browse files
authored
Merge pull request #2697 from headlamp-k8s/watching-namespaces-fix
frontend: Fix useKubeObjectLists when deselecting namespaces
2 parents d87a40e + ce50181 commit e069d3f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

frontend/src/lib/k8s/api/v2/useKubeObjectList.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
kubeObjectListQuery,
55
ListResponse,
66
makeListRequests,
7+
useKubeObjectList,
78
useWatchKubeObjectLists,
89
} from './useKubeObjectList';
910
import * as websocket from './webSocket';
@@ -81,6 +82,16 @@ const mockClass = class {
8182
static apiVersion = 'v1';
8283
static apiName = 'pods';
8384

85+
static apiEndpoint = {
86+
apiInfo: [
87+
{
88+
group: '',
89+
resource: 'pods',
90+
version: 'v1',
91+
},
92+
],
93+
};
94+
8495
constructor(public jsonData: any) {}
8596
} as any;
8697

@@ -221,3 +232,42 @@ describe('useWatchKubeObjectLists', () => {
221232
).toBe(objectB);
222233
});
223234
});
235+
236+
describe('useKubeObjectList', () => {
237+
it('should call useKubeObjectList with 1 namespace after reducing amount of namespaces', async () => {
238+
const spy = vi.spyOn(websocket, 'useWebSockets');
239+
const queryClient = new QueryClient();
240+
241+
queryClient.setQueryData(['kubeObject', 'list', 'v1', 'pods', 'default', 'a', {}], {
242+
list: { items: [], metadata: { resourceVersion: '0' } },
243+
cluster: 'default',
244+
namespace: 'a',
245+
});
246+
queryClient.setQueryData(['kubeObject', 'list', 'v1', 'pods', 'default', 'b', {}], {
247+
list: { items: [], metadata: { resourceVersion: '0' } },
248+
cluster: 'default',
249+
namespace: 'b',
250+
});
251+
252+
const result = renderHook(
253+
(props: {}) =>
254+
useKubeObjectList({
255+
kubeObjectClass: mockClass,
256+
requests: [{ cluster: 'default', namespaces: ['a', 'b'] }],
257+
...props,
258+
}),
259+
{
260+
wrapper: ({ children }) => (
261+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
262+
),
263+
}
264+
);
265+
266+
result.rerender({ requests: [{ cluster: 'default', namespaces: ['a'] }] });
267+
268+
expect(spy.mock.calls[0][0].connections.length).toBe(0); // initial render
269+
expect(spy.mock.calls[1][0].connections.length).toBe(2); // new connections with 'a' and 'b' namespaces
270+
expect(spy.mock.calls[2][0].connections.length).toBe(2); // rerender with new props
271+
expect(spy.mock.calls[3][0].connections.length).toBe(1); // updated connections after we removed namespace 'b'
272+
});
273+
});

frontend/src/lib/k8s/api/v2/useKubeObjectList.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ export function useKubeObjectList<K extends KubeObject>({
306306
setListsToWatch([...listsToWatch, ...listsNotYetWatched]);
307307
}
308308

309+
const listsToStopWatching = listsToWatch.filter(
310+
watching =>
311+
requests.find(request =>
312+
watching.cluster === request?.cluster && request.namespaces && watching.namespace
313+
? request.namespaces?.includes(watching.namespace)
314+
: true
315+
) === undefined
316+
);
317+
318+
if (listsToStopWatching.length > 0) {
319+
setListsToWatch(listsToWatch.filter(it => !listsToStopWatching.includes(it)));
320+
}
321+
309322
useWatchKubeObjectLists({
310323
lists: shouldWatch ? listsToWatch : [],
311324
endpoint,

0 commit comments

Comments
 (0)