github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/utils/pods.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/containers/libpod/cmd/podman/shared" 8 "github.com/containers/libpod/libpod" 9 "github.com/containers/libpod/pkg/domain/entities" 10 "github.com/gorilla/schema" 11 ) 12 13 func GetPods(w http.ResponseWriter, r *http.Request) ([]*entities.ListPodsReport, error) { 14 var ( 15 lps []*entities.ListPodsReport 16 pods []*libpod.Pod 17 podErr error 18 ) 19 runtime := r.Context().Value("runtime").(*libpod.Runtime) 20 decoder := r.Context().Value("decoder").(*schema.Decoder) 21 22 query := struct { 23 All bool 24 Filters map[string][]string `schema:"filters"` 25 Digests bool 26 }{} 27 28 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 29 return nil, err 30 } 31 var filters = []string{} 32 if _, found := r.URL.Query()["digests"]; found && query.Digests { 33 UnSupportedParameter("digests") 34 } 35 36 if len(query.Filters) > 0 { 37 for k, v := range query.Filters { 38 for _, val := range v { 39 filters = append(filters, fmt.Sprintf("%s=%s", k, val)) 40 } 41 } 42 filterFuncs, err := shared.GenerateFilterFunction(runtime, filters) 43 if err != nil { 44 return nil, err 45 } 46 pods, podErr = shared.FilterAllPodsWithFilterFunc(runtime, filterFuncs...) 47 } else { 48 pods, podErr = runtime.GetAllPods() 49 } 50 if podErr != nil { 51 return nil, podErr 52 } 53 for _, pod := range pods { 54 status, err := pod.GetPodStatus() 55 if err != nil { 56 return nil, err 57 } 58 ctrs, err := pod.AllContainers() 59 if err != nil { 60 return nil, err 61 } 62 infraId, err := pod.InfraContainerID() 63 if err != nil { 64 return nil, err 65 } 66 lp := entities.ListPodsReport{ 67 Cgroup: pod.CgroupParent(), 68 Created: pod.CreatedTime(), 69 Id: pod.ID(), 70 Name: pod.Name(), 71 Namespace: pod.Namespace(), 72 Status: status, 73 InfraId: infraId, 74 } 75 for _, ctr := range ctrs { 76 state, err := ctr.State() 77 if err != nil { 78 return nil, err 79 } 80 lp.Containers = append(lp.Containers, &entities.ListPodContainer{ 81 Id: ctr.ID(), 82 Names: ctr.Name(), 83 Status: state.String(), 84 }) 85 } 86 lps = append(lps, &lp) 87 } 88 return lps, nil 89 }