/
cgmgr.go
174 lines (158 loc) · 6.86 KB
/
cgmgr.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
//go:build linux
// +build linux
package cgmgr
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/containers/common/pkg/cgroups"
"github.com/cri-o/cri-o/internal/config/node"
libctr "github.com/opencontainers/runc/libcontainer/cgroups"
cgcfgs "github.com/opencontainers/runc/libcontainer/configs"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
const (
crioPrefix = "crio"
// minMemoryLimit is the minimum memory that must be set for a container.
// A lower value would result in the container failing to start.
// this value has been arrived at for runc on x86_64 hardware
minMemoryLimit = 12 * 1024 * 1024
// CgroupfsCgroupManager represents cgroupfs native cgroup manager
cgroupfsCgroupManager = "cgroupfs"
// SystemdCgroupManager represents systemd native cgroup manager
systemdCgroupManager = "systemd"
DefaultCgroupManager = systemdCgroupManager
// these constants define the path and name of the memory max file
// for v1 and v2 respectively
cgroupMemoryPathV1 = "/sys/fs/cgroup/memory"
cgroupMemoryMaxFileV1 = "memory.limit_in_bytes"
cgroupMemoryPathV2 = "/sys/fs/cgroup"
cgroupMemoryMaxFileV2 = "memory.max"
)
// CgroupManager is an interface to interact with cgroups on a node. CRI-O is configured at startup to either use
// systemd or cgroupfs, and the node itself is booted with cgroup v1, or cgroup v2. CgroupManager is an interface for
// the CRI-O server to use cgroups, regardless of how it or the node was configured.
type CgroupManager interface {
// String returns the name of the cgroup manager (either cgroupfs or systemd)
Name() string
// IsSystemd returns whether it is a systemd cgroup manager
IsSystemd() bool
// ContainerCgroupPath takes arguments sandbox parent cgroup and container ID and returns
// the cgroup path for that containerID. If parentCgroup is empty, it
// uses the default parent for that particular manager
ContainerCgroupPath(string, string) string
// ContainerCgroupAbsolutePath takes arguments sandbox parent cgroup and container ID and
// returns the cgroup path on disk for that containerID. If parentCgroup is empty, it
// uses the default parent for that particular manager
ContainerCgroupAbsolutePath(string, string) (string, error)
// PopulateContainerCgroupStats fills the stats object with information from the cgroup found
// given a cgroup parent and container ID.
PopulateContainerCgroupStats(sbParent, containerID string, stats *types.ContainerStats) error
// SandboxCgroupPath takes the sandbox parent, and sandbox ID. It
// returns the cgroup parent, cgroup path, and error. For systemd cgroups,
// it also checks there is enough memory in the given cgroup
SandboxCgroupPath(string, string) (string, string, error)
// PopulateContainerCgroupStats takes arguments sandbox parent cgroup, and sandbox stats object.
// It fills the object with information from the cgroup found given that parent.
PopulateSandboxCgroupStats(sbParent string, stats *types.PodSandboxStats) error
// MoveConmonToCgroup takes the container ID, cgroup parent, conmon's cgroup (from the config), conmon's PID, and some customized resources
// It attempts to move conmon to the correct cgroup, and set the resources for that cgroup.
// It returns the cgroupfs parent that conmon was put into
// so that CRI-O can clean the parent cgroup of the newly added conmon once the process terminates (systemd handles this for us)
MoveConmonToCgroup(cid, cgroupParent, conmonCgroup string, pid int, resources *rspec.LinuxResources) (string, error)
// CreateSandboxCgroup takes the sandbox parent, and sandbox ID.
// It creates a new cgroup for that sandbox, which is useful when spoofing an infra container.
CreateSandboxCgroup(sbParent, containerID string) error
}
// New creates a new CgroupManager with defaults
func New() CgroupManager {
cm, err := SetCgroupManager(DefaultCgroupManager)
if err != nil {
panic(err)
}
return cm
}
// SetCgroupManager takes a string and branches on it to return
// the type of cgroup manager configured
func SetCgroupManager(cgroupManager string) (CgroupManager, error) {
switch cgroupManager {
case systemdCgroupManager:
return NewSystemdManager(), nil
case cgroupfsCgroupManager:
cgroupfsMgr := CgroupfsManager{
memoryPath: cgroupMemoryPathV1,
memoryMaxFile: cgroupMemoryMaxFileV1,
}
if node.CgroupIsV2() {
cgroupfsMgr.memoryPath = cgroupMemoryPathV2
cgroupfsMgr.memoryMaxFile = cgroupMemoryMaxFileV2
}
return &cgroupfsMgr, nil
default:
return nil, fmt.Errorf("invalid cgroup manager: %s", cgroupManager)
}
}
func verifyCgroupHasEnoughMemory(slicePath, memorySubsystemPath, memoryMaxFilename string) error {
// read in the memory limit from memory max file
fileData, err := os.ReadFile(filepath.Join(memorySubsystemPath, slicePath, memoryMaxFilename))
if err != nil {
if os.IsNotExist(err) {
logrus.Warnf("Failed to find %s at path: %q", memoryMaxFilename, slicePath)
return nil
}
return fmt.Errorf("unable to read memory file for cgroups at %s: %w", slicePath, err)
}
// strip off the newline character and convert it to an int
strMemory := strings.TrimRight(string(fileData), "\n")
if strMemory != "" && strMemory != "max" {
memoryLimit, err := strconv.ParseInt(strMemory, 10, 64)
if err != nil {
return fmt.Errorf("error converting cgroup memory value from string to int %q: %w", strMemory, err)
}
// Compare with the minimum allowed memory limit
if err := VerifyMemoryIsEnough(memoryLimit); err != nil {
return fmt.Errorf("pod %w", err)
}
}
return nil
}
// VerifyMemoryIsEnough verifies that the cgroup memory limit is above a specified minimum memory limit.
func VerifyMemoryIsEnough(memoryLimit int64) error {
if memoryLimit != 0 && memoryLimit < minMemoryLimit {
return fmt.Errorf("set memory limit %d too low; should be at least %d", memoryLimit, minMemoryLimit)
}
return nil
}
// createSandboxCgroup takes the sandbox parent, and sandbox ID.
// It creates a new cgroup for that sandbox, which is useful when spoofing an infra container.
func createSandboxCgroup(sbParent, containerID string, mgr CgroupManager) error {
path, err := mgr.ContainerCgroupAbsolutePath(sbParent, containerID)
if err != nil {
return err
}
_, err = cgroups.New(path, &cgcfgs.Resources{})
return err
}
// MoveProcessToContainerCgroup moves process to the container cgroup
func MoveProcessToContainerCgroup(containerPid, commandPid int) error {
parentCgroupFile := fmt.Sprintf("/proc/%d/cgroup", containerPid)
cgmap, err := libctr.ParseCgroupFile(parentCgroupFile)
if err != nil {
return err
}
var dir string
for controller, path := range cgmap {
// For cgroups V2, controller will be an empty string
dir = filepath.Join("/sys/fs/cgroup", controller, path)
if libctr.PathExists(dir) {
if err := libctr.WriteCgroupProc(dir, commandPid); err != nil {
return err
}
}
}
return nil
}