mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-25 15:02:45 +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,42 +589,40 @@ func StatusContainer(ctx context.Context, sandboxID, containerID string) (Contai
|
||||
// taken from the caller, even if we simply return the container status without
|
||||
// taking any action regarding the container.
|
||||
func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, error) {
|
||||
for _, container := range sandbox.containers {
|
||||
if container.id == containerID {
|
||||
// We have to check for the process state to make sure
|
||||
// we update the status in case the process is supposed
|
||||
// to be running but has been killed or terminated.
|
||||
if (container.state.State == types.StateReady ||
|
||||
container.state.State == types.StateRunning ||
|
||||
container.state.State == types.StatePaused) &&
|
||||
container.process.Pid > 0 {
|
||||
if container, ok := sandbox.containers[containerID]; ok {
|
||||
// We have to check for the process state to make sure
|
||||
// we update the status in case the process is supposed
|
||||
// to be running but has been killed or terminated.
|
||||
if (container.state.State == types.StateReady ||
|
||||
container.state.State == types.StateRunning ||
|
||||
container.state.State == types.StatePaused) &&
|
||||
container.process.Pid > 0 {
|
||||
|
||||
running, err := isShimRunning(container.process.Pid)
|
||||
if err != nil {
|
||||
return ContainerStatus{}, err
|
||||
}
|
||||
|
||||
if !running {
|
||||
virtLog.WithFields(logrus.Fields{
|
||||
"state": container.state.State,
|
||||
"pid": container.process.Pid}).
|
||||
Info("container isn't running")
|
||||
if err := container.stop(true); err != nil {
|
||||
return ContainerStatus{}, err
|
||||
}
|
||||
}
|
||||
running, err := isShimRunning(container.process.Pid)
|
||||
if err != nil {
|
||||
return ContainerStatus{}, err
|
||||
}
|
||||
|
||||
return ContainerStatus{
|
||||
ID: container.id,
|
||||
State: container.state,
|
||||
PID: container.process.Pid,
|
||||
StartTime: container.process.StartTime,
|
||||
RootFs: container.config.RootFs.Target,
|
||||
Spec: container.GetOCISpec(),
|
||||
Annotations: container.config.Annotations,
|
||||
}, nil
|
||||
if !running {
|
||||
virtLog.WithFields(logrus.Fields{
|
||||
"state": container.state.State,
|
||||
"pid": container.process.Pid}).
|
||||
Info("container isn't running")
|
||||
if err := container.stop(true); err != nil {
|
||||
return ContainerStatus{}, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ContainerStatus{
|
||||
ID: container.id,
|
||||
State: container.state,
|
||||
PID: container.process.Pid,
|
||||
StartTime: container.process.StartTime,
|
||||
RootFs: container.config.RootFs.Target,
|
||||
Spec: container.GetOCISpec(),
|
||||
Annotations: container.config.Annotations,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// No matching containers in the sandbox
|
||||
|
@ -752,10 +752,8 @@ func (s *Sandbox) findContainer(containerID string) (*Container, error) {
|
||||
return nil, vcTypes.ErrNeedContainerID
|
||||
}
|
||||
|
||||
for id, c := range s.containers {
|
||||
if containerID == id {
|
||||
return c, nil
|
||||
}
|
||||
if c, ok := s.containers[containerID]; ok {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
return nil, errors.Wrapf(vcTypes.ErrNoSuchContainer, "Could not find the container %q from the sandbox %q containers list",
|
||||
@ -1324,21 +1322,20 @@ func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
|
||||
return ContainerStatus{}, vcTypes.ErrNeedContainerID
|
||||
}
|
||||
|
||||
for id, c := range s.containers {
|
||||
if c, ok := s.containers[containerID]; ok {
|
||||
rootfs := c.config.RootFs.Source
|
||||
if c.config.RootFs.Mounted {
|
||||
rootfs = c.config.RootFs.Target
|
||||
}
|
||||
if id == containerID {
|
||||
return ContainerStatus{
|
||||
ID: c.id,
|
||||
State: c.state,
|
||||
PID: c.process.Pid,
|
||||
StartTime: c.process.StartTime,
|
||||
RootFs: rootfs,
|
||||
Annotations: c.config.Annotations,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return ContainerStatus{
|
||||
ID: c.id,
|
||||
State: c.state,
|
||||
PID: c.process.Pid,
|
||||
StartTime: c.process.StartTime,
|
||||
RootFs: rootfs,
|
||||
Annotations: c.config.Annotations,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return ContainerStatus{}, vcTypes.ErrNoSuchContainer
|
||||
|
Loading…
Reference in New Issue
Block a user