11
"github.com/containers/podman/v5/libpod/define"
12
"golang.org/x/exp/slices"
15
// Contains the public Runtime API for pods
17
// A PodCreateOption is a functional option which alters the Pod created by
19
type PodCreateOption func(*Pod) error
21
// PodFilter is a function to determine whether a pod is included in command
22
// output. Pods to be outputted are tested using the function. A true return
23
// will include the pod, a false return will exclude it.
24
type PodFilter func(*Pod) bool
26
// RemovePod removes a pod
27
// If removeCtrs is specified, containers will be removed
28
// Otherwise, a pod that is not empty will return an error and not be removed
29
// If force is specified with removeCtrs, all containers will be stopped before
31
// Otherwise, the pod will not be removed if any containers are running
32
func (r *Runtime) RemovePod(ctx context.Context, p *Pod, removeCtrs, force bool, timeout *uint) (map[string]error, error) {
34
return nil, define.ErrRuntimeStopped
38
if ok, _ := r.state.HasPod(p.ID()); !ok {
39
// Pod probably already removed
40
// Or was never in the runtime to begin with
41
return make(map[string]error), nil
48
return r.removePod(ctx, p, removeCtrs, force, timeout)
51
// GetPod retrieves a pod by its ID
52
func (r *Runtime) GetPod(id string) (*Pod, error) {
54
return nil, define.ErrRuntimeStopped
57
return r.state.Pod(id)
60
// HasPod checks to see if a pod with the given ID exists
61
func (r *Runtime) HasPod(id string) (bool, error) {
63
return false, define.ErrRuntimeStopped
66
return r.state.HasPod(id)
69
// LookupPod retrieves a pod by its name or a partial ID
70
// If a partial ID is not unique, an error will be returned
71
func (r *Runtime) LookupPod(idOrName string) (*Pod, error) {
73
return nil, define.ErrRuntimeStopped
76
return r.state.LookupPod(idOrName)
79
// Pods retrieves all pods
80
// Filters can be provided which will determine which pods are included in the
81
// output. Multiple filters are handled by ANDing their output, so only pods
82
// matching all filters are returned
83
func (r *Runtime) Pods(filters ...PodFilter) ([]*Pod, error) {
84
pods, err := r.GetAllPods()
88
podsFiltered := make([]*Pod, 0, len(pods))
89
for _, pod := range pods {
91
for _, filter := range filters {
92
include = include && filter(pod)
96
podsFiltered = append(podsFiltered, pod)
100
return podsFiltered, nil
103
// GetAllPods retrieves all pods
104
func (r *Runtime) GetAllPods() ([]*Pod, error) {
106
return nil, define.ErrRuntimeStopped
109
return r.state.AllPods()
112
// GetLatestPod returns a pod object of the latest created pod.
113
func (r *Runtime) GetLatestPod() (*Pod, error) {
114
lastCreatedIndex := -1
115
var lastCreatedTime time.Time
116
pods, err := r.GetAllPods()
118
return nil, fmt.Errorf("unable to get all pods: %w", err)
121
return nil, define.ErrNoSuchPod
123
for podIndex, pod := range pods {
124
createdTime := pod.config.CreatedTime
125
if createdTime.After(lastCreatedTime) {
126
lastCreatedTime = createdTime
127
lastCreatedIndex = podIndex
130
return pods[lastCreatedIndex], nil
133
// GetRunningPods returns an array of running pods
134
func (r *Runtime) GetRunningPods() ([]*Pod, error) {
140
return nil, define.ErrRuntimeStopped
142
containers, err := r.GetRunningContainers()
146
// Assemble running pods
147
for _, c := range containers {
148
if !slices.Contains(pods, c.PodID()) {
149
pods = append(pods, c.PodID())
150
pod, err := r.GetPod(c.PodID())
152
if errors.Is(err, define.ErrPodRemoved) || errors.Is(err, define.ErrNoSuchPod) {
157
runningPods = append(runningPods, pod)
160
return runningPods, nil
163
// PrunePods removes unused pods and their containers from local storage.
164
func (r *Runtime) PrunePods(ctx context.Context) (map[string]error, error) {
165
response := make(map[string]error)
166
states := []string{define.PodStateStopped, define.PodStateExited}
167
filterFunc := func(p *Pod) bool {
168
state, _ := p.GetPodStatus()
169
for _, status := range states {
176
pods, err := r.Pods(filterFunc)
183
for _, pod := range pods {
185
_, err := r.removePod(context.TODO(), pod, true, false, timeout)
186
response[pod.ID()] = err