14
14
limitations under the License.
15
15
*/
16
16
17
+ /*
18
+ Portions from:
19
+ - https://github.com/moby/moby/blob/v20.10.6/api/types/container/container_top.go
20
+ - https://github.com/moby/moby/blob/v20.10.6/daemon/top_unix.go
21
+ Copyright (C) The Moby authors.
22
+ Licensed under the Apache License, Version 2.0
23
+ */
24
+
17
25
package main
18
26
19
27
import (
@@ -34,6 +42,8 @@ import (
34
42
"github.com/urfave/cli/v2"
35
43
)
36
44
45
+ // ContainerTopOKBody is from https://github.com/moby/moby/blob/v20.10.6/api/types/container/container_top.go
46
+ //
37
47
// ContainerTopOKBody OK response to ContainerTop operation
38
48
type ContainerTopOKBody struct {
39
49
@@ -65,7 +75,11 @@ func topAction(clicontext *cli.Context) error {
65
75
// NOTE: rootless container does not rely on cgroupv1.
66
76
// more details about possible ways to resolve this concern: #223
67
77
if rootlessutil .IsRootless () && infoutil .CgroupsVersion () == "1" {
68
- return fmt .Errorf ("top is not supported for rootless container and cgroupv1" )
78
+ return errors .Errorf ("top requires cgroup v2 for rootless containers, see https://rootlesscontaine.rs/getting-started/common/cgroup2/" )
79
+ }
80
+
81
+ if clicontext .String ("cgroup-manager" ) == "none" {
82
+ return errors .New ("cgroup manager must not be \" none\" " )
69
83
}
70
84
71
85
client , ctx , cancel , err := newClient (clicontext )
@@ -93,7 +107,7 @@ func topAction(clicontext *cli.Context) error {
93
107
return nil
94
108
}
95
109
96
- //function from moby/moby/daemon/top_unix.go
110
+ // appendProcess2ProcList is from https://github.com/ moby/moby/blob/v20.10.6/ daemon/top_unix.go#L49-L55
97
111
func appendProcess2ProcList (procList * ContainerTopOKBody , fields []string ) {
98
112
// Make sure number of fields equals number of header titles
99
113
// merging "overhanging" fields
@@ -102,7 +116,8 @@ func appendProcess2ProcList(procList *ContainerTopOKBody, fields []string) {
102
116
procList .Processes = append (procList .Processes , process )
103
117
}
104
118
105
- //function from moby/moby/daemon/top_unix.go
119
+ // psPidsArg is from https://github.com/moby/moby/blob/v20.10.6/daemon/top_unix.go#L119-L131
120
+ //
106
121
// psPidsArg converts a slice of PIDs to a string consisting
107
122
// of comma-separated list of PIDs prepended by "-q".
108
123
// For example, psPidsArg([]uint32{1,2,3}) returns "-q1,2,3".
@@ -117,7 +132,7 @@ func psPidsArg(pids []uint32) string {
117
132
return string (b )
118
133
}
119
134
120
- //function from moby/moby/daemon/top_unix.go
135
+ // validatePSArgs is from https://github.com/ moby/moby/blob/v20.10.6/ daemon/top_unix.go#L19-L35
121
136
func validatePSArgs (psArgs string ) error {
122
137
// NOTE: \\s does not detect unicode whitespaces.
123
138
// So we use fieldsASCII instead of strings.Fields in parsePSOutput.
@@ -136,7 +151,8 @@ func validatePSArgs(psArgs string) error {
136
151
return nil
137
152
}
138
153
139
- //function from moby/moby/daemon/top_unix.go
154
+ // fieldsASCII is from https://github.com/moby/moby/blob/v20.10.6/daemon/top_unix.go#L37-L47
155
+ //
140
156
// fieldsASCII is similar to strings.Fields but only allows ASCII whitespaces
141
157
func fieldsASCII (s string ) []string {
142
158
fn := func (r rune ) bool {
@@ -149,7 +165,7 @@ func fieldsASCII(s string) []string {
149
165
return strings .FieldsFunc (s , fn )
150
166
}
151
167
152
- //function from moby/moby/daemon/top_unix.go
168
+ // hasPid is from https://github.com/ moby/moby/blob/v20.10.6/ daemon/top_unix.go#L57-L64
153
169
func hasPid (procs []uint32 , pid int ) bool {
154
170
for _ , p := range procs {
155
171
if int (p ) == pid {
@@ -159,7 +175,7 @@ func hasPid(procs []uint32, pid int) bool {
159
175
return false
160
176
}
161
177
162
- //function from moby/moby/daemon/top_unix.go
178
+ // parsePSOutput is from https://github.com/ moby/moby/blob/v20.10.6/ daemon/top_unix.go#L66-L117
163
179
func parsePSOutput (output []byte , procs []uint32 ) (* ContainerTopOKBody , error ) {
164
180
procList := & ContainerTopOKBody {}
165
181
@@ -213,7 +229,8 @@ func parsePSOutput(output []byte, procs []uint32) (*ContainerTopOKBody, error) {
213
229
return procList , nil
214
230
}
215
231
216
- // function inspired from moby/moby/daemon/top_unix.go
232
+ // containerTop was inspired from https://github.com/moby/moby/blob/v20.10.6/daemon/top_unix.go#L133-L189
233
+ //
217
234
// ContainerTop lists the processes running inside of the given
218
235
// container by calling ps with the given args, or with the flags
219
236
// "-ef" if no args are given. An error is returned if the container
@@ -298,6 +315,10 @@ func topBashComplete(clicontext *cli.Context) {
298
315
defaultBashComplete (clicontext )
299
316
return
300
317
}
301
- // show container names (TODO: only running containers)
302
- bashCompleteContainerNames (clicontext , nil )
318
+
319
+ // show running container names
320
+ statusFilterFn := func (st containerd.ProcessStatus ) bool {
321
+ return st == containerd .Running
322
+ }
323
+ bashCompleteContainerNames (clicontext , statusFilterFn )
303
324
}
0 commit comments