github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/libpod/filters/pods.go (about) 1 package lpfilters 2 3 import ( 4 "strconv" 5 "strings" 6 7 "github.com/containers/libpod/libpod" 8 "github.com/containers/libpod/libpod/define" 9 "github.com/containers/libpod/pkg/util" 10 "github.com/pkg/errors" 11 ) 12 13 // GeneratePodFilterFunc takes a filter and filtervalue (key, value) 14 // and generates a libpod function that can be used to filter 15 // pods 16 func GeneratePodFilterFunc(filter, filterValue string) ( 17 func(pod *libpod.Pod) bool, error) { 18 switch filter { 19 case "ctr-ids": 20 return func(p *libpod.Pod) bool { 21 ctrIds, err := p.AllContainersByID() 22 if err != nil { 23 return false 24 } 25 return util.StringInSlice(filterValue, ctrIds) 26 }, nil 27 case "ctr-names": 28 return func(p *libpod.Pod) bool { 29 ctrs, err := p.AllContainers() 30 if err != nil { 31 return false 32 } 33 for _, ctr := range ctrs { 34 if filterValue == ctr.Name() { 35 return true 36 } 37 } 38 return false 39 }, nil 40 case "ctr-number": 41 return func(p *libpod.Pod) bool { 42 ctrIds, err := p.AllContainersByID() 43 if err != nil { 44 return false 45 } 46 47 fVint, err2 := strconv.Atoi(filterValue) 48 if err2 != nil { 49 return false 50 } 51 return len(ctrIds) == fVint 52 }, nil 53 case "ctr-status": 54 if !util.StringInSlice(filterValue, 55 []string{"created", "restarting", "running", "paused", 56 "exited", "unknown"}) { 57 return nil, errors.Errorf("%s is not a valid status", filterValue) 58 } 59 return func(p *libpod.Pod) bool { 60 ctr_statuses, err := p.Status() 61 if err != nil { 62 return false 63 } 64 for _, ctr_status := range ctr_statuses { 65 state := ctr_status.String() 66 if ctr_status == define.ContainerStateConfigured { 67 state = "created" 68 } 69 if state == filterValue { 70 return true 71 } 72 } 73 return false 74 }, nil 75 case "id": 76 return func(p *libpod.Pod) bool { 77 return strings.Contains(p.ID(), filterValue) 78 }, nil 79 case "name": 80 return func(p *libpod.Pod) bool { 81 return strings.Contains(p.Name(), filterValue) 82 }, nil 83 case "status": 84 if !util.StringInSlice(filterValue, []string{"stopped", "running", "paused", "exited", "dead", "created"}) { 85 return nil, errors.Errorf("%s is not a valid pod status", filterValue) 86 } 87 return func(p *libpod.Pod) bool { 88 status, err := p.GetPodStatus() 89 if err != nil { 90 return false 91 } 92 if strings.ToLower(status) == filterValue { 93 return true 94 } 95 return false 96 }, nil 97 case "label": 98 var filterArray = strings.SplitN(filterValue, "=", 2) 99 var filterKey = filterArray[0] 100 if len(filterArray) > 1 { 101 filterValue = filterArray[1] 102 } else { 103 filterValue = "" 104 } 105 return func(p *libpod.Pod) bool { 106 for labelKey, labelValue := range p.Labels() { 107 if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) { 108 return true 109 } 110 } 111 return false 112 }, nil 113 } 114 return nil, errors.Errorf("%s is an invalid filter", filter) 115 }