diff options
Diffstat (limited to 'libpod/runtime_ctr.go')
-rw-r--r-- | libpod/runtime_ctr.go | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 914d457a0..0f39ead35 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -3,6 +3,7 @@ package libpod import ( "os" "path/filepath" + "time" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" @@ -204,7 +205,6 @@ func (r *Runtime) LookupContainer(idOrName string) (*Container, error) { if !r.valid { return nil, ErrRuntimeStopped } - return r.state.LookupContainer(idOrName) } @@ -268,3 +268,21 @@ func (r *Runtime) GetContainersByList(containers []string) ([]*Container, error) } return ctrs, nil } + +// GetLatestContainer returns a container object of the latest created container. +func (r *Runtime) GetLatestContainer() (*Container, error) { + var lastCreatedIndex int + var lastCreatedTime time.Time + ctrs, err := r.GetAllContainers() + if err != nil { + return nil, errors.Wrapf(err, "unable to find latest container") + } + for containerIndex, ctr := range ctrs { + createdTime := ctr.config.CreatedTime + if createdTime.After(lastCreatedTime) { + lastCreatedTime = createdTime + lastCreatedIndex = containerIndex + } + } + return ctrs[lastCreatedIndex], nil +} |