cgroup_path.go 5.1 KB
Newer Older
“李磊”'s avatar
“李磊” committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cgroup

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

	"linkfog.com/public/lib/cgroup/fs"
)

func GetContainerMemoryCgroupPath(pid int) (string, error) {
	if IsCgroup2() {
		return GetContainerCgroupPath(pid, "")
	}
	return GetContainerCgroupPath(pid, fs.MemorySubSystemName())
}

func GetContainerCPUCgroupPath(pid int) (string, error) {
	if IsCgroup2() {
		return GetContainerCgroupPath(pid, "")
	}
	return GetContainerCgroupPath(pid, fs.CPUSubSystemName())
}

func GetContainerIOCgroupPath(pid int) (string, error) {
	if IsCgroup2() {
		return GetContainerCgroupPath(pid, "")
	}
	return GetContainerCgroupPath(pid, fs.IOSubSystemName())
}

// 获取容器cgroup路径,当pid为-1时,代表进程自身,cgroup路径选择顺序如下:
// 1.主机视角,有hostPID权限:/proc/1/root/sys/fs/cgroup[/subsystem]/kubepods...
// 2.主机视角,挂载主机host:/host/sys/fs/cgroup[/subsystem]/kubepods...
// 3.容器视角,/proc/<pid>/root/sys/fs/cgroup[/subsystem]
func GetContainerCgroupPath(pid int, subsystem string) (string, error) {
	path, err := getContainerCgroupPathFromHost(pid, subsystem)
	if err == nil {
		return path, nil
	}

	path, err = getContainerCgroupPathFromContainer(pid, subsystem)
	if err == nil {
		return path, nil
	}

	return "", fmt.Errorf("unable to get container cgroup path")
}

// 主机视角,获取容器cgroup的特征
func GetContainerCgroupSpec(pid int, subsystem string) (string, error) {
	// 获取进程cgroup配置文件
	var pidCgroup string
	if pid == -1 {
		pidCgroup = "/proc/self/cgroup"
	} else {
		pidCgroup = filepath.Join("/proc", strconv.Itoa(pid), "cgroup")
	}

	// 解析进程cgroup配置文件,获取相对cgroup路径
	var relativeCgroupPath string
	var err error
	if IsCgroup2() {
		relativeCgroupPath, err = parseRelativeCgroupPathV2(pidCgroup)
	} else {
		relativeCgroupPath, err = parseRelativeCgroupPathV1(pidCgroup, subsystem)
	}
	return relativeCgroupPath, err
}

// 主机视角,获取容器cgroup路径
func getContainerCgroupPathFromHost(pid int, subsystem string) (string, error) {
	// 获取进程cgroup配置文件
	var pidCgroup string
	if pid == -1 {
		pidCgroup = "/proc/self/cgroup"
	} else {
		pidCgroup = filepath.Join("/proc", strconv.Itoa(pid), "cgroup")
	}

	// 解析进程cgroup配置文件,获取相对cgroup路径
	var relativeCgroupPath string
	var err error
	if IsCgroup2() {
		relativeCgroupPath, err = parseRelativeCgroupPathV2(pidCgroup)
	} else {
		relativeCgroupPath, err = parseRelativeCgroupPathV1(pidCgroup, subsystem)
	}
	if err != nil {
		return "", err
	}

	// 生成绝对cgroup路径
	cgroupPrefixs := []string{
		"/proc/1/root/sys/fs/cgroup",
		HostCgroup,
	}
	var absoluteCgroupPath string
	for _, prefix := range cgroupPrefixs {
		if IsCgroup2() {
			absoluteCgroupPath = filepath.Join(prefix, relativeCgroupPath)
		} else {
			absoluteCgroupPath = filepath.Join(prefix, subsystem, relativeCgroupPath)
		}

		if _, err := os.Stat(absoluteCgroupPath); err == nil {
			return absoluteCgroupPath, nil
		}
	}

	return "", fmt.Errorf("unable to get container cgroup path from host")
}

// 容器视角,获取容器cgroup路径
func getContainerCgroupPathFromContainer(pid int, subsystem string) (string, error) {
	var absoluteCgroupPath string
	if pid == -1 {
		absoluteCgroupPath = "/proc/self/root/sys/fs/cgroup"
	} else {
		absoluteCgroupPath = filepath.Join("/proc", strconv.Itoa(pid), "root/sys/fs/cgroup")
	}

	if !IsCgroup2() {
		absoluteCgroupPath = filepath.Join(absoluteCgroupPath, subsystem)
	}

	if _, err := os.Stat(absoluteCgroupPath); err == nil {
		return absoluteCgroupPath, nil
	}

	return "", fmt.Errorf("unable to get container cgroup path from container")
}

func parseRelativeCgroupPathV1(path string, subsystem string) (string, error) {
	f, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), subsystem) {
			tokens := strings.SplitN(scanner.Text(), ":", 3)
			if len(tokens) == 3 {
				return tokens[2], nil
			}
			break
		}
	}
	return "", fmt.Errorf("unable to find subsystem(%s) relative cgroup path "+
		"in container cgroup file", subsystem)
}

func parseRelativeCgroupPathV2(path string) (string, error) {
	f, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer f.Close()

	reg := regexp.MustCompile("([0-9a-z]){64}")
	relativePath := ""
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		tokens := strings.SplitN(scanner.Text(), ":", 3)
		if len(tokens) == 3 {
			// example: "0::/kubepods/besteffort/podad1189b4-15b6-4ee5-b509-08..."
			if strings.HasPrefix(tokens[2], "/kubepods") {
				return tokens[2], nil
			}
			// 当找不到/kubepods时,用于兜底
			if relativePath == "" && reg.FindString(tokens[2]) != "" {
				relativePath = tokens[2]
			}
		}
	}

	if relativePath != "" {
		return relativePath, nil
	}

	return "", fmt.Errorf("unable to find relative cgroup path in container cgroup file")
}