1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
package entities
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/docker/go-units"
"github.com/pkg/errors"
)
// Listcontainer describes a container suitable for listing
type ListContainer struct {
// Container command
Cmd []string
// Container creation time
Created int64
// If container has exited/stopped
Exited bool
// Time container exited
ExitedAt int64
// If container has exited, the return code from the command
ExitCode int32
// The unique identifier for the container
ID string `json:"Id"`
// Container image
Image string
// If this container is a Pod infra container
IsInfra bool
// Labels for container
Labels map[string]string
// User volume mounts
Mounts []string
// The names assigned to the container
ContainerNames []string
// Namespaces the container belongs to. Requires the
// namespace boolean to be true
Namespaces ListContainerNamespaces
// The process id of the container
Pid int
// If the container is part of Pod, the Pod ID. Requires the pod
// boolean to be set
Pod string
// If the container is part of Pod, the Pod name. Requires the pod
// boolean to be set
PodName string
// Port mappings
PortMappings []ocicni.PortMapping
// Size of the container rootfs. Requires the size boolean to be true
ContainerSize *shared.ContainerSize
// Time when container started
StartedAt int64
// State of container
ContainerState string
}
// State returns the container state in human duration
func (l ListContainer) State() string {
var state string
switch l.ContainerState {
case "running":
t := units.HumanDuration(time.Since(time.Unix(l.StartedAt, 0)))
state = "Up " + t + " ago"
case "configured":
state = "Created"
case "exited":
t := units.HumanDuration(time.Since(time.Unix(l.ExitedAt, 0)))
state = fmt.Sprintf("Exited (%d) %s ago", l.ExitCode, t)
default:
state = l.ContainerState
}
return state
}
// Command returns the container command in string format
func (l ListContainer) Command() string {
return strings.Join(l.Cmd, " ")
}
// Size returns the rootfs and virtual sizes in human duration in
// and output form (string) suitable for ps
func (l ListContainer) Size() string {
virt := units.HumanSizeWithPrecision(float64(l.ContainerSize.RootFsSize), 3)
s := units.HumanSizeWithPrecision(float64(l.ContainerSize.RwSize), 3)
return fmt.Sprintf("%s (virtual %s)", s, virt)
}
// Names returns the container name in string format
func (l ListContainer) Names() string {
return l.ContainerNames[0]
}
// Ports converts from Portmappings to the string form
// required by ps
func (l ListContainer) Ports() string {
if len(l.PortMappings) < 1 {
return ""
}
return portsToString(l.PortMappings)
}
// CreatedAt returns the container creation time in string format. podman
// and docker both return a timestamped value for createdat
func (l ListContainer) CreatedAt() string {
return time.Unix(l.Created, 0).String()
}
// CreateHuman allows us to output the created time in human readable format
func (l ListContainer) CreatedHuman() string {
return units.HumanDuration(time.Since(time.Unix(l.Created, 0))) + " ago"
}
// ListContainer Namespaces contains the identifiers of the container's Linux namespaces
type ListContainerNamespaces struct {
// Mount namespace
MNT string `json:"Mnt,omitempty"`
// Cgroup namespace
Cgroup string `json:"Cgroup,omitempty"`
// IPC namespace
IPC string `json:"Ipc,omitempty"`
// Network namespace
NET string `json:"Net,omitempty"`
// PID namespace
PIDNS string `json:"Pidns,omitempty"`
// UTS namespace
UTS string `json:"Uts,omitempty"`
// User namespace
User string `json:"User,omitempty"`
}
// SortContainers helps us set-up ability to sort by createTime
type SortContainers []*libpod.Container
func (a SortContainers) Len() int { return len(a) }
func (a SortContainers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type SortCreateTime struct{ SortContainers }
func (a SortCreateTime) Less(i, j int) bool {
return a.SortContainers[i].CreatedTime().Before(a.SortContainers[j].CreatedTime())
}
type SortListContainers []ListContainer
func (a SortListContainers) Len() int { return len(a) }
func (a SortListContainers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type psSortedCommand struct{ SortListContainers }
func (a psSortedCommand) Less(i, j int) bool {
return strings.Join(a.SortListContainers[i].Cmd, " ") < strings.Join(a.SortListContainers[j].Cmd, " ")
}
type psSortedId struct{ SortListContainers }
func (a psSortedId) Less(i, j int) bool {
return a.SortListContainers[i].ID < a.SortListContainers[j].ID
}
type psSortedImage struct{ SortListContainers }
func (a psSortedImage) Less(i, j int) bool {
return a.SortListContainers[i].Image < a.SortListContainers[j].Image
}
type psSortedNames struct{ SortListContainers }
func (a psSortedNames) Less(i, j int) bool {
return a.SortListContainers[i].ContainerNames[0] < a.SortListContainers[j].ContainerNames[0]
}
type psSortedPod struct{ SortListContainers }
func (a psSortedPod) Less(i, j int) bool {
return a.SortListContainers[i].Pod < a.SortListContainers[j].Pod
}
type psSortedRunningFor struct{ SortListContainers }
func (a psSortedRunningFor) Less(i, j int) bool {
return a.SortListContainers[i].StartedAt < a.SortListContainers[j].StartedAt
}
type psSortedStatus struct{ SortListContainers }
func (a psSortedStatus) Less(i, j int) bool {
return a.SortListContainers[i].ContainerState < a.SortListContainers[j].ContainerState
}
type psSortedSize struct{ SortListContainers }
func (a psSortedSize) Less(i, j int) bool {
if a.SortListContainers[i].ContainerSize == nil || a.SortListContainers[j].ContainerSize == nil {
return false
}
return a.SortListContainers[i].ContainerSize.RootFsSize < a.SortListContainers[j].ContainerSize.RootFsSize
}
type PsSortedCreateTime struct{ SortListContainers }
func (a PsSortedCreateTime) Less(i, j int) bool {
return a.SortListContainers[i].Created < a.SortListContainers[j].Created
}
func SortPsOutput(sortBy string, psOutput SortListContainers) (SortListContainers, error) {
switch sortBy {
case "id":
sort.Sort(psSortedId{psOutput})
case "image":
sort.Sort(psSortedImage{psOutput})
case "command":
sort.Sort(psSortedCommand{psOutput})
case "runningfor":
sort.Sort(psSortedRunningFor{psOutput})
case "status":
sort.Sort(psSortedStatus{psOutput})
case "size":
sort.Sort(psSortedSize{psOutput})
case "names":
sort.Sort(psSortedNames{psOutput})
case "created":
sort.Sort(PsSortedCreateTime{psOutput})
case "pod":
sort.Sort(psSortedPod{psOutput})
default:
return nil, errors.Errorf("invalid option for --sort, options are: command, created, id, image, names, runningfor, size, or status")
}
return psOutput, nil
}
// portsToString converts the ports used to a string of the from "port1, port2"
// and also groups a continuous list of ports into a readable format.
func portsToString(ports []ocicni.PortMapping) string {
type portGroup struct {
first int32
last int32
}
var portDisplay []string
if len(ports) == 0 {
return ""
}
//Sort the ports, so grouping continuous ports become easy.
sort.Slice(ports, func(i, j int) bool {
return comparePorts(ports[i], ports[j])
})
// portGroupMap is used for grouping continuous ports.
portGroupMap := make(map[string]*portGroup)
var groupKeyList []string
for _, v := range ports {
hostIP := v.HostIP
if hostIP == "" {
hostIP = "0.0.0.0"
}
// If hostPort and containerPort are not same, consider as individual port.
if v.ContainerPort != v.HostPort {
portDisplay = append(portDisplay, fmt.Sprintf("%s:%d->%d/%s", hostIP, v.HostPort, v.ContainerPort, v.Protocol))
continue
}
portMapKey := fmt.Sprintf("%s/%s", hostIP, v.Protocol)
portgroup, ok := portGroupMap[portMapKey]
if !ok {
portGroupMap[portMapKey] = &portGroup{first: v.ContainerPort, last: v.ContainerPort}
// This list is required to traverse portGroupMap.
groupKeyList = append(groupKeyList, portMapKey)
continue
}
if portgroup.last == (v.ContainerPort - 1) {
portgroup.last = v.ContainerPort
continue
}
}
// For each portMapKey, format group list and appned to output string.
for _, portKey := range groupKeyList {
group := portGroupMap[portKey]
portDisplay = append(portDisplay, formatGroup(portKey, group.first, group.last))
}
return strings.Join(portDisplay, ", ")
}
func comparePorts(i, j ocicni.PortMapping) bool {
if i.ContainerPort != j.ContainerPort {
return i.ContainerPort < j.ContainerPort
}
if i.HostIP != j.HostIP {
return i.HostIP < j.HostIP
}
if i.HostPort != j.HostPort {
return i.HostPort < j.HostPort
}
return i.Protocol < j.Protocol
}
// formatGroup returns the group as <IP:startPort:lastPort->startPort:lastPort/Proto>
// e.g 0.0.0.0:1000-1006->1000-1006/tcp.
func formatGroup(key string, start, last int32) string {
parts := strings.Split(key, "/")
groupType := parts[0]
var ip string
if len(parts) > 1 {
ip = parts[0]
groupType = parts[1]
}
group := strconv.Itoa(int(start))
if start != last {
group = fmt.Sprintf("%s-%d", group, last)
}
if ip != "" {
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
}
return fmt.Sprintf("%s/%s", group, groupType)
}
|