1 Star 0 Fork 1

Derek Ray/podman

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
runtime_pod.go 3.10 KB
一键复制 编辑 原始数据 按行查看 历史
haircommander 提交于 2018-07-11 16:27 . Added created time to pod state
package libpod
import (
"context"
"time"
"github.com/pkg/errors"
)
// Contains the public Runtime API for pods
// A PodCreateOption is a functional option which alters the Pod created by
// NewPod
type PodCreateOption func(*Pod) error
// PodFilter is a function to determine whether a pod is included in command
// output. Pods to be outputted are tested using the function. A true return
// will include the pod, a false return will exclude it.
type PodFilter func(*Pod) bool
// RemovePod removes a pod
// If removeCtrs is specified, containers will be removed
// Otherwise, a pod that is not empty will return an error and not be removed
// If force is specified with removeCtrs, all containers will be stopped before
// being removed
// Otherwise, the pod will not be removed if any containers are running
func (r *Runtime) RemovePod(ctx context.Context, p *Pod, removeCtrs, force bool) error {
return r.removePod(ctx, p, removeCtrs, force)
}
// GetPod retrieves a pod by its ID
func (r *Runtime) GetPod(id string) (*Pod, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if !r.valid {
return nil, ErrRuntimeStopped
}
return r.state.Pod(id)
}
// HasPod checks to see if a pod with the given ID exists
func (r *Runtime) HasPod(id string) (bool, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if !r.valid {
return false, ErrRuntimeStopped
}
return r.state.HasPod(id)
}
// LookupPod retrieves a pod by its name or a partial ID
// If a partial ID is not unique, an error will be returned
func (r *Runtime) LookupPod(idOrName string) (*Pod, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if !r.valid {
return nil, ErrRuntimeStopped
}
return r.state.LookupPod(idOrName)
}
// Pods retrieves all pods
// Filters can be provided which will determine which pods are included in the
// output. Multiple filters are handled by ANDing their output, so only pods
// matching all filters are returned
func (r *Runtime) Pods(filters ...PodFilter) ([]*Pod, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if !r.valid {
return nil, ErrRuntimeStopped
}
pods, err := r.state.AllPods()
if err != nil {
return nil, err
}
podsFiltered := make([]*Pod, 0, len(pods))
for _, pod := range pods {
include := true
for _, filter := range filters {
include = include && filter(pod)
}
if include {
podsFiltered = append(podsFiltered, pod)
}
}
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/derekhjray/podman.git
git@gitee.com:derekhjray/podman.git
derekhjray
podman
podman
v0.7.4

搜索帮助