-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
465 lines (390 loc) · 10.9 KB
/
client.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package cgroup
import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"github.com/castai/kvisor/pkg/logging"
"github.com/castai/kvisor/pkg/metrics"
)
var (
baseCgroupPath = ""
ErrContainerIDNotFoundInCgroupPath = errors.New("container id not found in cgroup path")
ErrCgroupNotFound = errors.New("cgroup not found")
)
type ID = uint64
const (
procCgroups = "/proc/cgroups"
cgroupControllersFile = "/sys/fs/cgroup/cgroup.controllers"
cgroupDefaultController = "cpuset"
)
type Version uint8
func (v Version) String() string {
if v == V1 {
return "V1"
}
if v == V2 {
return "V2"
}
return ""
}
const (
V1 = iota
V2
)
// Represents the internal ID of a container runtime
type ContainerRuntimeID int
const (
UnknownRuntime ContainerRuntimeID = iota
DockerRuntime
ContainerdRuntime
CrioRuntime
PodmanRuntime
GardenRuntime
)
var runtimeStringMap = map[ContainerRuntimeID]string{
UnknownRuntime: "unknown",
DockerRuntime: "docker",
ContainerdRuntime: "containerd",
CrioRuntime: "crio",
PodmanRuntime: "podman",
GardenRuntime: "garden", // there is no enricher (yet ?) for garden
}
func (runtime ContainerRuntimeID) String() string {
return runtimeStringMap[runtime]
}
func FromString(str string) ContainerRuntimeID {
switch str {
case "docker":
return DockerRuntime
case "crio":
return CrioRuntime
case "cri-o":
return CrioRuntime
case "podman":
return PodmanRuntime
case "containerd":
return ContainerdRuntime
case "garden":
return GardenRuntime
default:
return UnknownRuntime
}
}
type Client struct {
version Version
cgRoot string
cgroupCacheByID map[ID]func() *Cgroup
cgroupMu sync.RWMutex
defaultHierarchyID uint32
}
func NewClient(log *logging.Logger, root string) (*Client, error) {
version, defaultHierarchyID, err := getDefaultVersionAndHierarchy(log)
if err != nil {
return nil, fmt.Errorf("getting default cgroups version: %w", err)
}
log.WithField("component", "cgroup").Infof("cgroups detected version=%s, root=%s", version, root)
return &Client{
version: version,
cgRoot: root,
cgroupCacheByID: make(map[uint64]func() *Cgroup),
defaultHierarchyID: defaultHierarchyID,
}, nil
}
func (c *Client) lookupCgroupForIDInCache(id ID) (*Cgroup, bool) {
c.cgroupMu.RLock()
defer c.cgroupMu.RUnlock()
if cgroup, found := c.cgroupCacheByID[id]; found {
return cgroup(), true
}
return nil, false
}
func (c *Client) GetCgroupForID(cgroupID ID) (*Cgroup, error) {
if cg, found := c.lookupCgroupForIDInCache(cgroupID); found {
return cg, nil
}
metrics.AgentFindCgroupFS.Inc()
cgroupPath, _ := c.findCgroupPathForID(cgroupID)
if cgroupPath == "" {
return nil, ErrCgroupNotFound
}
cgroup := c.getCgroupForIDAndPath(cgroupID, cgroupPath)
c.cacheCgroup(cgroup)
return cgroup, nil
}
func (c *Client) getCgroupForIDAndPath(cgroupID ID, cgroupPath string) *Cgroup {
containerID, containerRuntime := getContainerIdFromCgroup(cgroupPath)
cg := &Cgroup{
Id: cgroupID,
ContainerID: containerID,
ContainerRuntime: containerRuntime,
Path: cgroupPath,
cgRoot: c.cgRoot,
subsystems: map[string]string{},
}
switch c.version {
case V1:
after, _ := strings.CutPrefix(cgroupPath, cg.cgRoot)
subpath := strings.SplitN(after, "/", 1)
if len(subpath) != 2 {
return cg
}
last := subpath[1]
cg.Version = V1
cg.subsystems = map[string]string{
"cpu": last,
"cpuacct": last,
"memory": last,
"blkio": last,
}
case V2:
after, _ := strings.CutPrefix(cgroupPath, cg.cgRoot)
cg.Version = V2
cg.subsystems = map[string]string{
"": after,
}
}
return cg
}
func (c *Client) getCgroupSearchBasePath() string {
rootDir := c.cgRoot
if c.version == V1 {
// TODO: we hardcode this for now, but in the future we might want to make this configurable
// (cpuset might not always be the first cgroup reported by the kernel)
rootDir = filepath.Join(rootDir, "cpuset")
}
return rootDir
}
func (c *Client) findCgroupPathForID(cgroupId ID) (string, ID) {
found := errors.New("found")
retPath := ""
var cgroupID ID
rootDir := c.getCgroupSearchBasePath()
_ = filepath.Walk(rootDir, func(path string, info fs.FileInfo, err error) error {
// nolint:nilerr
if err != nil || !info.IsDir() {
return nil
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return errors.New("unexpected stat")
}
if (stat.Ino & 0xFFFFFFFF) == (cgroupId & 0xFFFFFFFF) {
retPath = path
cgroupID = stat.Ino
return found
}
return nil
})
if retPath == rootDir {
return "", 0
}
return retPath, cgroupID
}
func (c *Client) DefaultCgroupVersion() Version {
return c.version
}
func (c *Client) IsDefaultHierarchy(hierarchyID uint32) bool {
// There is no such thing as a default hierarchy in cgroup v2, as this only applies to cgroup v1,
// where we need to ensure to always use the same type of cgroup for handling events.
if c.DefaultCgroupVersion() == V2 {
return true
}
return c.defaultHierarchyID == hierarchyID
}
func getDefaultVersionAndHierarchy(log *logging.Logger) (Version, uint32, error) {
// 1st Method: already mounted cgroupv1 filesystem
if ok, _ := isCgroupV2MountedAndDefault(); ok {
return V2, 0, nil
}
//
// 2nd Method: From cgroup man page:
// ...
// 2. The unique ID of the cgroup hierarchy on which this
// controller is mounted. If multiple cgroups v1
// controllers are bound to the same hierarchy, then each
// will show the same hierarchy ID in this field. The
// value in this field will be 0 if:
//
// a) the controller is not mounted on a cgroups v1
// hierarchy;
// b) the controller is bound to the cgroups v2 single
// unified hierarchy; or
// c) the controller is disabled (see below).
// ...
var value uint64
file, err := os.Open(procCgroups)
if err != nil {
return 0, 0, fmt.Errorf("opening %s: %w", procCgroups, err)
}
defer func() {
if err := file.Close(); err != nil {
log.Warnf("closing %s: %v", procCgroups, err)
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Fields(scanner.Text())
if line[0] != cgroupDefaultController {
continue
}
value, err = strconv.ParseUint(line[1], 10, 32)
if err != nil {
return 0, 0, fmt.Errorf("parsing %s: %w", procCgroups, err)
}
}
if value == 0 { // == (a), (b) or (c)
return V2, 0, nil
}
return V1, uint32(value), nil
}
func isCgroupV2MountedAndDefault() (bool, error) {
_, err := os.Stat(cgroupControllersFile)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("opening %s: %w", cgroupControllersFile, err)
}
return true, nil
}
func NewFromProcessCgroupFile(filePath string) (*Cgroup, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
cg := &Cgroup{
subsystems: map[string]string{},
cgRoot: baseCgroupPath,
}
for _, line := range strings.Split(string(data), "\n") {
parts := strings.SplitN(line, ":", 3)
if len(parts) < 3 {
continue
}
for _, cgType := range strings.Split(parts[1], ",") {
cg.subsystems[cgType] = path.Join(baseCgroupPath, parts[2])
}
}
if p := cg.subsystems["cpu"]; p != "" {
cg.Path = p
cg.Version = V1
} else {
cg.Path = cg.subsystems[""]
cg.Version = V2
}
if containerID, runtimeType := getContainerIdFromCgroup(cg.Path); containerID == "" {
return nil, ErrContainerIDNotFoundInCgroupPath
} else {
cg.ContainerID = containerID
cg.ContainerRuntime = runtimeType
}
if cg.Id, err = getCgroupIDForPath(cg.Path); err != nil {
return nil, err
}
return cg, nil
}
var (
containerIdFromCgroupRegex = regexp.MustCompile(`^[A-Fa-f0-9]{64}$`)
gardenContainerIdFromCgroupRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){4}$`)
)
// getContainerIdFromCgroup extracts container id and its runtime from path. It returns
// the container id and the used runtime.
func getContainerIdFromCgroup(cgroupPath string) (string, ContainerRuntimeID) {
cgroupParts := strings.Split(cgroupPath, "/")
// search from the end to get the most inner container id
for i := len(cgroupParts) - 1; i >= 0; i = i - 1 {
pc := cgroupParts[i]
if len(pc) < 28 {
continue // container id is at least 28 characters long
}
runtime := UnknownRuntime
id := strings.TrimSuffix(pc, ".scope")
switch {
case strings.HasPrefix(id, "docker-"):
runtime = DockerRuntime
id = strings.TrimPrefix(id, "docker-")
case strings.HasPrefix(id, "crio-"):
runtime = CrioRuntime
id = strings.TrimPrefix(id, "crio-")
case strings.HasPrefix(id, "cri-containerd-"):
runtime = ContainerdRuntime
id = strings.TrimPrefix(id, "cri-containerd-")
case strings.Contains(pc, ":cri-containerd:"):
runtime = ContainerdRuntime
id = pc[strings.LastIndex(pc, ":cri-containerd:")+len(":cri-containerd:"):]
case strings.HasPrefix(id, "libpod-"):
runtime = PodmanRuntime
id = strings.TrimPrefix(id, "libpod-")
}
if matched := containerIdFromCgroupRegex.MatchString(id); matched {
if runtime == UnknownRuntime && i > 0 && cgroupParts[i-1] == "docker" {
// non-systemd docker with format: .../docker/01adbf...f26db7f/
runtime = DockerRuntime
}
if runtime == UnknownRuntime && i > 0 && cgroupParts[i-1] == "actions_job" {
// non-systemd docker with format in GitHub Actions: .../actions_job/01adbf...f26db7f/
runtime = DockerRuntime
}
if runtime == UnknownRuntime && i > 0 {
for l := i; l > 0; l-- {
if cgroupParts[l] == "kubepods" {
runtime = DockerRuntime
break
}
}
}
// Return the first match, closest to the root dir path component, so that the
// container id of the outer container is returned. The container root is
// determined by being matched on the last path part.
return id, runtime
}
if matched := gardenContainerIdFromCgroupRegex.MatchString(id); matched {
runtime = GardenRuntime
return id, runtime
}
}
// cgroup dirs unrelated to containers provides empty (containerId, runtime)
return "", UnknownRuntime
}
func getCgroupIDForPath(path string) (ID, error) {
// Lower 32 bits of the cgroup id == inode number of matching cgroupfs entry
var stat syscall.Stat_t
if err := syscall.Stat(path, &stat); err != nil {
return 0, err
}
return stat.Ino, nil
}
func (c *Client) LoadCgroup(id ID, path string) {
c.cgroupMu.Lock()
defer c.cgroupMu.Unlock()
if _, found := c.cgroupCacheByID[id]; found {
return
}
c.cgroupCacheByID[id] = sync.OnceValue(func() *Cgroup {
cgroup := c.getCgroupForIDAndPath(id, path)
return cgroup
})
}
func (c *Client) cacheCgroup(cgroup *Cgroup) {
c.cgroupMu.Lock()
c.cgroupCacheByID[cgroup.Id] = func() *Cgroup { return cgroup }
c.cgroupMu.Unlock()
}
func (c *Client) CleanupCgroup(id ID) {
c.cgroupMu.Lock()
defer c.cgroupMu.Unlock()
_, found := c.cgroupCacheByID[id]
if !found {
return
}
delete(c.cgroupCacheByID, id)
}