Added created time to pod state
Signed-off-by: haircommander <pehunt@redhat.com> Closes: #1079 Approved by: rhatdan
This commit is contained in:
parent
4f699db8da
commit
a2dde5a50d
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containers/storage"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
|
@ -36,6 +37,9 @@ type PodConfig struct {
|
|||
// If true, all containers joined to the pod will use the pod cgroup as
|
||||
// their cgroup parent, and cannot set a different cgroup parent
|
||||
UsePodCgroup bool
|
||||
|
||||
// Time pod was created
|
||||
CreatedTime time.Time `json:"created"`
|
||||
}
|
||||
|
||||
// podState represents a pod's state
|
||||
|
@ -64,6 +68,11 @@ func (p *Pod) Labels() map[string]string {
|
|||
return labels
|
||||
}
|
||||
|
||||
// CreatedTime gets the time when the pod was created
|
||||
func (p *Pod) CreatedTime() time.Time {
|
||||
return p.config.CreatedTime
|
||||
}
|
||||
|
||||
// CgroupParent returns the pod's CGroup parent
|
||||
func (p *Pod) CgroupParent() string {
|
||||
return p.config.CgroupParent
|
||||
|
@ -92,6 +101,7 @@ func newPod(lockDir string, runtime *Runtime) (*Pod, error) {
|
|||
pod.config = new(PodConfig)
|
||||
pod.config.ID = stringid.GenerateNonCryptoID()
|
||||
pod.config.Labels = make(map[string]string)
|
||||
pod.config.CreatedTime = time.Now()
|
||||
pod.state = new(podState)
|
||||
pod.runtime = runtime
|
||||
|
||||
|
|
|
@ -2,6 +2,9 @@ package libpod
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Contains the public Runtime API for pods
|
||||
|
@ -93,3 +96,36 @@ func (r *Runtime) Pods(filters ...PodFilter) ([]*Pod, error) {
|
|||
|
||||
return podsFiltered, nil
|
||||
}
|
||||
|
||||
// GetAllPods retrieves all pods
|
||||
func (r *Runtime) GetAllPods() ([]*Pod, error) {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
|
||||
if !r.valid {
|
||||
return nil, ErrRuntimeStopped
|
||||
}
|
||||
|
||||
return r.state.AllPods()
|
||||
}
|
||||
|
||||
// GetLatestPod returns a pod object of the latest created pod.
|
||||
func (r *Runtime) GetLatestPod() (*Pod, error) {
|
||||
lastCreatedIndex := -1
|
||||
var lastCreatedTime time.Time
|
||||
pods, err := r.GetAllPods()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to get all pods")
|
||||
}
|
||||
if len(pods) == 0 {
|
||||
return nil, ErrNoSuchPod
|
||||
}
|
||||
for podIndex, pod := range pods {
|
||||
createdTime := pod.config.CreatedTime
|
||||
if createdTime.After(lastCreatedTime) {
|
||||
lastCreatedTime = createdTime
|
||||
lastCreatedIndex = podIndex
|
||||
}
|
||||
}
|
||||
return pods[lastCreatedIndex], nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue