Skip to content

Commit 375a279

Browse files
dmkendeloof
authored andcommitted
top: expose container labels
Signed-off-by: Dominik Menke <[email protected]>
1 parent a766e16 commit 375a279

File tree

4 files changed

+90
-60
lines changed

4 files changed

+90
-60
lines changed

cmd/compose/top.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@ func runTop(ctx context.Context, dockerCli command.Cli, backend api.Service, opt
7373
func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries) {
7474
// map column name to its header (should keep working if backend.Top returns
7575
// varying columns for different containers)
76-
header := topHeader{"SERVICE": 0}
76+
header := topHeader{"SERVICE": 0, "#": 1}
7777

7878
// assume one process per container and grow if needed
7979
entries := make([]topEntries, 0, len(containers))
8080

8181
for _, container := range containers {
8282
for _, proc := range container.Processes {
83-
entry := topEntries{"SERVICE": container.Name}
83+
svc := container.Name
84+
if tmp, ok := container.Labels[api.ServiceLabel]; ok {
85+
svc = tmp
86+
}
87+
replica := "-"
88+
if tmp, ok := container.Labels[api.ContainerNumberLabel]; ok {
89+
replica = tmp
90+
}
91+
92+
entry := topEntries{"SERVICE": svc, "#": replica}
8493

8594
for i, title := range container.Titles {
8695
if _, exists := header[title]; !exists {

cmd/compose/top_test.go

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var topTestCases = []struct {
3939
name: "noprocs",
4040
titles: []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"},
4141
procs: [][]string{},
42-
header: topHeader{"SERVICE": 0},
42+
header: topHeader{"SERVICE": 0, "#": 1},
4343
entries: []topEntries{},
4444
output: "",
4545
},
@@ -49,18 +49,20 @@ var topTestCases = []struct {
4949
procs: [][]string{{"root", "1", "1", "0", "12:00", "?", "00:00:01", "/entrypoint"}},
5050
header: topHeader{
5151
"SERVICE": 0,
52-
"UID": 1,
53-
"PID": 2,
54-
"PPID": 3,
55-
"C": 4,
56-
"STIME": 5,
57-
"TTY": 6,
58-
"TIME": 7,
59-
"CMD": 8,
52+
"#": 1,
53+
"UID": 2,
54+
"PID": 3,
55+
"PPID": 4,
56+
"C": 5,
57+
"STIME": 6,
58+
"TTY": 7,
59+
"TIME": 8,
60+
"CMD": 9,
6061
},
6162
entries: []topEntries{
6263
{
6364
"SERVICE": "simple",
65+
"#": "1",
6466
"UID": "root",
6567
"PID": "1",
6668
"PPID": "1",
@@ -72,8 +74,8 @@ var topTestCases = []struct {
7274
},
7375
},
7476
output: trim(`
75-
SERVICE UID PID PPID C STIME TTY TIME CMD
76-
simple root 1 1 0 12:00 ? 00:00:01 /entrypoint
77+
SERVICE # UID PID PPID C STIME TTY TIME CMD
78+
simple 1 root 1 1 0 12:00 ? 00:00:01 /entrypoint
7779
`),
7880
},
7981
{
@@ -82,17 +84,19 @@ var topTestCases = []struct {
8284
procs: [][]string{{"root", "1", "0", "12:00", "?", "00:00:02", "/entrypoint"}},
8385
header: topHeader{
8486
"SERVICE": 0,
85-
"UID": 1,
86-
"PID": 2,
87-
"C": 3,
88-
"STIME": 4,
89-
"TTY": 5,
90-
"TIME": 6,
91-
"CMD": 7,
87+
"#": 1,
88+
"UID": 2,
89+
"PID": 3,
90+
"C": 4,
91+
"STIME": 5,
92+
"TTY": 6,
93+
"TIME": 7,
94+
"CMD": 8,
9295
},
9396
entries: []topEntries{
9497
{
9598
"SERVICE": "noppid",
99+
"#": "1",
96100
"UID": "root",
97101
"PID": "1",
98102
"C": "0",
@@ -103,8 +107,8 @@ var topTestCases = []struct {
103107
},
104108
},
105109
output: trim(`
106-
SERVICE UID PID C STIME TTY TIME CMD
107-
noppid root 1 0 12:00 ? 00:00:02 /entrypoint
110+
SERVICE # UID PID C STIME TTY TIME CMD
111+
noppid 1 root 1 0 12:00 ? 00:00:02 /entrypoint
108112
`),
109113
},
110114
{
@@ -113,19 +117,21 @@ var topTestCases = []struct {
113117
procs: [][]string{{"root", "1", "1", "1", "0", "12:00", "?", "00:00:03", "/entrypoint"}},
114118
header: topHeader{
115119
"SERVICE": 0,
116-
"UID": 1,
117-
"GID": 2,
118-
"PID": 3,
119-
"PPID": 4,
120-
"C": 5,
121-
"STIME": 6,
122-
"TTY": 7,
123-
"TIME": 8,
124-
"CMD": 9,
120+
"#": 1,
121+
"UID": 2,
122+
"GID": 3,
123+
"PID": 4,
124+
"PPID": 5,
125+
"C": 6,
126+
"STIME": 7,
127+
"TTY": 8,
128+
"TIME": 9,
129+
"CMD": 10,
125130
},
126131
entries: []topEntries{
127132
{
128133
"SERVICE": "extra-hdr",
134+
"#": "1",
129135
"UID": "root",
130136
"GID": "1",
131137
"PID": "1",
@@ -138,8 +144,8 @@ var topTestCases = []struct {
138144
},
139145
},
140146
output: trim(`
141-
SERVICE UID GID PID PPID C STIME TTY TIME CMD
142-
extra-hdr root 1 1 1 0 12:00 ? 00:00:03 /entrypoint
147+
SERVICE # UID GID PID PPID C STIME TTY TIME CMD
148+
extra-hdr 1 root 1 1 1 0 12:00 ? 00:00:03 /entrypoint
143149
`),
144150
},
145151
{
@@ -151,18 +157,20 @@ var topTestCases = []struct {
151157
},
152158
header: topHeader{
153159
"SERVICE": 0,
154-
"UID": 1,
155-
"PID": 2,
156-
"PPID": 3,
157-
"C": 4,
158-
"STIME": 5,
159-
"TTY": 6,
160-
"TIME": 7,
161-
"CMD": 8,
160+
"#": 1,
161+
"UID": 2,
162+
"PID": 3,
163+
"PPID": 4,
164+
"C": 5,
165+
"STIME": 6,
166+
"TTY": 7,
167+
"TIME": 8,
168+
"CMD": 9,
162169
},
163170
entries: []topEntries{
164171
{
165172
"SERVICE": "multiple",
173+
"#": "1",
166174
"UID": "root",
167175
"PID": "1",
168176
"PPID": "1",
@@ -174,6 +182,7 @@ var topTestCases = []struct {
174182
},
175183
{
176184
"SERVICE": "multiple",
185+
"#": "1",
177186
"UID": "root",
178187
"PID": "123",
179188
"PPID": "1",
@@ -185,9 +194,9 @@ var topTestCases = []struct {
185194
},
186195
},
187196
output: trim(`
188-
SERVICE UID PID PPID C STIME TTY TIME CMD
189-
multiple root 1 1 0 12:00 ? 00:00:04 /entrypoint
190-
multiple root 123 1 0 12:00 ? 00:00:42 sleep infinity
197+
SERVICE # UID PID PPID C STIME TTY TIME CMD
198+
multiple 1 root 1 1 0 12:00 ? 00:00:04 /entrypoint
199+
multiple 1 root 123 1 0 12:00 ? 00:00:42 sleep infinity
191200
`),
192201
},
193202
}
@@ -201,9 +210,13 @@ func TestRunTopCore(t *testing.T) {
201210

202211
for _, tc := range topTestCases {
203212
summary := api.ContainerProcSummary{
204-
Name: tc.name,
213+
Name: "not used",
205214
Titles: tc.titles,
206215
Processes: tc.procs,
216+
Labels: map[string]string{
217+
api.ServiceLabel: tc.name,
218+
api.ContainerNumberLabel: "1",
219+
},
207220
}
208221
all = append(all, summary)
209222

@@ -224,19 +237,21 @@ func TestRunTopCore(t *testing.T) {
224237
header, entries := collectTop(all)
225238
assert.EqualValues(t, topHeader{
226239
"SERVICE": 0,
227-
"UID": 1,
228-
"PID": 2,
229-
"PPID": 3,
230-
"C": 4,
231-
"STIME": 5,
232-
"TTY": 6,
233-
"TIME": 7,
234-
"CMD": 8,
235-
"GID": 9,
240+
"#": 1,
241+
"UID": 2,
242+
"PID": 3,
243+
"PPID": 4,
244+
"C": 5,
245+
"STIME": 6,
246+
"TTY": 7,
247+
"TIME": 8,
248+
"CMD": 9,
249+
"GID": 10,
236250
}, header)
237251
assert.EqualValues(t, []topEntries{
238252
{
239253
"SERVICE": "simple",
254+
"#": "1",
240255
"UID": "root",
241256
"PID": "1",
242257
"PPID": "1",
@@ -247,6 +262,7 @@ func TestRunTopCore(t *testing.T) {
247262
"CMD": "/entrypoint",
248263
}, {
249264
"SERVICE": "noppid",
265+
"#": "1",
250266
"UID": "root",
251267
"PID": "1",
252268
"C": "0",
@@ -256,6 +272,7 @@ func TestRunTopCore(t *testing.T) {
256272
"CMD": "/entrypoint",
257273
}, {
258274
"SERVICE": "extra-hdr",
275+
"#": "1",
259276
"UID": "root",
260277
"GID": "1",
261278
"PID": "1",
@@ -267,6 +284,7 @@ func TestRunTopCore(t *testing.T) {
267284
"CMD": "/entrypoint",
268285
}, {
269286
"SERVICE": "multiple",
287+
"#": "1",
270288
"UID": "root",
271289
"PID": "1",
272290
"PPID": "1",
@@ -277,6 +295,7 @@ func TestRunTopCore(t *testing.T) {
277295
"CMD": "/entrypoint",
278296
}, {
279297
"SERVICE": "multiple",
298+
"#": "1",
280299
"UID": "root",
281300
"PID": "123",
282301
"PPID": "1",
@@ -292,12 +311,12 @@ func TestRunTopCore(t *testing.T) {
292311
err := topPrint(&buf, header, entries)
293312
require.NoError(t, err)
294313
assert.Equal(t, trim(`
295-
SERVICE UID PID PPID C STIME TTY TIME CMD GID
296-
simple root 1 1 0 12:00 ? 00:00:01 /entrypoint -
297-
noppid root 1 - 0 12:00 ? 00:00:02 /entrypoint -
298-
extra-hdr root 1 1 0 12:00 ? 00:00:03 /entrypoint 1
299-
multiple root 1 1 0 12:00 ? 00:00:04 /entrypoint -
300-
multiple root 123 1 0 12:00 ? 00:00:42 sleep infinity -
314+
SERVICE # UID PID PPID C STIME TTY TIME CMD GID
315+
simple 1 root 1 1 0 12:00 ? 00:00:01 /entrypoint -
316+
noppid 1 root 1 - 0 12:00 ? 00:00:02 /entrypoint -
317+
extra-hdr 1 root 1 1 0 12:00 ? 00:00:03 /entrypoint 1
318+
multiple 1 root 1 1 0 12:00 ? 00:00:04 /entrypoint -
319+
multiple 1 root 123 1 0 12:00 ? 00:00:42 sleep infinity -
301320
`), buf.String())
302321

303322
})

pkg/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ type ContainerProcSummary struct {
523523
Name string
524524
Processes [][]string
525525
Titles []string
526+
Labels map[string]string
526527
}
527528

528529
// ImageSummary holds container image description

pkg/compose/top.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (s *composeService) Top(ctx context.Context, projectName string, services [
4747
Name: getCanonicalContainerName(ctr),
4848
Processes: topContent.Processes,
4949
Titles: topContent.Titles,
50+
Labels: container.Labels,
5051
}
5152
return nil
5253
})

0 commit comments

Comments
 (0)