Skip to content

Commit

Permalink
fix: clear local state for services when informer is stopped (podman-…
Browse files Browse the repository at this point in the history
…desktop#6250)

* fix: clear local state for services when informer is stopped
Signed-off-by: Philippe Martin <[email protected]>

* test: add unit test
Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Mar 4, 2024
1 parent 2bcb5f6 commit ef60133
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 5 additions & 2 deletions packages/main/src/plugin/kubernetes-context-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ test('calling getCurrentContextResources should start service informer, the firs
expect(makeInformerMock).not.toHaveBeenCalled();
});

test('changing context should stop service informer on previous current context', async () => {
test('changing context should stop service informer on previous current context and clear state', async () => {
vi.useFakeTimers();
const makeInformerMock = vi.mocked(makeInformer);
makeInformerMock.mockImplementation(
Expand All @@ -1140,7 +1140,7 @@ test('changing context should stop service informer on previous current context'
path: string,
_listPromiseFn: kubeclient.ListPromise<kubeclient.KubernetesObject>,
) => {
return new FakeInformer(kubeconfig.currentContext, path, 0, undefined, [], []);
return new FakeInformer(kubeconfig.currentContext, path, 1, undefined, [], []);
},
);
const client = new ContextsManager(apiSender);
Expand Down Expand Up @@ -1188,6 +1188,8 @@ test('changing context should stop service informer on previous current context'
expect.anything(),
);

expect(client.getContextResources('context1', 'services').length).toBe(1);

makeInformerMock.mockClear();

config.currentContext = 'context2';
Expand All @@ -1199,6 +1201,7 @@ test('changing context should stop service informer on previous current context'

expect(informerStopMock).toHaveBeenCalledTimes(1);
expect(informerStopMock).toHaveBeenCalledWith('context1', '/api/v1/namespaces/ns1/services');
expect(client.getContextResources('context1', 'services').length).toBe(0);
});

describe('ContextsStates tests', () => {
Expand Down
17 changes: 13 additions & 4 deletions packages/main/src/plugin/kubernetes-context-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class ContextsStates {
};
}

getCurrentContextResources(current: string, resourceName: ResourceName): KubernetesObject[] {
getContextResources(current: string, resourceName: ResourceName): KubernetesObject[] {
if (current) {
const state = this.state.get(current);
if (!state?.reachable) {
Expand Down Expand Up @@ -273,9 +273,13 @@ export class ContextsStates {
if (informers) {
for (const [resourceName, informer] of informers) {
if (isSecondaryResourceName(resourceName)) {
console.debug(`stop watching ${resourceName} in context ${contextName}`);
await informer?.stop();
// We clear the informer and the local state
informers.delete(resourceName);
const state = this.state.get(contextName);
if (state) {
state.resources[resourceName] = [];
}
}
}
}
Expand Down Expand Up @@ -663,7 +667,7 @@ export class ContextsManager {
private dispatchCurrentContextResource(resourceName: ResourceName): void {
this.apiSender.send(
`kubernetes-current-context-${resourceName}-update`,
this.states.getCurrentContextResources(this.kubeConfig.currentContext, resourceName),
this.states.getContextResources(this.kubeConfig.currentContext, resourceName),
);
}

Expand All @@ -673,7 +677,7 @@ export class ContextsManager {
}
if (this.states.hasInformer(this.currentContext, resourceName)) {
console.debug(`already watching ${resourceName} in context ${this.currentContext}`);
return this.states.getCurrentContextResources(this.kubeConfig.currentContext, resourceName);
return this.states.getContextResources(this.kubeConfig.currentContext, resourceName);
}
if (!this.states.isReachable(this.currentContext)) {
console.debug(`skip watching ${resourceName} in context ${this.currentContext}, as the context is not reachable`);
Expand All @@ -683,4 +687,9 @@ export class ContextsManager {
this.startResourceInformer(this.currentContext, resourceName);
return [];
}

// for tests
public getContextResources(contextName: string, resourceName: ResourceName): KubernetesObject[] {
return this.states.getContextResources(contextName, resourceName);
}
}

0 comments on commit ef60133

Please sign in to comment.