mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 15:32:30 +00:00
virtcontainers: improve algorithm to find containers
Do not iterate over a map to find a container, use map built-in method instead. fixes #2254 Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
parent
fea166d8eb
commit
7f67b9f084
@ -589,8 +589,7 @@ func StatusContainer(ctx context.Context, sandboxID, containerID string) (Contai
|
|||||||
// taken from the caller, even if we simply return the container status without
|
// taken from the caller, even if we simply return the container status without
|
||||||
// taking any action regarding the container.
|
// taking any action regarding the container.
|
||||||
func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, error) {
|
func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, error) {
|
||||||
for _, container := range sandbox.containers {
|
if container, ok := sandbox.containers[containerID]; ok {
|
||||||
if container.id == containerID {
|
|
||||||
// We have to check for the process state to make sure
|
// We have to check for the process state to make sure
|
||||||
// we update the status in case the process is supposed
|
// we update the status in case the process is supposed
|
||||||
// to be running but has been killed or terminated.
|
// to be running but has been killed or terminated.
|
||||||
@ -625,7 +624,6 @@ func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, err
|
|||||||
Annotations: container.config.Annotations,
|
Annotations: container.config.Annotations,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// No matching containers in the sandbox
|
// No matching containers in the sandbox
|
||||||
return ContainerStatus{}, nil
|
return ContainerStatus{}, nil
|
||||||
|
@ -752,11 +752,9 @@ func (s *Sandbox) findContainer(containerID string) (*Container, error) {
|
|||||||
return nil, vcTypes.ErrNeedContainerID
|
return nil, vcTypes.ErrNeedContainerID
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, c := range s.containers {
|
if c, ok := s.containers[containerID]; ok {
|
||||||
if containerID == id {
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.Wrapf(vcTypes.ErrNoSuchContainer, "Could not find the container %q from the sandbox %q containers list",
|
return nil, errors.Wrapf(vcTypes.ErrNoSuchContainer, "Could not find the container %q from the sandbox %q containers list",
|
||||||
containerID, s.id)
|
containerID, s.id)
|
||||||
@ -1324,12 +1322,12 @@ func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
|
|||||||
return ContainerStatus{}, vcTypes.ErrNeedContainerID
|
return ContainerStatus{}, vcTypes.ErrNeedContainerID
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, c := range s.containers {
|
if c, ok := s.containers[containerID]; ok {
|
||||||
rootfs := c.config.RootFs.Source
|
rootfs := c.config.RootFs.Source
|
||||||
if c.config.RootFs.Mounted {
|
if c.config.RootFs.Mounted {
|
||||||
rootfs = c.config.RootFs.Target
|
rootfs = c.config.RootFs.Target
|
||||||
}
|
}
|
||||||
if id == containerID {
|
|
||||||
return ContainerStatus{
|
return ContainerStatus{
|
||||||
ID: c.id,
|
ID: c.id,
|
||||||
State: c.state,
|
State: c.state,
|
||||||
@ -1339,7 +1337,6 @@ func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
|
|||||||
Annotations: c.config.Annotations,
|
Annotations: c.config.Annotations,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ContainerStatus{}, vcTypes.ErrNoSuchContainer
|
return ContainerStatus{}, vcTypes.ErrNoSuchContainer
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user