Skip to content

Commit

Permalink
Merge pull request #23325 from Luap99/ps-pod-err
Browse files Browse the repository at this point in the history
podman ps: fix racy pod name query
  • Loading branch information
openshift-merge-bot[bot] authored Jul 18, 2024
2 parents 73986f6 + 182224d commit 164ecb2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
5 changes: 5 additions & 0 deletions pkg/domain/infra/abi/pods_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/containers/common/pkg/cgroups"
"github.com/containers/podman/v5/libpod"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/docker/go-units"
Expand Down Expand Up @@ -39,6 +40,10 @@ func (ic *ContainerEngine) podsToStatsReport(pods []*libpod.Pod) ([]*entities.Po
for i := range pods { // Access by index to prevent potential loop-variable leaks.
podStats, err := pods[i].GetPodStats(nil)
if err != nil {
// pod was removed, skip it
if errors.Is(err, define.ErrNoSuchPod) {
continue
}
return nil, err
}
podID := pods[i].ID()[:12]
Expand Down
27 changes: 12 additions & 15 deletions pkg/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
for _, con := range cons {
listCon, err := ListContainerBatch(runtime, con, options)
switch {
case errors.Is(err, define.ErrNoSuchCtr):
// ignore both no ctr and no such pod errors as it means the ctr is gone now
case errors.Is(err, define.ErrNoSuchCtr), errors.Is(err, define.ErrNoSuchPod):
continue
case err != nil:
return nil, err
Expand Down Expand Up @@ -148,6 +149,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
networks []string
healthStatus string
restartCount uint
podName string
)

batchErr := ctr.Batch(func(c *libpod.Container) error {
Expand Down Expand Up @@ -201,10 +203,6 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
return err
}

if !opts.Size && !opts.Namespace {
return nil
}

if opts.Namespace {
ctrPID := strconv.Itoa(pid)
cgroup, _ = getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "cgroup"))
Expand All @@ -231,6 +229,14 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
size.RootFsSize = rootFsSize
size.RwSize = rwSize
}

if opts.Pod && len(conConfig.Pod) > 0 {
podName, err = rt.GetPodName(conConfig.Pod)
if err != nil {
return fmt.Errorf("could not find container %s pod (id %s) in state: %w", conConfig.ID, conConfig.Pod, err)
}
}

return nil
})
if batchErr != nil {
Expand All @@ -256,23 +262,14 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
Networks: networks,
Pid: pid,
Pod: conConfig.Pod,
PodName: podName,
Ports: portMappings,
Restarts: restartCount,
Size: size,
StartedAt: startedTime.Unix(),
State: conState.String(),
Status: healthStatus,
}
if opts.Pod && len(conConfig.Pod) > 0 {
podName, err := rt.GetPodName(conConfig.Pod)
if err != nil {
if errors.Is(err, define.ErrNoSuchCtr) {
return entities.ListContainer{}, fmt.Errorf("could not find container %s pod (id %s) in state: %w", conConfig.ID, conConfig.Pod, define.ErrNoSuchPod)
}
return entities.ListContainer{}, err
}
ps.PodName = podName
}

if opts.Namespace {
ps.Namespaces = entities.ListContainerNamespaces{
Expand Down

0 comments on commit 164ecb2

Please sign in to comment.