Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return title fields as a list #18136

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +248 to +258
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we somehow expose this in psgo? This looks rather hacky and will break every-time psgo adds a new header with space. cc @vrothberg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality doesn't look being tested. A flip in psgo wouldn't hurt.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we guaranteed to go through psgo all the time, don't we sometimes still exec ps?

While a new "space" added filed could cause an issue, it would be only a bad formatting on screen, not an actual bug.

Moving this function to psgo makes sense or having psgo expose all of these fields so we could get the list from it would make sense.

}
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