Skip to content

Commit c5e704c

Browse files
rhatdanopenshift-cherrypick-robot
authored and
openshift-cherrypick-robot
committed
Return title fields as a list
Podman is attempting to split the headers returned by the ps command into a list of headers. Problem is that some headers are multi-word, and headers are not guaranteed to be split via a tab. This PR splits the headers bases on white space, and for the select group of CAPS headers which are multi-word, combines them back together. Fixes: containers#17524 Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 5a83f55 commit c5e704c

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

pkg/api/handlers/compat/containers_top.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ loop: // break out of for/select infinite` loop
6363
case <-r.Context().Done():
6464
break loop
6565
default:
66-
output, err := c.Top([]string{query.PsArgs})
66+
output, err := c.Top(strings.Split(query.PsArgs, ","))
6767
if err != nil {
6868
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
6969
break loop
7070
}
7171

7272
if len(output) > 0 {
7373
body := handlers.ContainerTopOKBody{}
74-
body.Titles = strings.Split(output[0], "\t")
74+
body.Titles = utils.PSTitles(output[0])
75+
7576
for i := range body.Titles {
7677
body.Titles[i] = strings.TrimSpace(body.Titles[i])
7778
}

pkg/api/handlers/libpod/pods.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ loop: // break out of for/select infinite` loop
413413

414414
if len(output) > 0 {
415415
body := handlers.PodTopOKBody{}
416-
body.Titles = strings.Split(output[0], "\t")
416+
body.Titles = utils.PSTitles(output[0])
417417
for i := range body.Titles {
418418
body.Titles[i] = strings.TrimSpace(body.Titles[i])
419419
}

pkg/api/handlers/utils/containers.go

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"strconv"
9+
"strings"
910
"time"
1011

1112
"github.com/containers/podman/v4/libpod/events"
@@ -238,3 +239,23 @@ func containerExists(ctx context.Context, name string) (bool, error) {
238239
}
239240
return ctrExistRep.Value, nil
240241
}
242+
243+
// PSTitles merges CAPS headers from ps output. All PS headers are single words, except for
244+
// CAPS. Function compines CAP Headers into single field separated by a space.
245+
func PSTitles(output string) []string {
246+
var titles []string
247+
248+
for _, title := range strings.Fields(output) {
249+
switch title {
250+
case "AMBIENT", "INHERITED", "PERMITTED", "EFFECTIVE", "BOUNDING":
251+
{
252+
titles = append(titles, title+" CAPS")
253+
}
254+
case "CAPS":
255+
continue
256+
default:
257+
titles = append(titles, title)
258+
}
259+
}
260+
return titles
261+
}

test/apiv2/20-containers.at

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ if root; then
121121
.memory_stats.limit=536870912 \
122122
.id~[0-9a-f]\\{64\\}
123123

124+
t GET containers/$CTRNAME/top?stream=false 200 \
125+
.Titles='[
126+
"PID",
127+
"USER",
128+
"TIME",
129+
"COMMAND"
130+
]'
131+
124132
podman rm -f $CTRNAME
125133
fi
126134

0 commit comments

Comments
 (0)