mirror of
https://github.com/containers/podman.git
synced 2025-06-20 09:03:43 +08:00
Use abi PodPs implementation for libpod/pods/json endpoint
This removes unnecessary code duplication. Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
@ -43,6 +43,7 @@ func PodCreate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func Pods(w http.ResponseWriter, r *http.Request) {
|
||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||
decoder := r.Context().Value("decoder").(*schema.Decoder)
|
||||
query := struct {
|
||||
Filters map[string][]string `schema:"filters"`
|
||||
@ -55,7 +56,11 @@ func Pods(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
pods, err := utils.GetPods(w, r)
|
||||
containerEngine := abi.ContainerEngine{Libpod: runtime}
|
||||
podPSOptions := entities.PodPSOptions{
|
||||
Filters: query.Filters,
|
||||
}
|
||||
pods, err := containerEngine.PodPs(r.Context(), podPSOptions)
|
||||
if err != nil {
|
||||
utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
|
||||
return
|
||||
|
@ -1,87 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/containers/podman/v2/libpod"
|
||||
"github.com/containers/podman/v2/pkg/domain/entities"
|
||||
dfilters "github.com/containers/podman/v2/pkg/domain/filters"
|
||||
"github.com/gorilla/schema"
|
||||
)
|
||||
|
||||
func GetPods(w http.ResponseWriter, r *http.Request) ([]*entities.ListPodsReport, error) {
|
||||
var (
|
||||
pods []*libpod.Pod
|
||||
)
|
||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||
decoder := r.Context().Value("decoder").(*schema.Decoder)
|
||||
|
||||
query := struct {
|
||||
All bool
|
||||
Filters map[string][]string `schema:"filters"`
|
||||
Digests bool
|
||||
}{}
|
||||
|
||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, found := r.URL.Query()["digests"]; found && query.Digests {
|
||||
UnSupportedParameter("digests")
|
||||
}
|
||||
|
||||
filters := make([]libpod.PodFilter, 0, len(query.Filters))
|
||||
for k, v := range query.Filters {
|
||||
f, err := dfilters.GeneratePodFilterFunc(k, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filters = append(filters, f)
|
||||
}
|
||||
pods, err := runtime.Pods(filters...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(pods) == 0 {
|
||||
return []*entities.ListPodsReport{}, nil
|
||||
}
|
||||
|
||||
lps := make([]*entities.ListPodsReport, 0, len(pods))
|
||||
for _, pod := range pods {
|
||||
status, err := pod.GetPodStatus()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctrs, err := pod.AllContainers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
infraID, err := pod.InfraContainerID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lp := entities.ListPodsReport{
|
||||
Cgroup: pod.CgroupParent(),
|
||||
Created: pod.CreatedTime(),
|
||||
Id: pod.ID(),
|
||||
Name: pod.Name(),
|
||||
Namespace: pod.Namespace(),
|
||||
Status: status,
|
||||
InfraId: infraID,
|
||||
Labels: pod.Labels(),
|
||||
}
|
||||
for _, ctr := range ctrs {
|
||||
state, err := ctr.State()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lp.Containers = append(lp.Containers, &entities.ListPodContainer{
|
||||
Id: ctr.ID(),
|
||||
Names: ctr.Name(),
|
||||
Status: state.String(),
|
||||
})
|
||||
}
|
||||
lps = append(lps, &lp)
|
||||
}
|
||||
return lps, nil
|
||||
}
|
Reference in New Issue
Block a user