Merge pull request #19412 from dfr/freebsd-pod-top

libpod: add 'pod top' support on FreeBSD
This commit is contained in:
OpenShift Merge Robot
2023-07-28 13:47:24 +02:00
committed by GitHub
5 changed files with 124 additions and 11 deletions

View File

@ -310,11 +310,35 @@ func (c *Container) getConmonPidFd() int {
return -1
}
func (c *Container) jailName() string {
if c.state.NetNS != "" {
return c.state.NetNS + "." + c.ID()
func (c *Container) jailName() (string, error) {
// If this container is in a pod, get the vnet name from the
// corresponding infra container
var ic *Container
if c.config.Pod != "" && c.config.Pod != c.ID() {
// Get the pod from state
pod, err := c.runtime.state.Pod(c.config.Pod)
if err != nil {
return "", fmt.Errorf("cannot find infra container for pod %s: %w", c.config.Pod, err)
}
ic, err = pod.InfraContainer()
if err != nil {
return "", fmt.Errorf("getting infra container for pod %s: %w", pod.ID(), err)
}
if ic.ID() != c.ID() {
ic.lock.Lock()
defer ic.lock.Unlock()
if err := ic.syncContainer(); err != nil {
return "", err
}
}
} else {
return c.ID()
ic = c
}
if ic.state.NetNS != "" {
return ic.state.NetNS + "." + c.ID(), nil
} else {
return c.ID(), nil
}
}

View File

@ -75,13 +75,18 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
}
}
jailName, err := c.jailName()
if err != nil {
return nil, fmt.Errorf("getting jail name: %w", err)
}
args := []string{
"-J",
c.jailName(),
jailName,
}
args = append(args, psDescriptors...)
output, err := c.execPS(args)
output, err := execPS(args)
if err != nil {
return nil, fmt.Errorf("executing ps(1): %w", err)
}
@ -89,7 +94,7 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
return output, nil
}
func (c *Container) execPS(args []string) ([]string, error) {
func execPS(args []string) ([]string, error) {
cmd := exec.Command("ps", args...)
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {

79
libpod/pod_top_freebsd.go Normal file
View File

@ -0,0 +1,79 @@
//go:build freebsd
// +build freebsd
package libpod
import (
"fmt"
"strings"
"github.com/containers/podman/v4/libpod/define"
)
// GetPodPidInformation returns process-related data of all processes in
// the pod. The output data can be controlled via the `descriptors`
// argument which expects format descriptors and supports all AIXformat
// descriptors of ps (1) plus some additional ones to for instance inspect the
// set of effective capabilities. Each element in the returned string slice
// is a tab-separated string.
//
// For more details, please refer to github.com/containers/psgo.
func (p *Pod) GetPodPidInformation(descriptors []string) ([]string, error) {
// Default to 'ps -ef' compatible descriptors
if len(strings.Join(descriptors, "")) == 0 {
descriptors = []string{"user", "pid", "ppid", "pcpu", "etime", "tty", "time", "args"}
}
jailNames := make([]string, 0)
ctrsInPod, err := p.AllContainers()
if err != nil {
return nil, err
}
for _, c := range ctrsInPod {
c.lock.Lock()
err := c.syncContainer()
c.lock.Unlock()
if err != nil {
return nil, err
}
if c.state.State == define.ContainerStateRunning {
jailName, err := c.jailName()
if err != nil {
return nil, fmt.Errorf("getting jail name: %w", err)
}
jailNames = append(jailNames, jailName)
}
}
// Also support comma-separated input.
psDescriptors := []string{}
for _, d := range descriptors {
for _, s := range strings.Split(d, ",") {
if s != "" {
psDescriptors = append(psDescriptors, s)
}
}
}
// For consistency with pod_top_linux.go, only allow descriptor names
for _, d := range psDescriptors {
if _, ok := isDescriptor[d]; !ok {
return nil, fmt.Errorf("unknown descriptor: %s", d)
}
}
args := []string{
"-J",
strings.Join(jailNames, ","),
"-ao",
strings.Join(psDescriptors, ","),
}
output, err := execPS(args)
if err != nil {
return nil, fmt.Errorf("executing ps(1): %w", err)
}
return output, nil
}

View File

@ -1,5 +1,5 @@
//go:build !linux
// +build !linux
//go:build !linux && !freebsd
// +build !linux,!freebsd
package libpod

View File

@ -20,9 +20,14 @@ import (
func (c *Container) getPlatformContainerStats(stats *define.ContainerStats, previousStats *define.ContainerStats) error {
now := uint64(time.Now().UnixNano())
entries, err := rctl.GetRacct("jail:" + c.jailName())
jailName, err := c.jailName()
if err != nil {
return fmt.Errorf("unable to read accounting for %s: %w", c.jailName(), err)
return fmt.Errorf("getting jail name: %w", err)
}
entries, err := rctl.GetRacct("jail:" + jailName)
if err != nil {
return fmt.Errorf("unable to read accounting for %s: %w", jailName, err)
}
// If the current total usage is less than what was previously