podman

Форк
0
/
pod_top_linux.go 
68 строк · 1.9 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"strconv"
7
	"strings"
8

9
	"github.com/containers/podman/v5/libpod/define"
10
	"github.com/containers/podman/v5/pkg/rootless"
11
	"github.com/containers/psgo"
12
)
13

14
// GetPodPidInformation returns process-related data of all processes in
15
// the pod.  The output data can be controlled via the `descriptors`
16
// argument which expects format descriptors and supports all AIXformat
17
// descriptors of ps (1) plus some additional ones to for instance inspect the
18
// set of effective capabilities.  Each element in the returned string slice
19
// is a tab-separated string.
20
//
21
// For more details, please refer to github.com/containers/psgo.
22
func (p *Pod) GetPodPidInformation(descriptors []string) ([]string, error) {
23
	p.lock.Lock()
24
	defer p.lock.Unlock()
25

26
	pids := make([]string, 0)
27
	ctrsInPod, err := p.allContainers()
28
	if err != nil {
29
		return nil, err
30
	}
31
	for _, c := range ctrsInPod {
32
		c.lock.Lock()
33

34
		if err := c.syncContainer(); err != nil {
35
			c.lock.Unlock()
36
			return nil, err
37
		}
38
		if c.state.State == define.ContainerStateRunning {
39
			pid := strconv.Itoa(c.state.PID)
40
			pids = append(pids, pid)
41
		}
42
		c.lock.Unlock()
43
	}
44

45
	// Also support comma-separated input.
46
	psgoDescriptors := []string{}
47
	for _, d := range descriptors {
48
		for _, s := range strings.Split(d, ",") {
49
			if s != "" {
50
				psgoDescriptors = append(psgoDescriptors, s)
51
			}
52
		}
53
	}
54

55
	// NOTE: psgo returns a [][]string to give users the ability to apply
56
	//       filters on the data.  We need to change the API here to return
57
	//       a [][]string if we want to make use of filtering.
58
	opts := psgo.JoinNamespaceOpts{FillMappings: rootless.IsRootless()}
59
	output, err := psgo.JoinNamespaceAndProcessInfoByPidsWithOptions(pids, psgoDescriptors, &opts)
60
	if err != nil {
61
		return nil, err
62
	}
63
	res := []string{}
64
	for _, out := range output {
65
		res = append(res, strings.Join(out, "\t"))
66
	}
67
	return res, nil
68
}
69

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.