Skip to content

Commit

Permalink
Added created time to pod state
Browse files Browse the repository at this point in the history
Signed-off-by: haircommander <[email protected]>

Closes: #1079
Approved by: rhatdan
  • Loading branch information
haircommander authored and rh-atomic-bot committed Jul 12, 2018
1 parent 4f699db commit a2dde5a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libpod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"path/filepath"
"strings"
"time"

"github.com/containers/storage"
"github.com/docker/docker/pkg/stringid"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions libpod/runtime_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package libpod

import (
"context"
"time"

"github.com/pkg/errors"
)

// Contains the public Runtime API for pods
Expand Down Expand Up @@ -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
}

0 comments on commit a2dde5a

Please sign in to comment.