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