github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/entities/container_ps.go (about) 1 package entities 2 3 import ( 4 "sort" 5 "strings" 6 7 "github.com/containers/podman/v2/pkg/ps/define" 8 "github.com/cri-o/ocicni/pkg/ocicni" 9 "github.com/pkg/errors" 10 ) 11 12 // Listcontainer describes a container suitable for listing 13 type ListContainer struct { 14 // Container command 15 Command []string 16 // Container creation time 17 Created int64 18 // Human readable container creation time. 19 CreatedAt string 20 // If container has exited/stopped 21 Exited bool 22 // Time container exited 23 ExitedAt int64 24 // If container has exited, the return code from the command 25 ExitCode int32 26 // The unique identifier for the container 27 ID string `json:"Id"` 28 // Container image 29 Image string 30 // Container image ID 31 ImageID string 32 // If this container is a Pod infra container 33 IsInfra bool 34 // Labels for container 35 Labels map[string]string 36 // User volume mounts 37 Mounts []string 38 // The names assigned to the container 39 Names []string 40 // Namespaces the container belongs to. Requires the 41 // namespace boolean to be true 42 Namespaces ListContainerNamespaces 43 // The process id of the container 44 Pid int 45 // If the container is part of Pod, the Pod ID. Requires the pod 46 // boolean to be set 47 Pod string 48 // If the container is part of Pod, the Pod name. Requires the pod 49 // boolean to be set 50 PodName string 51 // Port mappings 52 Ports []ocicni.PortMapping 53 // Size of the container rootfs. Requires the size boolean to be true 54 Size *define.ContainerSize 55 // Time when container started 56 StartedAt int64 57 // State of container 58 State string 59 // Status is a human-readable approximation of a duration for json output 60 Status string 61 } 62 63 // ListContainer Namespaces contains the identifiers of the container's Linux namespaces 64 type ListContainerNamespaces struct { 65 // Mount namespace 66 MNT string `json:"Mnt,omitempty"` 67 // Cgroup namespace 68 Cgroup string `json:"Cgroup,omitempty"` 69 // IPC namespace 70 IPC string `json:"Ipc,omitempty"` 71 // Network namespace 72 NET string `json:"Net,omitempty"` 73 // PID namespace 74 PIDNS string `json:"Pidns,omitempty"` 75 // UTS namespace 76 UTS string `json:"Uts,omitempty"` 77 // User namespace 78 User string `json:"User,omitempty"` 79 } 80 81 type SortListContainers []ListContainer 82 83 func (a SortListContainers) Len() int { return len(a) } 84 func (a SortListContainers) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 85 86 type psSortedCommand struct{ SortListContainers } 87 88 func (a psSortedCommand) Less(i, j int) bool { 89 return strings.Join(a.SortListContainers[i].Command, " ") < strings.Join(a.SortListContainers[j].Command, " ") 90 } 91 92 type psSortedID struct{ SortListContainers } 93 94 func (a psSortedID) Less(i, j int) bool { 95 return a.SortListContainers[i].ID < a.SortListContainers[j].ID 96 } 97 98 type psSortedImage struct{ SortListContainers } 99 100 func (a psSortedImage) Less(i, j int) bool { 101 return a.SortListContainers[i].Image < a.SortListContainers[j].Image 102 } 103 104 type psSortedNames struct{ SortListContainers } 105 106 func (a psSortedNames) Less(i, j int) bool { 107 return a.SortListContainers[i].Names[0] < a.SortListContainers[j].Names[0] 108 } 109 110 type psSortedPod struct{ SortListContainers } 111 112 func (a psSortedPod) Less(i, j int) bool { 113 return a.SortListContainers[i].Pod < a.SortListContainers[j].Pod 114 } 115 116 type psSortedRunningFor struct{ SortListContainers } 117 118 func (a psSortedRunningFor) Less(i, j int) bool { 119 return a.SortListContainers[i].StartedAt < a.SortListContainers[j].StartedAt 120 } 121 122 type psSortedStatus struct{ SortListContainers } 123 124 func (a psSortedStatus) Less(i, j int) bool { 125 return a.SortListContainers[i].State < a.SortListContainers[j].State 126 } 127 128 type psSortedSize struct{ SortListContainers } 129 130 func (a psSortedSize) Less(i, j int) bool { 131 if a.SortListContainers[i].Size == nil || a.SortListContainers[j].Size == nil { 132 return false 133 } 134 return a.SortListContainers[i].Size.RootFsSize < a.SortListContainers[j].Size.RootFsSize 135 } 136 137 type PsSortedCreateTime struct{ SortListContainers } 138 139 func (a PsSortedCreateTime) Less(i, j int) bool { 140 return a.SortListContainers[i].Created < a.SortListContainers[j].Created 141 } 142 143 func SortPsOutput(sortBy string, psOutput SortListContainers) (SortListContainers, error) { 144 switch sortBy { 145 case "id": 146 sort.Sort(psSortedID{psOutput}) 147 case "image": 148 sort.Sort(psSortedImage{psOutput}) 149 case "command": 150 sort.Sort(psSortedCommand{psOutput}) 151 case "runningfor": 152 sort.Sort(psSortedRunningFor{psOutput}) 153 case "status": 154 sort.Sort(psSortedStatus{psOutput}) 155 case "size": 156 sort.Sort(psSortedSize{psOutput}) 157 case "names": 158 sort.Sort(psSortedNames{psOutput}) 159 case "created": 160 sort.Sort(PsSortedCreateTime{psOutput}) 161 case "pod": 162 sort.Sort(psSortedPod{psOutput}) 163 default: 164 return nil, errors.Errorf("invalid option for --sort, options are: command, created, id, image, names, runningfor, size, or status") 165 } 166 return psOutput, nil 167 } 168 169 func (l ListContainer) CGROUPNS() string { 170 return l.Namespaces.Cgroup 171 } 172 173 func (l ListContainer) IPC() string { 174 return l.Namespaces.IPC 175 } 176 177 func (l ListContainer) MNT() string { 178 return l.Namespaces.MNT 179 } 180 181 func (l ListContainer) NET() string { 182 return l.Namespaces.NET 183 } 184 185 func (l ListContainer) PIDNS() string { 186 return l.Namespaces.PIDNS 187 } 188 189 func (l ListContainer) USERNS() string { 190 return l.Namespaces.User 191 } 192 193 func (l ListContainer) UTS() string { 194 return l.Namespaces.UTS 195 }