From 66516e121499116ede5356b9b6093b604b62b73f Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Wed, 5 Jul 2023 11:38:06 +0200 Subject: [PATCH] Clean up container removal handling There is no need to re-read the state of all containers from the API -- this is very expensive and adds to the API call avalanche. Just delete the container from the state. This also removes the last case where updateContainers() was called after initialization. Thus eliminate the `init` parameter and rename the method to initContainers. --- src/app.jsx | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/app.jsx b/src/app.jsx index 669055358..ef203047a 100644 --- a/src/app.jsx +++ b/src/app.jsx @@ -190,7 +190,7 @@ class Application extends React.Component { .then(scriptResult => scriptResult === "0\n"); } - updateContainers(system, init) { + initContainers(system) { return client.getContainers(system) .then(reply => Promise.all( (reply || []).map(container => @@ -203,7 +203,7 @@ class Application extends React.Component { ) )) .then(reply => { - debug(system, "updateContainers init", init, "getContainers", JSON.stringify(reply)); + debug(system, "initContainers getContainers", JSON.stringify(reply)); this.setState(prevState => { // Copy only containers that could not be deleted with this event // So when event from system come, only copy user containers and vice versa @@ -222,11 +222,9 @@ class Application extends React.Component { [system ? "systemContainersLoaded" : "userContainersLoaded"]: true, }; }); - if (init) { - this.updateContainerStats(system); - for (const container of reply || []) { - this.inspectContainerDetail(container.Id, system); - } + this.updateContainerStats(system); + for (const container of reply || []) { + this.inspectContainerDetail(container.Id, system); } }) .catch(console.log); @@ -412,13 +410,10 @@ class Application extends React.Component { this.updateContainer(event.Actor.ID, system, event); break; case 'remove': - this.updateContainers(system).then(() => { - // HACK: we don't get a pod event when a container in a pod is removed. - // https://github.com/containers/podman/issues/15408 - if (event.Actor.Attributes.podId) - this.updatePod(event.Actor.Attributes.podId, system); - else - this.updatePods(system); + this.setState(prevState => { + const containers = { ...prevState.containers }; + delete containers[event.id + system.toString()]; + return { containers }; }); break; /* The following events need only to update the Image list */ @@ -488,7 +483,7 @@ class Application extends React.Component { cgroupVersion: reply.host.cgroupVersion, }); this.updateImages(system); - this.updateContainers(system, true); + this.initContainers(system); this.updatePods(system); client.streamEvents(system, message => this.handleEvent(message, system))