diff options
Diffstat (limited to 'libpod/runtime_pod.go')
-rw-r--r-- | libpod/runtime_pod.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libpod/runtime_pod.go b/libpod/runtime_pod.go index f5a2b017b..280699b79 100644 --- a/libpod/runtime_pod.go +++ b/libpod/runtime_pod.go @@ -4,6 +4,7 @@ import ( "context" "time" + "github.com/containers/libpod/pkg/util" "github.com/pkg/errors" ) @@ -129,3 +130,36 @@ func (r *Runtime) GetLatestPod() (*Pod, error) { } return pods[lastCreatedIndex], nil } + +// GetRunningPods returns an array of running pods +func (r *Runtime) GetRunningPods() ([]*Pod, error) { + var ( + pods []string + runningPods []*Pod + ) + r.lock.RLock() + defer r.lock.RUnlock() + + if !r.valid { + return nil, ErrRuntimeStopped + } + containers, err := r.GetRunningContainers() + if err != nil { + return nil, err + } + // Assemble running pods + for _, c := range containers { + if !util.StringInSlice(c.PodID(), pods) { + pods = append(pods, c.PodID()) + pod, err := r.GetPod(c.PodID()) + if err != nil { + if errors.Cause(err) == ErrPodRemoved || errors.Cause(err) == ErrNoSuchPod { + continue + } + return nil, err + } + runningPods = append(runningPods, pod) + } + } + return runningPods, nil +} |