Skip to content

Commit

Permalink
fix: do not add again an existing Kubernetes resource (podman-desktop…
Browse files Browse the repository at this point in the history
…#9380)

* fix: do not add again an existing Kubernetes resource
Signed-off-by: Philippe Martin <[email protected]>

* fix: review
Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Oct 17, 2024
1 parent 89711ea commit 32eb3ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
25 changes: 19 additions & 6 deletions packages/main/src/plugin/kubernetes/contexts-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,13 @@ describe('update', async () => {
deployments: DEPLOYMENTS_NS1,
},
});
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('pods', Array(PODS_NS1).fill({}));
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('deployments', Array(DEPLOYMENTS_NS1).fill({}));
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('pods', [{ metadata: { uid: '0' } }]);
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('deployments', [
{ metadata: { uid: '0' } },
{ metadata: { uid: '1' } },
{ metadata: { uid: '2' } },
{ metadata: { uid: '3' } },
]);

const expectedCheckMap = new Map<string, CheckingState>();
expectedCheckMap.set('context1', { state: 'waiting' });
Expand Down Expand Up @@ -329,8 +334,13 @@ describe('update', async () => {
deployments: DEPLOYMENTS_NS1,
},
});
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('pods', Array(PODS_NS1).fill({}));
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('deployments', Array(DEPLOYMENTS_NS1).fill({}));
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('pods', [{ metadata: { uid: '0' } }]);
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('deployments', [
{ metadata: { uid: '0' } },
{ metadata: { uid: '1' } },
{ metadata: { uid: '2' } },
{ metadata: { uid: '3' } },
]);
});

test('should check current context if contexts are > 10', async () => {
Expand Down Expand Up @@ -860,7 +870,10 @@ describe('update', async () => {
},
});
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('pods', []);
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('deployments', [{}, {}]);
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith('deployments', [
{ metadata: { uid: '0' } },
{ metadata: { uid: '1' } },
]);

vi.advanceTimersToNextTimer(); // error event
vi.advanceTimersToNextTimer(); // dispatches
Expand Down Expand Up @@ -1038,7 +1051,7 @@ describe('update', async () => {
deployments: 0,
},
});
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith(resource, [{}]);
expect(dispatchCurrentContextResourceSpy).toHaveBeenCalledWith(resource, [{ metadata: { uid: '0' } }]);
});

test('createInformer should send data for deleted and updated resource', async () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/main/src/plugin/kubernetes/contexts-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,13 @@ export class ContextsManager {
sendGeneral: true,
currentContext: this.kubeConfig.currentContext,
resources: { pods: true },
update: state => state.resources.pods.push(obj),
update: state => {
if (state.resources.pods.some(o => o.metadata?.uid !== obj.metadata?.uid)) {
console.debug(`pod ${obj.metadata?.name} already added in context ${this.kubeConfig.currentContext}`);
}
state.resources.pods = state.resources.pods.filter(o => o.metadata?.uid !== obj.metadata?.uid);
state.resources.pods.push(obj);
},
});
},
onUpdate: obj => {
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/plugin/kubernetes/test-informer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class TestInformer {
}
if (this.connectResponse === undefined) {
for (let i = 0; i < this.resourcesCount; i++) {
this.onCb.get('add')?.({});
this.onCb.get('add')?.({ metadata: { uid: i.toString() } });
}
this.events.forEach(event => {
setTimeout(() => {
Expand Down

0 comments on commit 32eb3ea

Please sign in to comment.