Skip to content

Commit

Permalink
Merge pull request #18136 from rhatdan/docker
Browse files Browse the repository at this point in the history
Return title fields as a list
  • Loading branch information
openshift-merge-robot authored Apr 13, 2023
2 parents 16cffcb + 1fa4e45 commit 2b78157
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/api/handlers/compat/containers_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ loop: // break out of for/select infinite` loop
case <-r.Context().Done():
break loop
default:
output, err := c.Top([]string{query.PsArgs})
output, err := c.Top(strings.Split(query.PsArgs, ","))
if err != nil {
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
break loop
}

if len(output) > 0 {
body := handlers.ContainerTopOKBody{}
body.Titles = strings.Split(output[0], "\t")
body.Titles = utils.PSTitles(output[0])

for i := range body.Titles {
body.Titles[i] = strings.TrimSpace(body.Titles[i])
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/handlers/libpod/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ loop: // break out of for/select infinite` loop

if len(output) > 0 {
body := handlers.PodTopOKBody{}
body.Titles = strings.Split(output[0], "\t")
body.Titles = utils.PSTitles(output[0])
for i := range body.Titles {
body.Titles[i] = strings.TrimSpace(body.Titles[i])
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/api/handlers/utils/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/containers/podman/v4/libpod/events"
Expand Down Expand Up @@ -238,3 +239,23 @@ func containerExists(ctx context.Context, name string) (bool, error) {
}
return ctrExistRep.Value, nil
}

// PSTitles merges CAPS headers from ps output. All PS headers are single words, except for
// CAPS. Function compines CAP Headers into single field separated by a space.
func PSTitles(output string) []string {
var titles []string

for _, title := range strings.Fields(output) {
switch title {
case "AMBIENT", "INHERITED", "PERMITTED", "EFFECTIVE", "BOUNDING":
{
titles = append(titles, title+" CAPS")
}
case "CAPS":
continue
default:
titles = append(titles, title)
}
}
return titles
}
8 changes: 8 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ if root; then
.memory_stats.limit=536870912 \
.id~[0-9a-f]\\{64\\}

t GET containers/$CTRNAME/top?stream=false 200 \
.Titles='[
"PID",
"USER",
"TIME",
"COMMAND"
]'

podman rm -f $CTRNAME
fi

Expand Down

0 comments on commit 2b78157

Please sign in to comment.